Skip to content
Snippets Groups Projects
Commit bcb55ed0 authored by msyamkumar's avatar msyamkumar
Browse files

Lec 10 notebooks

parent 2af525b6
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:dd1c3a1b tags:
 
# Iteration 1
 
## Readings:
- Chapter 7 of Think Python
- Chapter 6.1 to 6.3 of Python for Everybody
%% Cell type:markdown id:595d2e5b tags:
 
## Learning Objectives:
 
- Implement an iterative algorithm using a `while` loop, for
- printing / counting
- validating user input
- performing an iterative calculation
- printing character art
 
- Trace iterative algorithms and determine their output
 
- Recognize common `while` loop errors
- Infinite loops (when unintentional)
- Off-by-one mistakes in the loop control variable
 
%% Cell type:code id:c30a8ea2 tags:
 
``` python
# import statements
 
import time
import math
 
# This module only works on MAC, sorry Windows folks :(
import beeper
```
 
%% Cell type:markdown id:f113fc7b tags:
 
### Example 0: Simple countdowns
 
%% Cell type:markdown id:73959e77 tags:
 
**How to termination infinite loop in:**
- jupyter: Kernel > Interrupt (fix and then re-run)
- script mode / interactive mode: Ctrl + C (Kill signal)
 
%% Cell type:code id:cb8de263 tags:
 
``` python
#Count from 0 to 3, printing each number
count = 0
 
while count <= 3:
print(count)
count += 1
```
 
%% Output
 
0
1
2
3
 
%% Cell type:code id:23dbc9da tags:
 
``` python
#Count from 3 to -3, printing each number
count = 3
 
while count >= -3:
print(count)
count -= 1
```
 
%% Output
 
3
2
1
0
-1
-2
-3
 
%% Cell type:markdown id:115e8742 tags:
 
### Example 1: Countdown timer alarm
 
%% Cell type:code id:42f4a48f tags:
 
``` python
# TODO: use input function to get user input for number of seconds
start = int(input("How many seconds? "))
 
# TODO: copy start into another variable
remaining = start
while remaining >= 1: # TODO: iterate from start to 1
print(remaining, "seconds left")
# TODO: update loop control variable's value to make progress towards terminating the loop,
# that is turning loop condition to False
# TODO: update loop control variable's value to make progress towards terminating
# the loop, that is turning loop condition to False
remaining -= 1
# TODO: now run the cell to see the output. Didn't it go too fast?
# TODO: call time module sleep function, by passing 1 as argument
time.sleep(1)
 
# TODO: print "BEEP BEEP BEEP ..." (10 BEEPS) without typing BEEP 10 times
# What string operator can you use here?
print("BEEP " * 10)
# wake up call
beeper.beep(10) # Only works on MAC laptops, sorry Windows users :(
```
 
%% Output
 
How many seconds? 10
10 seconds left
9 seconds left
8 seconds left
7 seconds left
6 seconds left
How many seconds? 5
5 seconds left
4 seconds left
3 seconds left
2 seconds left
1 seconds left
BEEP BEEP BEEP BEEP BEEP BEEP BEEP BEEP BEEP BEEP
 
%% Cell type:markdown id:062eb9a7 tags:
 
## `for` loop
 
- another kind of loop
- does not require initialization of loop control variable outside the loop
- loop statement itself creates the loop control variable
- keywords `for` and `in`
 
### range built-in function
- accepts a single integer argument and produces a sequence of numbers from 0 to argument - 1, that is argument is exclusive
- accepts two integer arguments and produces a sequence of numbers from start (argument1) to end (argument2) - 1
 
%% Cell type:code id:92053fd6 tags:
 
``` python
for i in range(5): # single arugment -> produces 0, 1, 2, 3, and 4
print(i)
```
 
%% Output
 
0
1
2
3
4
 
%% Cell type:code id:25df6abb tags:
 
``` python
for i in range(5, 10): # two arguments -> produces 5, 6, 7, 8, and 9
print(i)
```
 
%% Output
 
5
6
7
8
9
 
%% Cell type:code id:ed75ffbc tags:
 
``` python
# TODO: write a for loop to iterate over the numbers 2 to 8
for i in range(2, 9):
print(i)
```
 
%% Output
 
2
3
4
5
6
7
8
 
%% Cell type:markdown id:cdc66ffa tags:
 
### Example 2: Find the max value of a function on an interval
 
<div>
<img src="attachment:Curve_peak.png" width="600"/>
</div>
 
%% Cell type:code id:2ec27141 tags:
 
``` python
def f(x):
#Try out both the definitions of this function
return 5 - (x - 2) ** 2
# return 5 - (x - 2.5) ** 2
 
print(f(1))
# TODO: for what value of x will f(x) produce the maximum y value?
print(f(2))
```
 
%% Output
 
4
5
 
%% Cell type:code id:27298992 tags:
 
``` python
# Goal: find the x that maximizes the y = f(x)
 
# Let's try the values from -5 to 5
x = -5
 
# Goal: after the loop, best_x and best_y should contain just that
best_x = x
best_y = f(x) # at any time, this is the BEST SO FAR
 
#Try out increasing increments, make sure to comment the other increment
# delta_x = 1
# delta_x = 0.1
# delta_x = 0.01
delta_x = 0.001
 
while x <= 5:
y = f(x)
if y >= best_y:
best_x = x
best_y = y
x += delta_x
 
print("Best x:", best_x)
print("Best y:", best_y)
```
 
%% Output
 
Best x: 1.9999999999998948
Best y: 5.0
 
%% Cell type:markdown id:249f2aa7 tags:
 
### Example 3: Integration (Riemann Sum)
 
<div>
<img src="attachment:ReimannSum.png" width="600"/>
</div>
 
%% Cell type:code id:96b98336 tags:
 
``` python
start_x = 1
end_x = 5
total_area = 0
current_x = start_x
# Try out increasing values of width, make sure to comment the other width values
# delta_x = 1
delta_x = 0.1
# delta_x = 0.01
# delta_x = 0.001
 
while current_x <= end_x:
y = f(current_x) # TODO: use f(x) defined previously
rect_area = delta_x * y
total_area += rect_area
current_x += delta_x
 
print("Area found using approximation is:", total_area)
```
 
%% Output
 
Area found using approximation is: 10.65999999999999
 
%% Cell type:markdown id:3e8d609b tags:
 
### Example 4: Find primes
 
%% Cell type:code id:f2d0c381 tags:
 
``` python
def is_prime(num):
""" returns True if x is prime, false otherwise. Assumes x is positive"""
 
# try all divisors from 2 to sqrt(num) to check if num is prime
divisor = 2
while divisor <= math.sqrt(num):
# check if num is divisible by divisor
if num % divisor == 0:
return False
divisor += 1
 
return True
```
 
%% Cell type:code id:e7aea11c tags:
 
``` python
print(is_prime(1))
print(is_prime(2))
print(is_prime(3))
print(is_prime(7))
print(is_prime(16))
print(is_prime(23))
print(is_prime(1000000))
```
 
%% Output
 
True
True
True
True
False
True
False
 
%% Cell type:code id:56d538aa tags:
 
``` python
print("Prime numbers:")
number = 2
while number <= 50: # TODO: comment out this while loop and write equivalent for loop using range
# TODO: comment out this while loop and write equivalent for loop using range
#while number <= 50:
for number in range(2, 51):
if is_prime(number):
print(number, "is prime")
else:
print(number, "is not prime")
number += 1
```
 
%% Output
 
Prime numbers:
2 is prime
3 is prime
4 is not prime
5 is prime
6 is not prime
7 is prime
8 is not prime
9 is not prime
10 is not prime
11 is prime
12 is not prime
13 is prime
14 is not prime
15 is not prime
16 is not prime
17 is prime
18 is not prime
19 is prime
20 is not prime
21 is not prime
22 is not prime
23 is prime
24 is not prime
25 is not prime
26 is not prime
27 is not prime
28 is not prime
29 is prime
30 is not prime
31 is prime
32 is not prime
33 is not prime
34 is not prime
35 is not prime
36 is not prime
37 is prime
38 is not prime
39 is not prime
40 is not prime
41 is prime
42 is not prime
43 is prime
44 is not prime
45 is not prime
46 is not prime
47 is prime
48 is not prime
49 is not prime
50 is not prime
%% Cell type:code id:aa37a65c tags:
``` python
```
......
%% Cell type:markdown id:dd1c3a1b tags:
 
# Iteration 1
 
## Readings:
- Chapter 7 of Think Python
- Chapter 6.1 to 6.3 of Python for Everybody
%% Cell type:markdown id:595d2e5b tags:
 
## Learning Objectives:
 
- Implement an iterative algorithm using a `while` loop, for
- printing / counting
- validating user input
- performing an iterative calculation
- printing character art
 
- Trace iterative algorithms and determine their output
 
- Recognize common `while` loop errors
- Infinite loops (when unintentional)
- Off-by-one mistakes in the loop control variable
 
%% Cell type:code id:c30a8ea2 tags:
 
``` python
# import statements
 
import time
import math
 
# This module only works on MAC, sorry Windows folks :(
import beeper
```
 
%% Cell type:markdown id:f113fc7b tags:
 
### Example 0: Simple countdowns
 
%% Cell type:markdown id:73959e77 tags:
 
**How to termination infinite loop in:**
- jupyter: Kernel > Interrupt (fix and then re-run)
- script mode / interactive mode: Ctrl + C (Kill signal)
 
%% Cell type:code id:cb8de263 tags:
 
``` python
#TODO: Copy/paste this example into PythonTutor
#Count from 0 to 3, printing each number
count = 0
 
while count <= 3:
print(count)
count += 1
```
 
%% Cell type:code id:23dbc9da tags:
 
``` python
#TODO: Copy/paste this example into PythonTutor
#Count from 3 to -3, printing each number
count = 3
 
while count >= -3:
print(count)
count -= 1
```
 
%% Cell type:markdown id:115e8742 tags:
 
### Example 1: Countdown timer alarm
 
%% Cell type:code id:42f4a48f tags:
 
``` python
# TODO: use input function to get user input for number of seconds
start = ???
 
# TODO: copy start into another variable
remaining = ???
while ???: # TODO: iterate from start to 1
print(remaining, "seconds left")
# TODO: update loop control variable's value to make progress towards terminating the loop,
# that is turning loop condition to False
# TODO: update loop control variable's value to make progress towards terminating
# the loop, that is turning loop condition to False
remaining -= ???
# TODO: now run the cell to see the output. Didn't it go too fast?
# TODO: call time module sleep function, by passing 1 as argument
 
# TODO: print "BEEP BEEP BEEP ..." (10 BEEPS) without typing BEEP 10 times
# What string operator can you use here?
# wake up call
```
 
%% Cell type:markdown id:d54200ad tags:
 
## `for` loop
 
- another kind of loop
- does not require initialization of loop control variable outside the loop
- loop statement itself creates the loop control variable
- keywords `for` and `in`
 
### range built-in function
- accepts a single integer argument and produces a sequence of numbers from 0 to argument - 1, that is argument is exclusive
- accepts two integer arguments and produces a sequence of numbers from start (argument1) to end (argument2) - 1
 
%% Cell type:code id:43776615 tags:
 
``` python
for i in range(5): # single arugment -> produces 0, 1, 2, 3, and 4
print(i)
```
 
%% Cell type:code id:e3f01e6f tags:
 
``` python
for i in range(5, 10): # two arguments -> produces 5, 6, 7, 8, and 9
print(i)
```
 
%% Cell type:code id:b37a6842 tags:
 
``` python
# TODO: write a for loop to iterate over the numbers 2 to 8
```
 
%% Cell type:markdown id:cdc66ffa tags:
 
### Example 2: Find the max value of a function on an interval
 
<div>
<img src="attachment:Curve_peak.png" width="600"/>
</div>
 
%% Cell type:code id:2ec27141 tags:
 
``` python
def f(x):
return 5 - (x - 2) ** 2
 
print(f(1))
```
 
%% Cell type:code id:0098b6aa tags:
 
``` python
# TODO: for what value of x will f(x) produce the maximum y value?
print(f(???))
```
 
%% Cell type:code id:27298992 tags:
 
``` python
# Goal: find the x that maximizes the y = f(x)
 
# Let's try the values from -5 to 5
 
# Goal: after the loop, best_x and best_y should contain just that
 
# Try out increasing increments, make sure to comment the other increment
# delta_x = 1
# delta_x = 0.1
# delta_x = 0.01
# delta_x = 0.001
 
 
 
print("Best x:", best_x)
print("Best y:", best_y)
```
 
%% Cell type:markdown id:249f2aa7 tags:
 
### Example 3: Integration (Riemann Sum)
 
<div>
<img src="attachment:ReimannSum.png" width="600"/>
</div>
 
%% Cell type:code id:96b98336 tags:
 
``` python
# Let's try the values from 1 to 5
start_x = 1
end_x = 5
total_area = 0
current_x = start_x
# Try out increasing values of width, make sure to comment the other width values
# delta_x = 1
delta_x = 0.1
# delta_x = 0.01
# delta_x = 0.001
 
while current_x <= end_x:
y = ??? # TODO: use f(x) defined previously
rect_area = ???
total_area += ???
current_x += delta_x
 
print("Area found using approximation is:", total_area)
```
 
%% Cell type:markdown id:3e8d609b tags:
 
### Example 4: Find primes
 
%% Cell type:code id:f2d0c381 tags:
 
``` python
def is_prime(num):
""" returns True if x is prime, false otherwise. Assumes x is positive"""
 
# try all divisors from 2 to sqrt(num) to check if num is prime
divisor = 2
while ???:
# check if num is divisible by divisor
if num % divisor == 0:
return False
divisor ???
 
return True
```
 
%% Cell type:code id:e7aea11c tags:
 
``` python
print(is_prime(1))
print(is_prime(2))
print(is_prime(3))
print(is_prime(7))
print(is_prime(16))
print(is_prime(23))
print(is_prime(1000000))
```
 
%% Cell type:code id:d26d790c tags:
 
``` python
print("Prime numbers:")
number = 2
while number <= 50: # TODO: comment out this while loop and write equivalent for loop using range
# TODO: comment out this while loop and write equivalent for loop using range
while number <= 50:
if is_prime(number):
print(number, "is prime")
else:
print(number, "is not prime")
number += 1
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment