python – part 4uquspy.weebly.com/uploads/1/6/9/5/16956564/python__part_4.pdf · loops •what is...
TRANSCRIPT
Modulus Operator
• Yields the remainder when first operand is divided by the second.
• >>>remainder=7%3
• >>>print (remainder)
• 1
Boolean expressions
• An expression that is either true or false
• Operator ==
• >>>5==5
• True
• >>>5==6
• False
Boolean expressions
• Type bool – True and False
• >>>type (True)
• <type ‘bool’>
• >>>type (False)
• <type ‘bool’>
Boolean expressions
Other operators:
x != y # x is not equal to y
x > y # x is greater than y
x < y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
Logical operators
• And, or, not
• Semantics similar to their meaning in English
• x>0 and x<10
• not(x>y)
• Any nonzero number in Python is interpreted as “true”
• >>> 17 and True
• True
Conditional execution
• If statementif x>0: # CONDITION
print (‘x is positive’)
Same structure as function definition
- Header
- Indented block
- No limit on number of statements in the body (but at least one)
Alternative execution
if – else statement
if x%2==0:
print (‘x is even’)
else:
print (‘x is odd’)
- Exactly one of the alternatives executed
- Alternatives are called branches
Chained conditionals
if-elseif statementif x<y:
print (‘x is less than y’)elif x>y:
print (‘x is greater than y’)else:
print (‘x and y are equal’)
- Exactly one branch executed (no limit on number of elseif stmts). If there is else must be at the end
Loops
• What is a loop for?
– To repeat a piece of code over and over.
– Examples:
• Iterating through an array (sum, search, print, etc.)
• Run the main program loop (i.e. keep asking for user input until the program is over.)
• Etc.
10
While Statement• New keyword
while
• Syntaxwhile ( condition ):
expression1
expression2
…
11
MUST end with colon.
MU
ST have in
den
tation
.
• While execution:
– Perform test
– If test true, go to body.
• Execute body expressions.
• At the end of the block, go back to test.
– If test is false, go on.
12
while ( condition ):
expression1
expression2
…
next expression
• The ingredients of a loop:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
13
1. Initialize loop variable outsideof the loop.
2. Define loop condition.3. Do loop work.4. Change the loop variable.
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
14
Execution:1. Loop variable, n, set to 0
outside the loop.2. Perform test: 0 <=5 True. So
we enter loop.3. Print n (so, we print 0)4. Change n from 0 to 1.5. Go back to test.
0
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
15
Execution:6. Perform test: 1 <=5 True. So
we enter loop.7. Print n (so, we print 1)8. Change n from 1 to 2.9. Go back to test.
0
1
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
16
Execution:10. Perform test: 2 <=5 True. So
we enter loop.11. Print n (so, we print 2)12. Change n from 2 to 3.13. Go back to test.
0
1
2
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
17
Execution:14. Perform test: 3 <=5 True. So
we enter loop.15. Print n (so, we print 3)16. Change n from 3 to 4.17. Go back to test.
0
1
2
3
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
18
Execution:18. Perform test: 4 <= 5 True. So
we enter loop.19. Print n (so, we print 4)20. Change n from 4 to 5.21. Go back to test.
0
1
2
3
4
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
19
Execution:22. Perform test: 5 <= 5 True. So
we enter loop.23. Print n (so, we print 5)24. Change n from 5 to 6.25. Go back to test.
0
1
2
3
4
5
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Out Of LOOP!”
20
Execution:26. Perform test: 6 <= 5 False.
Skip the loop.27. Print “Out of LOOP!”
0
1
2
3
4
5
Out of LOOP!
OUTPUT
• Example 1:n = 0
while ( n <= 5 ):
print n
n = n + 1
print “Blast off!”
21
Key points:1. Initialize loop variable outside
of the loop.2. Determine loop condition.3. Do loop work.4. Change the test value.
• Example 1:n = 1
while ( n <= 5 ):
print n
# n = n + 1
print “Blast off!”
– What would happen if we didn’t change the loop variable?• The loop condition would never become false.
22
• Infinite loop
– When the test condition never has the chance to become False, you have an infinite loop.
– World’s simplest infinite loop:while ( True ):
print “hi”
• Other possible infinite loops?
23
• Infinite loopn = 5
while ( n < 6 ):
print n
n = n - 1
print “Blast off!”
– n must always be less than 6.
24
• Infinite loopn = 5
while ( n != 0 ):
print n
n = n - 2
print “Blast off!”
– n will never reach the value 0: 5, 3, 1, -1, -3, -5, -7, etc.
25
• Infinite loopn = 5
while ( n >= 0 ):
print n
n = n - 2
print “Blast off!”
– Not an infinite loop. When n reaches -1, the test wil no longer be true.
26
Recursion
• One function calls itselfdef countdown(n):
if n <= 0: print ('Blastoff!' )
else: print (n)countdown(n-1)
- What happens if we call- >>> coundown (3)
Recursion
• def print_n(s, n):if n <= 0:
return
print (s)
print_n(s, n-1)
- return statement exits the function
- Base case
- Recursive (general) case
Keyboard input
• Built-in function called input (previous versions raw_input)
• Program stops and waits for the user to type something
• Value pressed returned to program as a string
• Good idea to print a prompt telling user what to input
Keyboard input
• >>>name =input (‘What is your name?\n’)
• Arthur, King of the Britons!
• >>>print (name)
• Arthur, King of the Britons!
• \n represents a newline
Keyboard input
• >>> prompt = 'What is the velocity?\n'
• >>> speed = input(prompt)
• What is the velocity?
• 17
• >>> int(speed)
• 17