Most of our variables have one value in them - when we put a new value in the variable, the old value is overwritten
>>> x = 2
>>> x = 4
>>> print(x)
4
friends = ['Joseph', 'Glenn', 'Sally']
carryon = ['socks', 'shirt', 'perfume']
>>> print([1, 24, 76])
[1, 24, 76]
>>> print(['red', 'yellow', 'blue'])
['red', 'yellow', 'blue']
>>> print(['red', 24, 98.6])
['red', 24, 98.6]
>>> print([1, [5, 6], 7])
[1, [5, 6], 7]
>>> print([])
[]
for i in [5, 4, 3, 2, 1]:
print(i)
print('Blastoff!')
>>>5
4
3
2
1
Blastoff!
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print('Happy New Year:', friend)
print('Done!')
>>> Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
Done!
Just like strings, we can get at any single element in a list using an index specified in square brackets.
>>> fruit = 'Banana'
>>> fruit[0] = 'b'
Traceback
TypeError: 'str' object does not support item assignment
>>> x = fruit.lower()
>>> print(x)
banana
>>> lotto = [2, 14, 26, 41, 63]
>>> print(lotto)
[2, 14, 26, 41, 63]
>>> lotto[2] = 28
>>> print(lotto)
[2, 14, 28, 41, 63]
>>> greet = 'Hello Bob'
>>> print(len(greet))
9
>>> x = [1, 2, 'joe', 99]
>>> print(len(x))
4
>>> print(range(4))
[0, 1, 2, 3]
>>> friends = ['Joseph', 'Glenn', 'Sally']
>>> print(len(friends))
3
>>> print(range(len(firends))
[0, 1, 2]
frineds = ['Joseph', 'Glenn', 'Sally']
for friend in friends:
print('Happy New Year:', friend)
for i in range(len(friends)):
friend = friends[i]
print('Happy New Year:', friend)
>>> Happy New Year: Joseph
Happy New Year: Glenn
Happy New Year: Sally
>>> frineds = ['Joseph', 'Glenn', 'Sally']
>>> print(len(frineds))
3
>>> print(range(len(friends)))
[0, 1, 2]