Rename lists to lists.py
This commit is contained in:
54
python/lists.py
Normal file
54
python/lists.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# LISTS
|
||||
|
||||
# use square brackets to get one item of a list using index.
|
||||
# note that first item has index 0.
|
||||
# to get that first item, 67, scores[0]
|
||||
scores = [67, 32, 29]
|
||||
print(scores[2])
|
||||
|
||||
print() # print blank line
|
||||
|
||||
# sum is a function built into python lists.
|
||||
# if all items in the list are numeric it will add them up.
|
||||
# len gives you the number of items in the list, here 3 items.
|
||||
print(sum(scores))
|
||||
print(len(scores))
|
||||
|
||||
print()
|
||||
|
||||
# USING A FOR LOOP TO ITERATE A LIST
|
||||
# our variable for the list is scores
|
||||
# we use score as a variable for list iteration.
|
||||
# each time through the loop, score takes on the next value in the list
|
||||
for score in scores:
|
||||
if score % 2 == 0: # % is called mod, and gives you the remainder of a division
|
||||
print(score, ' is even.')
|
||||
|
||||
print()
|
||||
|
||||
names = ["Alice", "Tina", "Berta"]
|
||||
|
||||
# SORTING A LIST
|
||||
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()
|
||||
|
||||
# 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])
|
||||
Reference in New Issue
Block a user