*Ha Khanh Nguyen (hknguyen)*

- 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.

In [ ]:

```
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. - What's about 100 numbers?
- 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!

`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:

In [ ]:

```
for value in list:
# do something with value
```

- Example:

In [1]:

```
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:

In [2]:

```
nums = [5, 2, 9, 14, 3]
sum = 0
for i in nums:
if i % 2 == 0:
sum = sum + i
sum
```

Out[2]:

16

- See video for the step by step walkthrough of the code segment above.

`for`

loop for the smallest even problem¶- Let's say 10 numbers are given to us in a list named
`nums`

.

In [ ]:

```
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.

`for`

loop with strings¶`for`

loop works very well with string!

In [3]:

```
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`

.

- You're a given a string

In [ ]:

```
# method 1
temp = ''
for c in s:
if c == '+':
num1 = float(temp)
temp = ''
else:
temp = temp + c
num2 = float(temp)
result = num1 + num2
```

- The
`split()`

function in Python splits a string by a specified delimiter.

In [4]:

```
'hello'.split('e')
```

Out[4]:

['h', 'llo']

- We can split
`s`

by`+`

!

In [ ]:

```
# method 2
nums = s.split('+')
result = float(nums[0]) + float(nums[1])
```

`range()`

function¶- The
`range()`

function in Python returns an iterator that yields a sequence of evenly spaced integers. - What does this mean?

In [5]:

```
range(10)
```

Out[5]:

range(0, 10)

In [6]:

```
list(range(10))
```

Out[6]:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

- The syntax of the
`range()`

function is:`range(start, end, step)`

In [7]:

```
list(range(0, 20, 2))
```

Out[7]:

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

In [8]:

```
list(range(5, 0, -1))
```

Out[8]:

[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:

In [9]:

```
nums = [5, 2, 9, 14, 3]
total = 0
for i in range(len(nums)):
if nums[i] % 2 == 0:
total = total + nums[i]
total
```

Out[9]:

16

In [10]:

```
total2 = 0
for i in nums:
if i % 2 == 0:
total2 = total2 + i
total2
```

Out[10]:

16

`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`

.

In [11]:

```
nums = [1, 14, 3, 7, 32, 19, 4]
i = 0
total = 0
while total < 20:
total = total + nums[i]
i = i + 1
total
```

Out[11]:

25

- The use of the
`break`

keyword is extremely useful in cases like this:

In [12]:

```
nums = [1, 1, 3, 2, 2, 1, 4]
i = 0
total = 0
while total < 20:
total = total + nums[i]
i = i + 1
```

- To fix this error:

In [13]:

```
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
```

Out[13]:

14