1 / 24

Recursion and Conditionals

Recursion and Conditionals. Think Python Ch. 5. 5.1 Modulus Operator. >>>quotient = 7/3 >>>print quotient >>>2 >>>remainder = 7 % 3 >>>print remainder >>>1. Try it!. The result: 7 divided by 3 = 2 with 1 left over. Uses of Modulus. Is one number divisible by another

imaran
Download Presentation

Recursion and Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursion and Conditionals Think Python Ch. 5

  2. 5.1 Modulus Operator >>>quotient = 7/3 >>>print quotient >>>2 >>>remainder = 7 % 3 >>>print remainder >>>1 Try it! The result: 7 divided by 3 = 2 with 1 left over

  3. Uses of Modulus • Is one number divisible by another >>>defis_even(x,y): if x%y==0: print 'x is divisible y' else: print 'x is not divisible by y‘ >>>is_even(3,4) Try it x is not divisible by y

  4. Uses of Modulus • Extract the right most digit(s) of a number >>> 2010 % 10 0 >>> 2010 % 100 10 Try it! Modular arithmetic is referenced in number theory, group theory, ring theory, knot theory, abstract algebra, cryptography, computer science, chemistry and the visual and musical arts.

  5. 5.2 Boolean Expressions >>> x = 5 >>> y = 6 >>> x == x true >>> x == y false >>> x != y true >>> x != x false 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 Try each boolean expression

  6. 5.3 Logical Operators • Logical operators: and, or, not…plus many more >>>defcompare(n): if n%2 == 0 or n%3 ==0: print 'true' else: print 'false‘ >>>Compare(2) >>>Compare(3) >>>Compare(5) >>>Compare(6) =True =True =False =True Guess, then try it!

  7. 5.3 cont. >>>def compare(n): if n%2 == 0 andn%3 ==0: print 'true' else: #branch print 'false‘ >>>compare(2) >>>compare(3) >>>compare(6) = False = False = True Try it

  8. 5.4 Conditional Execution >>> if x > 0: print 'x is positive' • Whole thing is compound statement • If x > 0: #head • Print ‘x is positive’ #body • X > 0 #condition >>> if x < 0: pass #does nothing until fixed

  9. 5.4 cont. >>>def condition(x): if x > 0: print "X is positive" if x < 0: print "X is negative“ >>>condition(2) >>>condition(-2) X is positive X is negative Try it

  10. 5.5 Alternative Execution • Two possibilities >>>defodd_even(x): if X % 2 == 0: print ‘x is even’ else: #branch print ‘x is odd’ >>>odd_even(2) >>>odd_even(3) Try it X is even X is odd

  11. 5.6 Chained Conditionals >>>if x < y: print 'x is less than y' elifx > y: print 'x is greater than y' else: print 'x and y are equal' Try it: but, make it a definition and test for 2,3; 3,2; and 3,3

  12. 5.4 Conditional Practice • Create a definition which; • Determines if a number is even or odd • The output should indicate whether the number is even or odd • Test your definition for integers 2, 3, and 6 • Use a comment to indicate a branch if your definition has one

  13. 5.4 Practice Answer >>>def compare(n): if n%2 == 0 andn%3 ==0: print 'true' else: #branch print 'false‘ >>>compare(2) >>>compare(3) >>>compare(6) = False = False = True

  14. 5.6 cont. defchained_conditional(x,y): if x < y: print 'x is less than y' elifx > y: print 'x is greater than y' else: print 'x and y are equal‘ chained_conditional(2,3) (x is less than y) chained_conditional(3,2) (x is greater than y) Chained_conditional(3,3) (x and y are equal) • elif is an abbreviation of “else if.” • Only one branch will be executed. • There is no limit on the number of elif statements. • If there is an else clause, it has to be at the end, but there doesn’t have to be one.

  15. 5.7 Nested Conditionals defnd(x,y): if x == y: print 'x and y are equal' else: if x < y: print 'x is less than y' else: print 'x is greater than y'

  16. 5.8 Recursion def countdown(n): if n <= 0: print 'Blastoff!' else: print n countdown(n-1) Try it Add setup: import time #imports time function Add: time.sleep(1) #Waits1 second #add after print n

  17. 5.9 Stack Diagrams • Create a stack diagram for “Blastoff” recursion starting at 5. Try it Hint:

  18. 5.10 Infinite Recursion If a recursion never reaches a base case, it goes on making recursive calls forever, and the program never terminates. This is known as infinite recursion. >>>defrecurse( ): recurse( )

  19. 5.11 Input >>>input = raw_input() What are you waiting for? >>> print input What are you waiting for? Try it >>>name = raw_input('What...is your name?\n') What...is your name? Arthur, King of the Britons! >>> print name Arthur, King of the Britons! Try it There’s a small error here, what is it?

  20. 5.12 Debugging You enter: import math signal_power= 9 noise_power= 10 ratio = signal_power / noise_power decibels = 10 * math.log10(ratio) print decibels Python squawks back with: Traceback (most recent call last): File "snr.py", line 5, in ? decibels = 10 * math.log10(ratio) OverflowError: math range error What is the problem? Try printing results such as : Variables and ratio You get 0, what’s wrong with that? Dividing two integers does floor division Solution? Change ratio to: float(signal_power) / float(noise_power)

  21. 5.6 Chained Conditional Practice • Create a chained conditional definition that when run: • Determines if X is less than Y • Prints a statement indicating X is less than Y • Determines if x is greater than y • Prints a statement indicating X is greater than Y • Determines if X and Y are equal • Prints a statement indicating X is greater than Y • Test the definition for • (2, 3) • (3, 2) • (3, 3)

  22. 5.6 Answer defchained_conditional(x,y): if x < y: print 'x is less than y' elifx > y: print 'x is greater than y' else: print 'x and y are equal‘ chained_conditional(2,3) (x is less than y) chained_conditional(3,2) (x is greater than y) Chained_conditional(3,3) (x and y are equal)

  23. 5.6 Recursion Practice • Create a definition which • Counts down from a given number • Prints each recursion of the number • Waits one second before printing the next number • Prints “Blastoff!” when it reaches 0 • Use a conditional statement to determine if 0 is reached • Use an else statement to create the recursion if the number n != 0 • Test the definition for (10)

  24. 5.8 Practice Answer def countdown(n): if n <= 0: print 'Blastoff!' else: print n import timetime.sleep(1) countdown(n-1)

More Related