For Loops with Lists Practice Checklist

You will need to know how to iterate through a string or list using a for loop before you can complete these practice tasks.

for loops with lists notes

1A. Print the list of rocks on separate lines using a for loop. Use range() and len().
Starter Code:

rocks = ['igneous', 'metamorphic', 'sedimentary']

1B. Print the same list of rocks on a separate line using a for each loop. This time, do not use range() function; instead use a for each loop. Your code will be shorter.

Output for 1A and 1B:

igneous
metamorphic
sedimentary

2a. Loop through these numbers. Print only the numbers that are greater than 5.

nums = [3, 9, 8, 12, 2, 4, 8, 4, 5, 7]

2b. Instead of printing the numbers, store each number in the other list, big_values. Print big_values one time at the end of the program.

Starter Code:

values = [3, 7, 9, 12, 2, 5, 1, 3, 8, 6]
big_values = [] #creates a blank new list
#create a for loop here

Output:

7
9
12
8
6

3. Print this list of foods, but every time the item is “taco” also print “WE HAVE A WINNER!”

foods = ['fries','yogurt','taco','pizza','taco','eggs']

Output:

fries
yogurt
taco
WE HAVE A WINNER!
pizza
taco
WE HAVE A WINNER!
eggs

4. Create a new string out of the starting string, but remove the letter “i.” You can loop through a string in the same way you loop through a list. Create a blank string to start the program, then inside the loop, add one character at a time to build up the new string. Include if/else code to decide if each letter should be included.

word = "Mississippi"
new_word = ""  #creates a blank string
for letter in word:
    #if / else code to add letters to new_word
print(new_word)

Output (also try changing the word to see if it still works properly):

Msssspp

5. In the following list of percentage grades, the maximum value should be 100. Fix the list so that every value greater than 100 becomes exactly 100. Create a blank list before the loop. Then, inside the loop, either add the original grade to the new list, or add 100 instead of an invalid grade. Print the list after the loop completes.

Starter Code:

grades = [125, 80, 200, 95, 75]
new_grades = []
for i
#add code
print(new_grades)

Output:

[100, 80, 100, 95, 75]

6. Ask the user for a new password. Check if the password contains both a number (has a character in “0123456789”) and a symbol (has a character in “!@#$%^&*()”)

Hints: before your loop, create a couple of boolean variables to store whether a number has been found and whether a symbol has been found. Inside the loop, check each character to see if it is in the list of acceptable characters. If one of those is found, set the appropriate boolean variable equal to True.

Starter Code:

pw = input('Enter a new password: ')
has_number = False
#add a line of code here
for letter in pw:

7. Ask the user to type some text. Print the text backwards.

Hint 1: You can use subtraction, or negative index numbers, or a negative increment in range() to work from the end of a string to the beginning.
Hint 2: you can add or subtract one if your index endpoints are off by one. This will be necessary when you go backwards through a range of numbers.
Hint 3: print your index variable inside the loop as a troubleshooting technique.

Example Output:

Type some stuff:  I am fancy
ycnaf ma I

8. Ask the user for some text. Change every “s” to “$” and change every “a” to “@” and print the new version of their text.

For example, “space special” would become “$p@ce $peci@al”

NOTE: The remaining sections are more challenging. You may want to move onto nested for loops from here and come back later if you have time.

9. Ask the user to type a sentence. Scramble their sentence by printing every other character (even numbered indexes) followed by the remaining characters (odd numbered indexes).

Interlude: chr() and ord() functions and ciphers
(this will be used in the remaining tasks)

Try each code snippet separately and see what happens:

x = ord('m')
print(x)
c = chr(120)
print(c)
letters = 'abcd'
for c in letters:
    print(ord(c))
numbers = [101,102,103,104,105]
for n in numbers:
    print(chr(n))

Hopefully you noticed that ord() converts a single character of text to an integer number, while chr() converts an integer number to a single character of text.

10) Using a loop from 97 to 122, print a list of every lower case letter and its corresponding number. Use the ord() and chr() functions to keep your program to a maximum of 4 lines. (It can be done in 2 lines)

Output:

a 97
b 98
c 99
d 100
e 101
f 102
g 103
h 104
i 105
j 106
k 107
l 108
m 109
n 110
o 111
p 112
q 113
r 114
s 115
t 116
u 117
v 118
w 119
x 120
y 121
z 122

11) For each letter in the word below, replace it with the next letter in the alphabet. Use a for loop with chr() and ord(). The scrambled word is called a cipher. Convert each letter to a number, add one, then convert back to a letter and add that letter to the cipher. Print the cipher when finished.

Starter Code:

word = 'asparagus'
cipher = ''

Output:

btqbsbhvt

12) Ask the user for a word, then ask for a number of spaces to move in the alphabet. Increase each letter in their word by the appropriate number of letters in the alphabet.

Starter Code:

word = input('Type a word: ')
n = input('How many letters to shift?')
n = int(n)  #convert str to int

13) If you finish #11 and test it for a while, you will notice that funny things happen when you add to letters at the end of the alphabet. It outputs punctuation or symbols instead of letters.

What we really want it to do is add until it gets to ‘z’ (122)at the end of the alphabet, and then start over with ‘a’ (97) if it runs out of letters.

This can be done using using “mod 26” numbering. To convert a number to mod 26 numbering (which includes only numbers from 0 to 25), you can do “x%26” where x is any integer. Since the alphabet starts at character #97, you will have to add or subtract 97 at different times when using this trick.

Your challenge for #13: update the scrambling cipher code to handle words with late alphabet letters like “x” and “z.”

Example Output:

Type a word:  pizza
How many letters to shift? 3
slccd

Type a word:  rhythm
How many letters to shift? 7
yofaot