This is an old revision of the document!
Table of Contents
Lists
If you didn't know what a Class is in Python, you should know by the end of this lesson series. That is because this lesson series dives deep into what a list is, and we will also explore trees! We're not going to specifically focus on 'what is a class', it should be intuitive based on how the code works.
The Fundamental Question
What is a list?
This is a question which has reverberated throughout history. If it is not obvious, a list is an ordered group of names. For example, a grocery list might contain “bread”, “milk”, “eggs” and “rice” – while a list of animals at the zoo might contain “zebra”, “snake”, “monkey”, “bear”, “tiger”, “goat” and “horse”. Rabbits too, especially if it is a petting zoo. Sometimes you can feed the animals too and it is fun.
So, what is a list?
List Example 1
def list_example_1(): list = [] a = "Apple" b = "Banana" c = "Cherry" d = "Durian" list.append(a) list.append(b) list.append(c) list.append(d) print(list)
The code [] defines an empty list. Then we can add things to the list by using .append(). We can even print the list using print(). All of this is shown above. Another way to define a list is to add the data directly, without variables:
List Example 2
def list_example_2(): list = [] list.append("Egg") list.append("Fish") list.append("Grape") list.append("Hotdog") print(list)
This way is less common because it means the data is hard-coded into your program. If you use variables, you can use input() to ask the user for information, or you can read it from a file, et cetera. But the point is you can define a list this way. You can also do this:
List Example 3
def list_example_3(): list = ["Ice cream", "Juice", "Kiwi", "Lemonade"] print(list)
Finally, just to show you, you can define a list like this:
List Example 3
def list_example_4(): a = "Mango" b = "Nuts" c = "Orange" d = "Papaya" list = [a, b, c, d] print(list)
Classwork
Your task is simple. Please write a function that prints a random list of fruit.
How do you do it? It's simple, First, write out the steps of the program.
- 1. Choose a random fruit.
- 2. Add the fruit to a list.
- 3. Repeat
- 4. Print the list
As you know, we can break each task into a small function. First let's write a function that gives us a random fruit.
random_fruit()
def random_fruit(): fruit_list = [ "Apple", "Banana", "Canteloupe", "Cherry", "Durian", "Dragon Fruit", "Kiwi", "Mango", "Pineapple", "Watermelon" ] count = len(fruit_list) n = random.choice(range(count)) return fruit_list[n]
At this point, a simple program can print random fruits:
example
f = random_fruit() print(f)
It's finally time to write the actual program! We will create a list of random fruits, and print it!
import random def main(): num = 5 list = [] for x in range(num): f = random_fruit() list.append(f) print(list) ##### def random_fruit(): fruit_list = [ "Apple", "Banana", "Canteloupe", "Cherry", "Durian", "Dragon Fruit", "Kiwi", "Mango", "Pineapple", "Watermelon" ] count = len(fruit_list) n = random.choice(range(count)) return fruit_list[n] ##### if __name__ == '__main__': main()