Skip to main content
  1. Posts/

List Part 2

·207 words·1 min

Today we will be learning about list part 2

In list you can use a method to pull the object by using its index for example:

>>> fruit = ["banana", "grape", "apples", "mango"]
>>> fruit[0]
'banana'
>>>

The benefits of this method is that if you want to grab an element from the list you can use variable[index number]

#reminder

the index number starts with 0 for example banana is 0 grape is 1 and so on

Another thing I want to show you is that you have to use brackets not parentheses other why it will become a tuple and that is what I will be talking about.

Tuple: Tuple is the eact same thing as list only it has partheses and list has brackets another thing that tuple is immutable meaning (You can’t change it) or it will over write the variables.

If we want to print out all the items in the list/tuple we can use for loop with two ways as shown below.

For loop + element of the list #

>>> for f in fruit:
...     print(f)
...
banana
grape
apples
mango
>>>

For loop + index of the list #

>>> for t in range(4):
...     print(fruit[t])
...
banana
grape
apples
mango
>>>