1 / 33

Python – Part 4

Python – Part 4. Conditionals and Recursion. 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.

seven
Download Presentation

Python – Part 4

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. Python – Part 4 Conditionals and Recursion

  2. Modulus Operator • Yields the remainder when first operand is divided by the second. • >>>remainder=7%3 • >>>print (remainder) • 1 Prepared by Department of Preparatory year

  3. Boolean expressions • An expression that is either true or false • Operator == • >>>5==5 • True • >>>5==6 • False Prepared by Department of Preparatory year

  4. Boolean expressions • Type bool – True and False • >>>type (True) • <type ‘bool’> • >>>type (False) • <type ‘bool’> Prepared by Department of Preparatory year

  5. 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 Prepared by Department of Preparatory year

  6. 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 Prepared by Department of Preparatory year

  7. Conditional execution • If statement if 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) Prepared by Department of Preparatory year

  8. 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 Prepared by Department of Preparatory year

  9. Chained conditionals if-elseif statement if 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 Prepared by Department of Preparatory year

  10. Loops 10 • 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. Prepared by Department of Preparatory year

  11. While Statement MUST end with colon. MUST have indentation. 11 • New keyword while • Syntax while ( condition ): expression1 expression2 … Prepared by Department of Preparatory year

  12. While Statement • while ( condition ): expression1 expression2 … • next expression 12 • 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. Prepared by Department of Preparatory year

  13. While Statement • Initialize loop variable outside of the loop. • Define loop condition. • Do loop work. • Change the loop variable. 13 • The ingredients of a loop: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  14. While Statement Execution: Loop variable, n, set to 0 outside the loop. Perform test: 0 <=5 True. So we enter loop. Print n (so, we print 0) Change n from 0 to 1. Go back to test. OUTPUT 0 14 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  15. While Statement Execution: Perform test: 1 <=5  True. So we enter loop. Print n (so, we print 1) Change n from 1 to 2. Go back to test. OUTPUT 0 1 15 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  16. While Statement Execution: Perform test: 2 <=5  True. So we enter loop. Print n (so, we print 2) Change n from 2 to 3. Go back to test. OUTPUT 0 1 2 16 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  17. While Statement Execution: Perform test: 3 <=5  True. So we enter loop. Print n (so, we print 3) Change n from 3 to 4. Go back to test. OUTPUT 0 1 2 3 17 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  18. While Statement Execution: Perform test: 4 <= 5  True. So we enter loop. Print n (so, we print 4) Change n from 4 to 5. Go back to test. OUTPUT 0 1 2 3 4 18 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  19. While Statement Execution: Perform test: 5 <= 5  True. So we enter loop. Print n (so, we print 5) Change n from 5 to 6. Go back to test. OUTPUT 0 1 2 3 4 5 19 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  20. While Statement Execution: Perform test: 6 <= 5  False. Skip the loop. Print “Out of LOOP!” OUTPUT 0 1 2 3 4 5 Out of LOOP! 20 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Out Of LOOP!” Prepared by Department of Preparatory year

  21. While Statement Key points: Initialize loop variable outside of the loop. Determine loop condition. Do loop work. Change the test value. 21 • Example 1: n = 0 while ( n <= 5 ): print n n = n + 1 print “Blast off!” Prepared by Department of Preparatory year

  22. While Statement 22 • 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. Prepared by Department of Preparatory year

  23. While Statement 23 • 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? Prepared by Department of Preparatory year

  24. While Statement 24 • Infinite loop n = 5 while ( n < 6 ): print n n = n - 1 print “Blast off!” • n must always be less than 10. Prepared by Department of Preparatory year

  25. While Statement 25 • Infinite loop n = 5 while ( n != 0 ): print n n = n - 2 print “Blast off!” • n will never reach the value 0: 9, 7, 5, 3, 1, -1, -2, etc. Prepared by Department of Preparatory year

  26. While Statement 26 • Infinite loop n = 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. Prepared by Department of Preparatory year

  27. Recursion • One function calls itself def countdown(n): if n <= 0: print ('Blastoff!' ) else: print (n) countdown(n-1) • What happens if we call • >>> coundown (3) Prepared by Department of Preparatory year

  28. 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 Prepared by Department of Preparatory year

  29. Infinite recursion • Recursion never reaches a base case def recurse(): recurse() Prepared by Department of Preparatory year

  30. 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 Prepared by Department of Preparatory year

  31. 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 Prepared by Department of Preparatory year

  32. Keyboard input • >>> prompt = 'What is the velocity?\n' • >>> speed = input(prompt) • What is the velocity? • 17 • >>> int(speed) • 17 Prepared by Department of Preparatory year

  33. Part 4 End

More Related