This follows the lesson plan at https://edube.org
Program 41 – If Else
if the_weather_is_good:
go_for_a_walk()
elif tickets_are_available:
go_to_the_theater()
elif table_is_available:
go_for_lunch()
else:
play_chess_at_home()
elif and else are both optional. You can have simply an if.
If I get bit by a dog: see a doctor()
Program 42 – max() function – Also min() function
# Read three numbers.
number1 = int(input(“Enter the first number: “))
number2 = int(input(“Enter the second number: “))
number3 = int(input(“Enter the third number: “))
# Check which one of the numbers is the greatest
# and pass it to the largest_number variable.
largest_number = max(number1, number2, number3)
# Print the result.
print(“The largest number is:”, largest_number)
Program 43 – Sending a series of numbers to multiple variables.
x, y, z = 5, 10, 8
print(x > z)
# False
print((y - 5) == x) # True
Program 44 – While and For
i = 0
while i < 100:
# do_something()
i += 1
print(i)
for i in range(100):
# do_something()
pass
print(i)
for i in range(10):
print("The value of i is currently", i)
for i in range(2, 8):
print("The value of i is currently", i)
An example:
my_word = input("Enter a word: ")
for letter in my_word:
# Complete the body of the for loop.
if letter=="a":pass
elif letter=="e":pass
elif letter=="i":pass
elif letter=="i":pass
elif letter=="u":pass
else: print(letter)
pass
print(my_word)
Program 45 – One Second Delay in print
import time
i = 0
for i in range(1,6):
print(i,”Mississippi”)
time.sleep(1)
pass
print(“Here I come.”)
Program 46 – Block Counting Program
import sys import signal blocks = int(input("Enter the number of blocks: ")) blocks_used=0 row_count=1 blocks = blocks - row_count while blocks > blocks_used: if blocks<1: print("I exited here.") sys.exit() else: # print(row_count) blocks_used = blocks_used+row_count row_count += 1 blocks = blocks - row_count print(" ( Blocks",blocks,")") if blocks < row_count: print("Pyramid is",row_count,"high.") Unused_blocks = blocks print("Blocks used: ",blocks_used+row_count) print("Unused blocks: ",(blocks)) print("\n\n") sys.exit() pass
Program 47 - Lothar Collatz Hypothesis
# take any non-negative and non-zero integer number and name it c0; # if it's even, evaluate a new c0 as c0 ÷ 2; # otherwise, if it's odd, evaluate a new c0 as 3 × c0 + 1; # if c0 ≠ 1, skip to point 2. import sys mem_break = 0 iteration = 0 odd_even = "nothing" c0 = int(input("Enter any value: ")) while mem_break < 100: mem_break += 1 iteration += 1 if c0 % 2 == 0: c0 = c0 / 2 odd_even = "even" else: c0 = 3 * c0 + 1 odd_even = "odd" print(iteration, c0, odd_even) if c0 == 1.0: print("iterations",iteration) print("\n\n") sys.exit()
Program 48 - for letter in word
import sys mem_break = 0 iteration = 0 while mem_break < 100: mem_break += 1 iteration += 1 word = "Python" for letter in word: print(letter, end="*") # print("\n\n") # sys.exit()
Program 49 - More examples
import sys mem_break = 0 iteration = 0 # output: P*y*t*h*o*n* etc while mem_break < 100: mem_break += 1 iteration += 1 word = "Python" for letter in word: print(letter, end="*") # output: 2 4 6 8 - no remainder for i in range(1, 10): if i % 2 == 0: print(i) # output: OpenEDG - evaluation terminates with P text = "OpenEDG Python Institute" for letter in text: if letter == "P": break print(letter, end="") # output: prints pypypy right next to Open EDG without line feed text = "pyxpyxpyx" for letter in text: if letter == "x": continue print(letter, end="") # sys.exit()
Program 50 - More reference - While Else
import sys mem_break = 0 iteration = 0 # output: P*y*t*h*o*n* etc n = 0 while mem_break < 20: mem_break += 1 iteration += 1 while n != 3: print(n) n += 1 else: print(n, "else") print() for i in range(0, 3): print(i) else: print(i, "else") for i in range(3): print(i, end=" ") # Outputs: 0 1 2 for i in range(6, 1, -2): print(i, end=" ") # Outputs: 6, 4, 2