Day 50: Iteration Examples

# Iteration examples

# get the average length of items in the list
list = ["Sara", "Christiano", "Dorothy", "Pete", "Peter"]
list.append("Robert")
average = 0

for name in list:
    average += len(name)                 # adds the length of every item to average

average_length = average / len(list)
print("The average length is: " + str(average_length))      #   ...length is: 6.0

# find an item in list
if "Sara" in list:
    print("found Sara")                 # found Sara
else:
    print("Not found")

if "Tucker" in list:
    print("found Tucker")
else:
    print("Not found")                  # Not found

# iterating with while loops

n = 10
numb = []
while n > 0:
    numb.append(n)
    n -= 1

print(numb)                     # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.