0% found this document useful (0 votes)
20 views10 pages

03-Control 4pp

The document explains while loops, conditional operators, logical operators, and conditionals in Python. It provides examples of using while loops to count to 10 and draw an expanding circle. It also demonstrates using conditional statements like if/else and logical operators like and/or/not.

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

03-Control 4pp

The document explains while loops, conditional operators, logical operators, and conditionals in Python. It provides examples of using while loops to count to 10 and draw an expanding circle. It also demonstrates using conditional statements like if/else and logical operators like and/or/not.

Uploaded by

Kaphun Krub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Count to 10 While Loop

print( "1" )
while condition:
print( "2" ) body
print( "3" )
print( "4" )
.
.
.
print( "10" )

While Loop While Loop


while condition: while condition:
body body

an expression that evaluates to True/False


one or more lines of code
(indented, just like a function body)
Conditional Operators Conditional Operators
x = 5 x = 5
x < 4 —> False x < 6 —> True

Conditional Operators Conditional Operators


< less than = != ==
> greater than
== equal to x = 5
>= greater than or equal to
<= less than or equal to 5 == x
!= not equal to True

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

Logical Operators Logical Operators


A and B: not( (3 < 4) and (10 < 12) ) False
True if A is True and B is True
(10 > 12) or (5 != 6) True
A or B:
True if A is True or B is True not( not( False == False ) ) True

not A: “aardvark” < “zebra” True


True if A is False
False if A is True
Logical Operators # ——— DRILL ———
# write some code that generates the following
not( (3 < 4) and (10 < 12) ) False

(10 > 12) or (5 != 6) True

not( not( False == False ) ) True

“aardvark” < “zebra” True

True > False True

# ——— DRILL ———


# write some code that generates the following

import drawSvg as draw

# draw expanding circle


r = 0
R = 100
with draw.animate_jupyter(draw_frame, delay=0.01) as anim:
while( r < R ):
anim.draw_frame(r)
r = r + 1
# draw expanding circle # draw expanding circle
r = 0 r = 0
R = 100 R = 100
with draw.animate_jupyter(draw_frame, delay=0.01) as anim: with draw.animate_jupyter(draw_frame, delay=0.01) as anim:
while( r < R ): while( r < R ):
anim.draw_frame(r) anim.draw_frame(r)
r = r + 1 r = r + 1

# if r == R then switch directions

Conditionals Conditionals

temperature = 72 temperature = 72

if temperature <= 32: if temperature <= 32:


print("It's freezing.") print("It's freezing.")
else:
print("It’s not so cold.")
Conditionals Conditionals
x = 1
temperature = 72
if x > 0:
print("positive")
if temperature <= 32:
x = -1 * x
print("It's freezing.")
elif x < 0:
elif temperature <= 50:
print("negative")
print("It's cool.")
else:
elif temperature <= 75:
print("zero")
print("It's warm.")
else:
print( x )
print("It's hot.")

Conditionals # ——— DRILL ———


# write some code that generates the following

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

if circle is fully expanded or contracted: if circle is fully expanded or contracted:


reverse direction reverse direction

# 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 True:
anim.draw_frame(r) anim.draw_frame(r)
if( sign == 1 ): if sign == 1:
r = r + 1 r = r + 1
else: else:
r = r - 1 r = r - 1

if r > R or r < 0: if r > R or r < 0:


sign = -1 * sign sign = -1 * sign
docs ~/ python3 docs
>>> from math import sqrt
~/ python3 isPrime.py
>>>
~/
>>> def isPrime(n):
... i = 2
... while i <= int( sqrt(n) ):
... if n % i == 0:
... return False
... i = i + 1
... return True
...
>>> isPrime(7)
True
>>> isPrime(9)
False
>>>

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

# ——— DRILL ———


# write some code that prints all primes between 1 and N
# that are palindromes (e.g., 1764671)

[ pp.py ]

You might also like