## 1. Smallest Even Problem

• You are given 3 numbers x, y, z. Find the smallest even number among them and store it in a variable called result. If none of them is even, then result = -1. Note that they are all positive numbers.
even = []
if x % 2 == 0:
even.append(x)
if y % 2 == 0:
even.append(y)
if z % 2 == 0:
even.append(z)

if even == []:
result = -1
else:
result = min(even)
• Now, let’s say instead of 3 numbers, you are given 10 numbers! Well, the logic is the same, right? But we will have to write 10 if statements, each for each number. That’s tedious but doable.
• There must be a way for us to tell Python to repeat this statement for all the numbers provided!
• That is the idea of loops in programming!

## 2. for Loops

• for loops are for iterating over a list of objects (or a collection of objects or an iterater).
• The standard syntax for a for loop is:
for value in list:
# do something with value
• Example:
nums = [5, 2, 9, 14, 3]
for i in nums:
print(i)
## 5
## 2
## 9
## 14
## 3
• Let’s do something more interesting than printing out the number:
nums = [5, 2, 9, 14, 3]
sum = 0
for i in nums:
if i % 2 == 0:
sum = sum + i
sum
## 16
• See video for the step by step walkthrough of the code segment above.

### 2.1 Use for loop for the smallest even problem

• Let’s say 10 numbers are given to us in a list named nums.
even = []
for i in nums:
if i % 2 == 0:
even.append(i)

if even == []:
result = -1
else:
result = min(even)
• Similarly, we can use the same logic above for the other variations of this problem: smallest odd, largest even, largest odd.

### 2.2 for loop with strings

• for loop works very well with string!
s = 'hello'
for c in s:
print(c)
## h
## e
## l
## l
## o
• Example:
• You’re a given a string s containing a mathematical expression with 2 operands and an addition. For example, s = '3+2.5'. There is no space between the number (operand) and the operator. Write a Python program to evaluate the expression and store the result in a variable named result.
# method 1
temp = ''
for c in s:
if c == '+':
num1 = float(temp)
temp = ''
else:
temp = temp + c
num2 = float(temp)
result = num1 + num2
'hello'.split('e')
## ['h', 'llo']
• We can split s by +!
# method 1
nums = s.split('+')
result = float(nums[0]) + float(nums[1])

### 2.4 range() function

• The range() function in Python returns an iterator that yields a sequence of evenly spaced integers.
• What does this mean?
range(10)
## range(0, 10)
list(range(10))
## [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
• The syntax of the range() function is: range(start, end, step)
list(range(0, 20, 2))
## [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
list(range(5, 0, -1))
## [5, 4, 3, 2, 1]
• Sometimes, we don’t want to iterate over a list of objects in the “default” way, instead we want to iterate the index of the list and access the elements by the index:
nums = [5, 2, 9, 14, 3]
sum = 0
for i in range(len(nums)):
if nums[i] % 2 == 0:
sum = sum + nums[i]
sum
## 16

### Exercise

• Let $$X$$ be the number of pizzas Alex orders each week. $$X$$ has the following probability distribution:
$$x$$ $$f(x)$$
0 0.05
1 0.1
2 0.2
3 0.35
4 0.2
5 0.1

Use Python to:

• Find the expected value of $$X$$.
• Find the variance and standard deviation of $$X$$.

## 3. while Loops

• A while loop specifies a condition and a block of code that is to be executed until the condition evaluates to False or the loop is explicitly ended with break.
nums = [1, 14, 3, 7, 32, 19, 4]
i = 0
total = 0
while total < 20:
total = total + nums[i]
i = i + 1
total
## 25
• The use of the break keyword is extremely useful in cases like this:
nums = [1, 1, 3, 2, 2, 1, 4]
i = 0
total = 0
while total < 20:
total = total + nums[i]
i = i + 1
## Error in py_call_impl(callable, dots$args, dots$keywords): IndexError: list index out of range
##
## Detailed traceback:
##   File "<string>", line 2, in <module>
• To fix this error:
nums = [1, 1, 3, 2, 2, 1, 4]
i = 0
total = 0
while total < 20:
if i == len(nums):
break
total = total + nums[i]
i = i + 1
total
## 14