1. Smallest Even Problem

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)

2. for Loops

for value in list:
  # do something with value
nums = [5, 2, 9, 14, 3]
for i in nums:
  print(i)
## 5
## 2
## 9
## 14
## 3
nums = [5, 2, 9, 14, 3]
sum = 0
for i in nums:
  if i % 2 == 0:
    sum = sum + i
sum
## 16

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

nums = [1, 14, 3, 7, 32, 19, 4]
i = 0
total = 0
while total < 20:
  total = total + nums[i]
  i = i + 1
total
## 25
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>
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