forked from a-newman/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_for_loops.py
More file actions
150 lines (124 loc) · 4.84 KB
/
11_for_loops.py
File metadata and controls
150 lines (124 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import string
# ** For loops **
# In lesson 8 we talked about while loops. While loops kept running until their
# condition evaluated to `False`. Do you remember why this was dangerous? If
# the condition was never `True`, the while loop could keep running forever!
# For loops solve this problem!
# For loops work by "iterating over" every item in a set. This means that they
# run once for every item in a set. After that, they're done.
# Check out this example
my_list = [1, 2, 3]
for number in my_list:
print(number)
# When you run this code, the loop runs three times and then stops!
# Every time the loop is run, `number` is set equal to the next item in
# `my_list`. In technical terms, `number` is known as the "iteration variable"
# and `my_list` is an "iterator". The basic syntax of a for loop is:
"""
for iteration_variable in iterator:
do_something(iteration_variable)
"""
# For loops can loop over several different data types. Let's see some of them
# below.
print()
# ** For loops with lists **
print("For loops with lists:")
# The example above is an example of iterating over a list. Here's another:
for num in [1, 2, 3]:
print("Twice", num, "is", num*2)
print()
# You can loop over the elements and the indexes of a list at the same time
# using a function called `enumerate`:
for i, elt in enumerate(["zero", "one", "two"]):
print("The element at index", i, "is", elt)
print()
# You can also loop over a list backwards!
for num in reversed([1, 2, 3]):
print(num)
print()
# ** For loops with strings **
print("For loops with strings:")
# Interestingly, you can write a for loop over a string as well. In this case,
# the variable `letter` becomes every letter in the string.
for letter in "aeiou":
print(letter, "is a vowel")
print()
# ** For loops with ranges of numbers **
print("For loops with ranges of numbers")
# What if you just want your loop to run a fixed number of times? Python
# has an easy way of doing that too.
num_times_to_run = 3
for i in range(num_times_to_run):
print("I am running!")
print()
# Note that when looping over numbers, the variable `i` for index is commonly
# used for the iteration variable
# The Python function `range` creates a sequence of numbers. `range` can be
# used three different ways.
# 1. If you use range with one argument `end`, it creates a sequence of
# numbers starting at 0 and ending with `end - 1` (ranges, like lists,
# are 0-indexed--they start with 0, not 1!). As in the example above, this is
# really useful if you want your loop to run `end` times. print("Range with end only")
end = 10
for i in range(end):
print("i is", i)
print()
# 2. If you use range with two arguments, `start` and `end`, it
# creates a sequence of numbers starting at `start` and ending with
# `end - 1`. The loop runs `end-start` times.
print("Range with start and end")
start = 5
for i in range(start, end):
print("i is", i)
print()
# 3. If you use range with three arguments, `start`, `end`, and
# `step`, then the sequence is every number between `start` and `end-1`
# (inclusive) at intervals of `step`. `step` can be negative!
print("Range with start, end, and step")
step = 2
for i in range(start, end, step):
print("i is", i)
print()
# ** Exercises **
# 1. Write a for loop that finds the biggest number in this list that is
# divisible by 3.
# * Question: why are we looping over a list of numbers here instead of
# using range?
print("Exercise 1")
numbers = [1, 5, 6.7, 9, -1, -5, -6, -30, 30]
#Your code here: should print 30
print()
# 2. Write a for loop that prints out every letter in the alphabet and its
# numerical position, like so.
"""
a: 1
b: 2
c: 3
...
"""
# Hint: take a look at the "string constants" on this page
# https://docs.python.org/2/library/string.html#string-constants
print("Exercise 2")
# Your code here.
print()
# 3. Write a for loop using `range` that prints every odd number between 33 and
# 77, inclusive, in reverse order.
print("Exercise 3")
# Your code here
print()
# 4. A Caesar cipher, also known as a shift cipher, is a very simple encryption
# technique. Every letter in the encrypted message is replaced by the letter
# at a certain shift down the alphabet. For example, with a shift of +3,
# A->D, B->E, etc. You can read more here:
# https://en.wikipedia.org/wiki/Caesar_cipher
# Fill in the function `caesar_cipher`, which takes in a message and an
# integer value to shift and returns the encrypted message.
print("Exercise 4")
def caesar_cipher(message, shift):
pass # Your code here
message_to_encrypt = "bet you cant read this"
encrypted_message = caesar_cipher(message_to_encrypt, 3)
expected_message = "ehw brx fdqw uhdg wklv"
print("Expected message:", expected_message)
print("Your message:", encrypted_message)
print("Do they match?", encrypted_message == expected_message)