User Tools

Site Tools


lists

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 = []

    while len(list) < 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()

Homework

Now you can build a function that pairs students into groups. There basic idea is the same, but, make sure that the same student is not added to a group using the check “if name not in list:”. This will allow you to make a group of students which do not contain the same student. If you wish to create a number of groups at the same time, you can use the pop, del, or remove commands. For example;

    n = random.choice(len(students))
    s = students.pop(n)
    group.append(s)

The code above moves the student out of the list called “students” and adds them to the list called “group”. The benefit of this is that since it removes the student from the pool of possible choices, you do not need to check for duplicates when randomly picking a student.

Next

Next, we can look at Lists II, however, you might also want to look at Basics III to see how lists are used in action!

lists.txt · Last modified: 2023/10/27 02:55 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki