Update lists

This commit is contained in:
Joe James
2020-02-17 13:05:17 -06:00
committed by GitHub
parent 90561c7386
commit 1223c220ac

View File

@@ -1,15 +1,24 @@
# LISTS
# use square brackets to get one item of a list using index.
# note that first item has index 0.
scores = [67, 32, 29]
print(scores[2])
print() # print blank line
print(sum(scores))
print(len(scores))
print()
# USING A FOR LOOP TO ITERATE A LIST
for score in scores:
if score % 2 == 0:
print(score, ' is even.')
print()
names = ["Alice", "Tina", "Berta"]
# SORTING A LIST
@@ -18,4 +27,21 @@ print(sorted(names))
# ITERATE A LIST TO FIND NAMES THAT CONTAIN C
for name in names:
if 'c' in name:
print(name + ' contains a c')
print(name, ' contains a c')
print()
# add an item to a list
names.append('Jenny')
print(names)
# delete Tina from the list
names.remove('Tina')
print(names)
print()
# list inside a list
scores.append([100, 103])
print(scores)
print(scores[3][0])