for
loop is a way to do many operations[]
creates a list
In [2]:
odds = [1,3,5,7]
In [3]:
print('odds are:',odds)
In [4]:
print('first and last:', odds[0], odds[-1])
In [5]:
for number in odds:
print(number)
In [5]:
names = ['Newton', 'Darwing', 'Turing'] #typo in Darwins name
print('names is orignally: ', names)
names[1] = 'Darwin'
print('final value of names:', names)
In [6]:
name = 'Bell'
name[0] = 'b'
In [7]:
#nested lists
x = [['pepper', 'zucchini', 'onion'],
['cabbage', 'lettuce', 'garlic'],
['apple', 'pear', 'banana']]
visual representation of indexing nested lists
In [8]:
print([x[0]])
In [9]:
print(x[0])
In [10]:
print(x[0][1])
Many ways to change the contents of lists besides adding values
In [12]:
odds.append(11)
print('odds after adding a value: ', odds)
In [15]:
del odds[0]
print('odds after removing the first element:', odds)
In [17]:
odds.reverse()
print('odds after reversing:', odds)
In [18]:
whos
In [19]:
odds = [1,3,5,7]
primes = odds
primes += [2]
print('primes:', primes)
print('odds:', odds)
they point to same list! Why?
In [20]:
odds = [1,3,5,7]
primes = list(odds)
primes += [2]
print('primes:', primes)
print('odds:', odds)
In [1]:
my_list = []
for char in "hello":
my_list.append(char)
print(my_list)
In [ ]: