03-Control 4pp
03-Control 4pp
print( "1" )
while condition:
print( "2" ) body
print( "3" )
print( "4" )
.
.
.
print( "10" )
5 = x
Error
Count to 10 Count to 10
n = 1
while ???: while ???:
print(n) print(n)
??? ???
Count to 10 Count to 10
n = 1 n = 1
while ???: while n <= 10:
print(n) print(n)
n = n + 1 n = n + 1
Count to 10 Conditional Operators
n = 1 from math import pi, sin
while n < 11:
pi
print(n)
3.14159265359
n = n + 1
sin(pi)
1.22464679915e-16
sin(pi) == 0
False
Conditionals Conditionals
temperature = 72 temperature = 72
x = 1
if x > 0:
print("positive")
x = -1 * x
elif x < 0:
print("negative")
else:
print("zero")
positive
print( x ) -1
# Draw expanding/contracting circle # Draw expanding/contracting circle
r = 0 # current radius r = 0 # current radius
R = 100 # maximum radius R = 100 # maximum radius
sign = 1 # direction (1: expand; -1: contract) sign = 1 # direction (1: expand; -1: contract)
with draw.animate_jupyter(draw_frame, delay=0.01) as anim: with draw.animate_jupyter(draw_frame, delay=0.01) as anim:
while ???: while ???:
anim.draw_frame(r) anim.draw_frame(r)
if sign == 1: if sign == 1):
# expand circle r = r + 1
else: else:
# contract circle r = r - 1
docs docs
~/ python3 -m doctest -v isPrime.py
Trying:
~/ python3 -i isPrime.py isPrime(9)
Expecting:
False
>>> isPrime(7) ok
Trying:
True
isPrime(7)
>>> isPrime(9) Expecting:
True
False
ok
>>> Trying:
isPrime(797)
Expecting:
True
ok
1 items had no tests:
isPrime
1 items passed all tests:
3 tests in isPrime.isPrime
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
docs default params
~/ python3 -i isPrime.py
>>> print(isPrime.__doc__)
>>> isPrime()
isPrime is a function that takes True
as input an integer and returns
True if it is prime and False >>> isPrime(9)
otherwise False
>>> isPrime(9)
False >>>
>>> isPrime(7)
True
>>> isPrime(797)
True
[ pp.py ]