1 of 41

More with Loops

Part 2: For Loops

1

2 of 41

Recall from Last Lecture: While Loops

Last lecture, we worked with while loops to access every element in a list. This is also called “iteration.” Example:

2

�names = ['Larry', 'Curly', 'Moe', 'Seamus']

counter = 0

while counter < len(names):

print(names[counter])

counter += 1

3 of 41

For Loops

3

for item in sequence:

# execute code block

Pseudocode

4 of 41

For loops are a simpler way to access every element in a list

  • A simpler syntax for iterating through every item in a sequence
  • Uses for item in sequence syntax, where “item” is a variable that holds the next element in the sequence.
  • “Keep retrieving the next item in the sequence, starting from the beginning, until you reach the end”

4

�names = ['Larry', 'Curly', 'Moe', 'Seamus']

for name in names:

print(name)

5 of 41

for thing in my_sequence:

print(thing)

5

Keyword for marks the start of for loop

A name to uniquely reference each item in the list

Any sequence �(tuple, list, set, string)

loop body has 1 or more statements, which have same indentation level (usually 4 spaces)

For Loop: “Keep retrieving the next item in the sequence, starting from the beginning, until you reach the end”

6 of 41

While Loops v. For Loops: Which is Better?

6

# while loop:

counter = 0

while counter < len(names):

print(names[counter])

counter += 1

# for loop:

for name in names:

print(name)

7 of 41

...It depends...

  • If you need to iterate through a sequence (list, string, set, tuple, etc), the for loop is a clear winner because of its simplicity.
    • Iterate through a sequence → for loop�
  • If you need to run something forever, or for some conditional length of time, use a while loop, which is more flexible
    • Do something for some conditional length of time → while loop

7

8 of 41

Break & Continue Statements

Exiting early or skipping iterations

8

9 of 41

Break Statement

If you want to break out of a loop early, use the break statement. For instance, the code below breaks out of the loop when it reaches the note 67.

numbers = [76, 76, 76, 72, 76, 79, 67, 72, 67, 64, 69, 71]

for num in numbers:

if num == 67:

break

print(num)

9

05_break.py

10 of 41

Continue Statement

If you want to skip over a line of code in your loop, use the continue statement. For instance, the code below skips printing the notes 76 and 67.

numbers = [76, 76, 76, 72, 76, 79, 67, 72, 67, 64, 69, 71]

for num in numbers:

if num == 76 or num == 67:

continue

print(num)

10

06_continue.py

11 of 41

Range Function

Range functions help you to conveniently generate a list of numbers. This details of the range function are elaborated here.

range(10)

Generates a sequence from 0 to 9 (excludes last number)

range(5, 10)

Generates a sequence from 5 to 10 (excludes last number)

range(2, 30, 3)�Generates a sequence from 2 to 30, incrementing by 3 each time

11

04_for_loop_apply_range_function.py

12 of 41

PRACTICE TIME

SOLVING PROBLEMS WITH LOOPS

12

07_find_largest_number.py

13 of 41

CHALLENGE 1

WHAT’S THE LARGEST NUMBER?

13

3767, 8373, 5940, 9468, 2758, 9258, 3966, 1885, 4104, 2742, 5300, 9797, 7685, 8823, 7117, 2212, 6725, 8995, 2438, 8194, 2576, 4408, 438, 7688, 7967, 6143, 9514, 7950, 4729, 4840, 5668, 7659, 866, 763, 357, 1316, 8251, 4859, 4816, 4002, 6094, 9990, 7957, 8962, 8624, 1073, 5162, 2886, 8477, 1182, 3474, 3157, 6711, 4921, 2837, 2711, 8376, 9532, 3596, 9535, 4597, 342, 1978, 7842, 578, 647, 3354, 4695, 8844, 2188, 5103, 6599, 8973, 654, 7415, 5153, 8268, 6551, 2750, 5337, 5680, 5392, 6187, 6301, 5348, 1509, 1527, 1018, 2463, 9366, 1756, 9460, 7021, 3147, 2883, 4733, 704, 3377, 4398, 3408, 550, 6173, 3522, 5103, 6948, 5835, 8366, 5497, 6162, 8596, 7087, 9726, 7857, 9865, 7102, 8100, 8580, 6887, 1083, 7908, 5671, 3945, 6951, 6966, 3974, 2325, 9320, 4797, 9400, 800, 8976, 5302, 6577, 3743, 5891, 8817, 6738, 6404, 8759, 7476, 8818, 3597, 3933, 1226, 7728, 3858, 9518, 1869, 134, 4947, 6446, 3493, 8991, 2329, 500, 7550, 3516, 9892, 5244, 6231, 9960, 1701, 6111, 4616, 6920, 4647, 7627, 6763, 98, 7778, 4870, 2577, 8383, 8, 7798, 8465, 2054, 8810, 3865, 1717, 8792, 7228, 1672, 2657, 6231, 9927, 1402, 8428, 1136, 8325, 2593, 5919, 4582, 5052, 8621, 2149, 7607, 766, 4247, 4904, 3978, 8289, 1100, 515, 2504, 8166, 7445, 9366, 8752, 4557, 9825, 1625, 3566, 6101, 6357, 5623, 6518, 7836, 49, 9426, 3936, 490, 3890, 9002, 5919, 311, 5070, 8618, 754, 6347, 5093, 8087, 903, 3696, 8723, 6682, 5910, 433, 3661, 8981, 9320, 3909, 555, 4570, 5809, 6652, 694, 1281, 8301, 8096, 6368, 2473, 703, 2797, 6486, 2240, 1198, 9571, 6009, 5467, 4761, 6856, 8708, 5400, 5685, 6911, 3892, 4573, 9062, 6553, 9065, 8821, 8223, 100, 4374, 9743, 2675, 9841, 3547, 9678, 1291, 6210, 2948, 3515, 8142, 2691, 4368, 6156, 7549, 4303, 979, 8684, 8169, 9191, 6056, 6025, 3444, 6329, 1275, 6285, 7146, 4750, 9004, 4371, 8946, 1540, 1204, 7582, 3248, 8001, 2310, 8863, 1309, 8158, 7861, 8816, 695, 2300, 5073, 7535, 3952, 249, 327, 5919, 5408, 9292, 865, 1770, 2609, 6318, 9139, 8980, 7053, 4317, 8198, 5116, 7324, 8455, 2007, 4506, 4961, 2787, 144, 2253, 9254, 8610, 8214, 2789, 9819, 5198, 3812, 163, 9914, 2515, 974, 9555, 7869, 5395, 7776, 1640, 2363, 9808, 6015, 7981, 3310, 1087, 276, 6807, 7257, 3038, 401, 8622, 8598, 1377, 7635, 987, 1641, 1898, 3611, 9158, 2534, 435, 1808, 4414, 3593, 6419, 3188, 5527, 2892, 8258, 1135, 570, 7755, 7362, 479, 559, 7462, 204, 9471, 9391, 8304, 7846, 1955, 3311, 8213, 2399, 5163, 2368, 5928, 6847, 9756, 3083, 6957, 3022, 1826, 3650, 9843, 3385, 3894, 3191, 95, 894, 2287, 8419, 5949, 5283, 2230, 6309, 3037, 3134, 8520, 318, 4912, 1291, 956, 5024, 2355, 237, 1342, 8377, 3698, 4718, 3434, 2241, 4966, 8717, 3719, 4591, 4819, 7895, 8652, 2400, 8668, 9704, 8478, 8348, 764, 9932, 2423, 7346, 7610, 1762, 5848, 2274, 1208, 7720, 9493, 7237, 8898, 5277, 4994, 9173, 2190, 5089, 2182, 6496, 2889, 1341, 6634, 897, 5922, 783, 1259, 1697, 47, 3973, 6837, 7011, 2266, 1818, 9166, 1325, 8624, 2940, 5820, 4958, 8330, 1012, 4055, 6184, 546, 9283, 6106, 6241, 3846, 5750, 3754, 2110, 4685, 1811, 4731, 6234, 8990, 7965, 5799, 9275, 5116, 60, 6913, 1521, 1952, 9385, 3147, 4466, 4231, 2495, 4426, 9095, 9832, 4710, 977, 6742, 7723, 5671, 7266, 1592, 5909, 9743, 6907, 1670, 6914, 9261, 665, 3447, 8571, 138, 5509, 6274, 9492, 104, 6246, 5618, 1755, 5118, 7281, 571, 3648, 3154, 1302, 1436, 6765, 4760, 8161, 3996, 4452, 6818, 3151, 6502, 577, 7114, 4612, 4204, 6753, 9513, 419, 3409, 5855, 8836, 5907, 8488, 8933, 3710, 3031, 4995, 7647, 2874, 8848, 1188, 6135, 6533, 4473, 8075, 621, 820, 8689, 9359, 6131, 744, 6345, 232, 9953, 4462, 6846, 2575, 6794, 7695, 604, 1235, 5950, 7811, 2812, 4672, 9253, 8688, 7557, 9216, 1261, 5336, 3870, 6861, 540, 5147, 6729, 2982, 3764, 9305, 7375, 520, 3352, 1412, 1163, 2966, 1325, 7765, 9929, 6755, 3016, 1743, 351, 2134, 215, 1540, 7464, 2110, 8442, 3052, 3410, 8422, 1196, 8741, 2344, 118, 5822, 465, 1831, 7713, 6217, 3590, 6052, 9477, 5197, 6793, 2140, 9053, 1795, 5266, 5748, 3112, 5936, 4090, 5385, 1517, 2392, 6226, 2065, 3362, 853, 2639, 3686, 5073, 1561, 7804, 2888, 9603, 7406, 1780, 8111, 564, 4308, 2792, 3032, 4568, 4666, 2949, 4546, 3787, 3965, 6532, 5840, 4976, 7377, 2314, 215, 5046, 7287, 7922, 7418, 3196, 1823, 3470, 6447, 4126, 3408, 725, 6947, 1764, 3055, 8676, 1132, 2135, 9120, 6341, 7767, 5373, 1102, 9264, 7010, 3800, 9358, 7993, 902, 8299, 3136, 33, 1003, 2376, 279, 4741, 9313, 1006, 6646, 570, 2127, 1271, 5368, 6543, 350, 646, 2609, 3091, 9211, 7938, 8341, 5300, 7309, 2407, 8384, 5657, 4880, 5657, 7283, 205, 4855, 6379, 4775, 8743, 8061, 5936, 4278, 3704, 2357, 9211, 1227, 9566, 2357, 9696, 8167, 6784, 5595, 1921, 2160, 3846, 7477, 8335, 7494, 5974, 8223, 6847, 9131, 3154, 646, 5324, 2767, 4828, 1376, 3320, 1263, 3052, 1982, 1649, 6944, 2693, 9395, 611, 5528, 9107, 5980, 742, 881, 330, 3713, 6327, 9507, 9091, 7683, 2903, 9751, 4525, 4610, 7414, 5088, 6066, 586, 5314, 4284, 1319, 6445, 8474, 442, 2596, 1545, 1187, 3065, 5505, 2356, 1775, 9064, 5374, 6436, 9218, 2134, 4827, 7220, 681, 4123, 5492, 5093, 41, 8030, 7504, 3697, 8974, 46, 3587, 7041, 2587, 6930, 7924, 635, 899, 2810, 2489, 9639, 2653, 2633, 6488, 328, 5089, 7533, 4954, 3459, 8657, 4026, 6895, 7140, 9389, 4389, 989, 6690, 2036, 6985, 2474, 3761, 3968, 5055, 5912, 8832, 6541, 5233, 9175, 858, 8430, 9106, 9614, 177, 874, 957, 6498, 1164, 4318, 5899, 8616, 9994, 6194, 3258, 693, 5972, 5410, 5696, 3779, 3183, 8658, 3545, 7696, 7026, 3020, 3962, 5932, 6970, 6068, 4321, 8778, 4698, 1355, 3597, 3796, 1403, 1525, 8200, 8211, 2010, 1227, 5949, 589, 9042, 6393, 8678, 6554, 4235, 7071, 7178, 1737, 8978, 7847, 4789, 5502, 1684, 6178

14 of 41

For Loops: Challenge

14

[65, 1800, 12, 20, 1963, 5000, 260, 0, 40, 953, 775, 67, 33, 1688, 119]

Which number is biggest?

Credit: activity designed by Chuck Severance

15 of 41

15

BEGIN

16 of 41

Loop: Challenge

16

65

How can a computer scan a list of numbers and figure out the biggest number?

17 of 41

Loop: Challenge

17

1800

How can a computer scan a list of numbers and figure out the biggest number?

18 of 41

Loop: Challenge

18

12

How can a computer scan a list of numbers and figure out the biggest number?

19 of 41

Loop: Challenge

19

20

How can a computer scan a list of numbers and figure out the biggest number?

20 of 41

Loop: Challenge

20

1963

How can a computer scan a list of numbers and figure out the biggest number?

21 of 41

Loop: Challenge

21

5000

How can a computer scan a list of numbers and figure out the biggest number?

22 of 41

Loop: Challenge

22

260

How can a computer scan a list of numbers and figure out the biggest number?

23 of 41

Loop: Challenge

23

0

How can a computer scan a list of numbers and figure out the biggest number?

24 of 41

24

END

25 of 41

25

BEGIN

26 of 41

Loop: Challenge

26

65

How can a computer scan a list of numbers and figure out the biggest number?

27 of 41

Loop: Challenge

27

1800

How can a computer scan a list of numbers and figure out the biggest number?

28 of 41

Loop: Challenge

28

12

How can a computer scan a list of numbers and figure out the biggest number?

29 of 41

Loop: Challenge

29

20

How can a computer scan a list of numbers and figure out the biggest number?

30 of 41

Loop: Challenge

30

1963

How can a computer scan a list of numbers and figure out the biggest number?

31 of 41

Loop: Challenge

31

5000

How can a computer scan a list of numbers and figure out the biggest number?

32 of 41

Loop: Challenge

32

260

How can a computer scan a list of numbers and figure out the biggest number?

33 of 41

Loop: Challenge

33

0

How can a computer scan a list of numbers and figure out the biggest number?

34 of 41

34

END

35 of 41

How did you figure it out?

35

07_find_largest_number.py

36 of 41

Three Questions

  1. What do you want to repeat?�??�
  2. How long to you want to repeat it?�??
  3. What do you want to change each time?�??

36

07_find_largest_number.py

37 of 41

Loop Challenge: Find the Largest Number

37

numbers = [65, 1800, 12, 20, 1963, 5000, 260, 0, 40, 953, 775, 67, 33]

biggest_number_yet = 0

for num in numbers:

if biggest_number_yet < num:

biggest_number_yet = num

print('The biggest number in the list is: ', biggest_number_yet)

07_find_largest_number.py

38 of 41

Will work with millions of numbers

38

3767, 8373, 5940, 9468, 2758, 9258, 3966, 1885, 4104, 2742, 5300, 9797, 7685, 8823, 7117, 2212, 6725, 8995, 2438, 8194, 2576, 4408, 438, 7688, 7967, 6143, 9514, 7950, 4729, 4840, 5668, 7659, 866, 763, 357, 1316, 8251, 4859, 4816, 4002, 6094, 9990, 7957, 8962, 8624, 1073, 5162, 2886, 8477, 1182, 3474, 3157, 6711, 4921, 2837, 2711, 8376, 9532, 3596, 9535, 4597, 342, 1978, 7842, 578, 647, 3354, 4695, 8844, 2188, 5103, 6599, 8973, 654, 7415, 5153, 8268, 6551, 2750, 5337, 5680, 5392, 6187, 6301, 5348, 1509, 1527, 1018, 2463, 9366, 1756, 9460, 7021, 3147, 2883, 4733, 704, 3377, 4398, 3408, 550, 6173, 3522, 5103, 6948, 5835, 8366, 5497, 6162, 8596, 7087, 9726, 7857, 9865, 7102, 8100, 8580, 6887, 1083, 7908, 5671, 3945, 6951, 6966, 3974, 2325, 9320, 4797, 9400, 800, 8976, 5302, 6577, 3743, 5891, 8817, 6738, 6404, 8759, 7476, 8818, 3597, 3933, 1226, 7728, 3858, 9518, 1869, 134, 4947, 6446, 3493, 8991, 2329, 500, 7550, 3516, 9892, 5244, 6231, 9960, 1701, 6111, 4616, 6920, 4647, 7627, 6763, 98, 7778, 4870, 2577, 8383, 8, 7798, 8465, 2054, 8810, 3865, 1717, 8792, 7228, 1672, 2657, 6231, 9927, 1402, 8428, 1136, 8325, 2593, 5919, 4582, 5052, 8621, 2149, 7607, 766, 4247, 4904, 3978, 8289, 1100, 515, 2504, 8166, 7445, 9366, 8752, 4557, 9825, 1625, 3566, 6101, 6357, 5623, 6518, 7836, 49, 9426, 3936, 490, 3890, 9002, 5919, 311, 5070, 8618, 754, 6347, 5093, 8087, 903, 3696, 8723, 6682, 5910, 433, 3661, 8981, 9320, 3909, 555, 4570, 5809, 6652, 694, 1281, 8301, 8096, 6368, 2473, 703, 2797, 6486, 2240, 1198, 9571, 6009, 5467, 4761, 6856, 8708, 5400, 5685, 6911, 3892, 4573, 9062, 6553, 9065, 8821, 8223, 100, 4374, 9743, 2675, 9841, 3547, 9678, 1291, 6210, 2948, 3515, 8142, 2691, 4368, 6156, 7549, 4303, 979, 8684, 8169, 9191, 6056, 6025, 3444, 6329, 1275, 6285, 7146, 4750, 9004, 4371, 8946, 1540, 1204, 7582, 3248, 8001, 2310, 8863, 1309, 8158, 7861, 8816, 695, 2300, 5073, 7535, 3952, 249, 327, 5919, 5408, 9292, 865, 1770, 2609, 6318, 9139, 8980, 7053, 4317, 8198, 5116, 7324, 8455, 2007, 4506, 4961, 2787, 144, 2253, 9254, 8610, 8214, 2789, 9819, 5198, 3812, 163, 9914, 2515, 974, 9555, 7869, 5395, 7776, 1640, 2363, 9808, 6015, 7981, 3310, 1087, 276, 6807, 7257, 3038, 401, 8622, 8598, 1377, 7635, 987, 1641, 1898, 3611, 9158, 2534, 435, 1808, 4414, 3593, 6419, 3188, 5527, 2892, 8258, 1135, 570, 7755, 7362, 479, 559, 7462, 204, 9471, 9391, 8304, 7846, 1955, 3311, 8213, 2399, 5163, 2368, 5928, 6847, 9756, 3083, 6957, 3022, 1826, 3650, 9843, 3385, 3894, 3191, 95, 894, 2287, 8419, 5949, 5283, 2230, 6309, 3037, 3134, 8520, 318, 4912, 1291, 956, 5024, 2355, 237, 1342, 8377, 3698, 4718, 3434, 2241, 4966, 8717, 3719, 4591, 4819, 7895, 8652, 2400, 8668, 9704, 8478, 8348, 764, 9932, 2423, 7346, 7610, 1762, 5848, 2274, 1208, 7720, 9493, 7237, 8898, 5277, 4994, 9173, 2190, 5089, 2182, 6496, 2889, 1341, 6634, 897, 5922, 783, 1259, 1697, 47, 3973, 6837, 7011, 2266, 1818, 9166, 1325, 8624, 2940, 5820, 4958, 8330, 1012, 4055, 6184, 546, 9283, 6106, 6241, 3846, 5750, 3754, 2110, 4685, 1811, 4731, 6234, 8990, 7965, 5799, 9275, 5116, 60, 6913, 1521, 1952, 9385, 3147, 4466, 4231, 2495, 4426, 9095, 9832, 4710, 977, 6742, 7723, 5671, 7266, 1592, 5909, 9743, 6907, 1670, 6914, 9261, 665, 3447, 8571, 138, 5509, 6274, 9492, 104, 6246, 5618, 1755, 5118, 7281, 571, 3648, 3154, 1302, 1436, 6765, 4760, 8161, 3996, 4452, 6818, 3151, 6502, 577, 7114, 4612, 4204, 6753, 9513, 419, 3409, 5855, 8836, 5907, 8488, 8933, 3710, 3031, 4995, 7647, 2874, 8848, 1188, 6135, 6533, 4473, 8075, 621, 820, 8689, 9359, 6131, 744, 6345, 232, 9953, 4462, 6846, 2575, 6794, 7695, 604, 1235, 5950, 7811, 2812, 4672, 9253, 8688, 7557, 9216, 1261, 5336, 3870, 6861, 540, 5147, 6729, 2982, 3764, 9305, 7375, 520, 3352, 1412, 1163, 2966, 1325, 7765, 9929, 6755, 3016, 1743, 351, 2134, 215, 1540, 7464, 2110, 8442, 3052, 3410, 8422, 1196, 8741, 2344, 118, 5822, 465, 1831, 7713, 6217, 3590, 6052, 9477, 5197, 6793, 2140, 9053, 1795, 5266, 5748, 3112, 5936, 4090, 5385, 1517, 2392, 6226, 2065, 3362, 853, 2639, 3686, 5073, 1561, 7804, 2888, 9603, 7406, 1780, 8111, 564, 4308, 2792, 3032, 4568, 4666, 2949, 4546, 3787, 3965, 6532, 5840, 4976, 7377, 2314, 215, 5046, 7287, 7922, 7418, 3196, 1823, 3470, 6447, 4126, 3408, 725, 6947, 1764, 3055, 8676, 1132, 2135, 9120, 6341, 7767, 5373, 1102, 9264, 7010, 3800, 9358, 7993, 902, 8299, 3136, 33, 1003, 2376, 279, 4741, 9313, 1006, 6646, 570, 2127, 1271, 5368, 6543, 350, 646, 2609, 3091, 9211, 7938, 8341, 5300, 7309, 2407, 8384, 5657, 4880, 5657, 7283, 205, 4855, 6379, 4775, 8743, 8061, 5936, 4278, 3704, 2357, 9211, 1227, 9566, 2357, 9696, 8167, 6784, 5595, 1921, 2160, 3846, 7477, 8335, 7494, 5974, 8223, 6847, 9131, 3154, 646, 5324, 2767, 4828, 1376, 3320, 1263, 3052, 1982, 1649, 6944, 2693, 9395, 611, 5528, 9107, 5980, 742, 881, 330, 3713, 6327, 9507, 9091, 7683, 2903, 9751, 4525, 4610, 7414, 5088, 6066, 586, 5314, 4284, 1319, 6445, 8474, 442, 2596, 1545, 1187, 3065, 5505, 2356, 1775, 9064, 5374, 6436, 9218, 2134, 4827, 7220, 681, 4123, 5492, 5093, 41, 8030, 7504, 3697, 8974, 46, 3587, 7041, 2587, 6930, 7924, 635, 899, 2810, 2489, 9639, 2653, 2633, 6488, 328, 5089, 7533, 4954, 3459, 8657, 4026, 6895, 7140, 9389, 4389, 989, 6690, 2036, 6985, 2474, 3761, 3968, 5055, 5912, 8832, 6541, 5233, 9175, 858, 8430, 9106, 9614, 177, 874, 957, 6498, 1164, 4318, 5899, 8616, 9994, 6194, 3258, 693, 5972, 5410, 5696, 3779, 3183, 8658, 3545, 7696, 7026, 3020, 3962, 5932, 6970, 6068, 4321, 8778, 4698, 1355, 3597, 3796, 1403, 1525, 8200, 8211, 2010, 1227, 5949, 589, 9042, 6393, 8678, 6554, 4235, 7071, 7178, 1737, 8978, 7847, 4789, 5502, 1684, 6178

39 of 41

More Number Challenges: practice on your own!

39

numbers = [65, 1800, 12, 20, 1963, 5000, 260, 0, 40, 953, 775]

Given the list above, do the following:

  • Find the largest number
  • Find the smallest number
  • Find the sum of the numbers
  • Find the average of the numbers
  • Print every number in the list that is a multiple of 2 or 5
  • Print each number in the list in the reverse order

40 of 41

The Random Module

One important concept in computer science is the notion of randomness, which allows a programmer to simulate a number of different possible inputs quickly. Some applications:

  • Position a bunch of shapes randomly on a screen
  • Pick a note out of scale to simulate a jazz riff
  • Simulate a probability distribution
  • Make leaves on a tree have slightly different sizes, shapes, and angles to simulate the randomness of nature

40

41 of 41

The Random Module (Helpful for HW4)

Three of the most common functions used within the random module are:

import random # don't forget to import it!

random.randint(a, b) # picks a random integer between a and b

random.uniform(a, b) # picks a random float between a and b random.choice(['a', 'b', 'c', 'd', 'e']) # picks a random element from the list

41

10_random_simple.py