Tuesday, March 19, 2024

Lists

 

Lists
In Computer Science, we learn about a data structure called arrays. Arrays are a place to store more than one thing with the same variable name.

However, Python uses lists instead. Lists are literally lists of items. Any piece of data from any data type can go into a list. We can extract, remove, or change lists.

You may be asking: "What is the point of a list?"

Sometimes, you don't always know how much data you need to store. We can use a loop to move through data in a list without having to first manually tell the computer how many things are in that list.

Starting at 0
As far as Python is concerned, this is a list. Notice we start counting the first item at 0 (instead of 1).


Example: In this list, "lots" is index 0, "of" is index 1, etc.

We can directly add to the list with the variable name, [ ] with the index number of the row.


Printing Lists
We can print out data in the same way.

Let's make a list of our class schedule.

timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
print(timetable)

That looks awful with all the [ ],"" printing too! If I want to print out index 1 in my timetable, I need to tell the computer!



timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
print(timetable[1])

Changing Lists
We can also change lists and the index.
We built our list with timetable =, but we want to change index 4, "sport". We can do this by calling it with [ ].

timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
print(timetable[0])
print(timetable[1])
print(timetable[2])
print(timetable[3])
print(timetable[4])

Add this to to the code above:
timetable[4]= "Watch TV"
Why is it not printing correctly? I have created the timetable, printed it out, and changed index 4 of the timetable.
However, I need to print the changed version. Let's print our new index:

timetable = ["Computer Science", "Math", "English", "Art", "Sport"]
timetable[4]= "Watch TV"
print(timetable[0])
print(timetable[1])
print(timetable[2])
print(timetable[3])
print(timetable[4])

Lists and Loops
Why would I want to write all of those lines of code?
Introducing lists' best friend...loops

We can replace a lot of those lines of code we just wrote with just two lines of code. Change your code to look like this:

timetable = ["Computer Science", "Math", "English", "Art", "Watch TV"]
for lesson in timetable:
  print(lesson)

Remember, for loops work by creating the variable right after the word for and setting it equal to each value in a list (so far we have only used numbers with the range function).


Coding Challenge ;
Create a list that stores greetings in different languages. .
Import random library. Generate a random number between 0 and maximum number of items in your list.
At random, when the user clicks run, print one of the greetings.

while True:
  ask = input("want to play(y/n): ")
  if ask == "y":
    import random
    u= ["gh","hu","iuy","gyu","hui"]
    randi = random.randint(0,3)
    print(u[randi])
  else :
    break

No comments:

Post a Comment

  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...