For Loop Challenges

Write a for loop so that every item in the list is printed.¶

In [16]:
lst=["koala", "cat", "fox", "panda", "chipmunk", "sloth", "penguin", "dolphin"]
for x in lst:
    print(x)
koala
cat
fox
panda
chipmunk
sloth
penguin
dolphin

Write a for loop which print "Hello!, " plus each name in the list. i.e.: "Hello!, Sam"¶

In [12]:
lst=["Sam", "Lisa", "Micha", "Dave", "Wyatt", "Emma", "Sage"]
for x in lst:
    print(x)
Sam
Lisa
Micha
Dave
Wyatt
Emma
Sage

Write a for loop that iterates through a string and prints every letter.¶

In [2]:
str="Antarctica"
for x in str:
    print(x)
A
n
t
a
r
c
t
i
c
a

Type somecode inside the for loop so that counter variable named c is increased by one each time loop iterates. Can you guess how many times it will add 1?.¶

In [14]:
str="Civilization"

c=0
for i in str:
    c=c+1
print(c)
12

Using a for loop and .append() method append each item with a Dr. prefix to the lst.¶

Info on the .append() method can be found at https://www.w3schools.com/python/ref_list_append.asp¶

In [33]:
lst1=["Phil", "Oz", "Seuss", "Dre"]
lst2=["Dr."]
for x in lst1:
    for y in lst2:
        print(y, x)
Dr. Phil
Dr. Oz
Dr. Seuss
Dr. Dre

Write a for loop which appends the square of each number to the new list.

In [22]:
lst1=[3, 7, 6, 8, 9, 11, 15, 25]
lst2=[9, 49, 36, 64, 81, 121, 225, 625]
lst1.append(lst2)
print(lst1)
[3, 7, 6, 8, 9, 11, 15, 25, [9, 49, 36, 64, 81, 121, 225, 625]]

Write a for loop using an if statement, that appends each number to the new list if it's positive.¶

In [19]:
lst1=[111, 32, -9, -45, -17, 9, 85, -10]
lst2=[111, 32, 9, 45, 17, 9, 85, 10]
for x in lst2:
    print(x)
    
111
32
9
45
17
9
85
10

Write a for loop which appends the type of each element in the first list to the second list.¶

In [27]:
lst1=[3.14, 66, "Teddy Bear", True, [], {}]
lst2=[3.14, 66, "Teddy Bear", True, [], {}]
for x in lst1:
    print(x)
for y in lst2:
    print(y)
3.14
66
Teddy Bear
True
[]
{}
3.14
66
Teddy Bear
True
[]
{}