Python Control Structures and Examples

python control structure n.w
1 / 27
Embed
Share

Learn about Python control structures including conditional execution, alternative execution, chained conditionals, loops, and iterations. Examples provided for better understanding.

  • Python Basics
  • Control Structures
  • Conditional Execution
  • Loops
  • Iterations

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Python ~ Control Structure

  2. Conditional execution (if) Syntax: if condition: do_something Condition must be statement that evaluates to a boolean value (True or False) 2

  3. Example: Checking user input x = input("X=") ifx.isdigit(): print("You input a number") 3

  4. Alternative execution Syntax: ifcondition: do_something else: do_alternative 4

  5. Example: Checking user input x = input("x=") ifx.isdigit(): print "You input a number" else: print "Please input a number\ next time" 4/18/09 5

  6. Example: Avoiding division by zero x = int(input("x=")) y = int(input("y=")) ify <> 0: print(x / y) else: print("Attempted division by zero") 6

  7. Example: Checking user input x = input("x=") ifx.isdigit(): print("You input a number") else: print("Please input a number next time") 7

  8. Chained conditionals Syntax: ifcondition: do_something elifcondition: do_alternative1 else: do_alternative2 8

  9. Example: x = int(input("x=")) y = int(input("y=")) ifx < y: print('x is less than y') elifx > y: print('x is greater than y') else: print('x and y are equal') 9

  10. Loop & Iteration

  11. While Loop Repeated Steps n = 5 No Yes Output: n = 5 while n > 0 : print(n) n = n 1 n > 0 ? 5 4 3 2 1 print n n = n -1 print('Blastoff! ) Blastoff! print 'Blastoff'

  12. Washing Machine repeat(5){Lather, Rinse} Dry off An Infinite Loop n = 5 n = 5 while n > 0 : print('Lather') print('Rinse') No Yes n > 0 ? print 'Lather' print 'Rinse' print('Dry off! ') print 'Dry off!' What is wrong with this loop?

  13. Washing Machine repeat(5){Lather, Rinse} Dry off n = 5 n = 5 while n > 0 : print('Lather') print('Rinse') n = n - 1 No Yes n > 0 ? print 'Lather' print 'Rinse' n = n -1 print('Dry off! ') print 'Dry off!'

  14. Bad Washing Machine Another Loop n = 0 No Yes n = 0 while n > 0 : print('Lather') print('Rinse') print('Dry off! ') n > 0 ? print 'Lather' print 'Rinse' print 'Dry off!' What does this loop do?

  15. Breaking Out of a Loop The break statement ends the current loop and jumps to the statement immediately following the loop It is like a loop test that can happen anywhere in the body of the loop while True: x = int(input()) if x == -1 : break print(x) 4 4 3 3 -1 print('Done!') Done!

  16. from turtle import * wn = Screen() wn.setup(400,200) from turtle import * wn = Screen() wn.setup(400,200) sarah = Turtle() sarah = Turtle() sarah.forward(50) sarah.left(90) sarah.forward(50) sarah.left(90) sarah.forward(50) sarah.left(90) sarah.forward(50) sarah.left(90) #repeat four times for i in range(4): sarah.forward(50) sarah.left(90) wn.exitonclick() wn.exitonclick()

  17. def function():

  18. from turtle import * from turtle import * wn = Screen() def drawSquare(t, size): alex= Turtle() """Make turtle t draw a square of with side size.""" for i in range(4): for i in range(4): t.forward(size) alex.forward(150) t.left(90) alex.left(90) wn = Screen() alex = Turtle() # Set up the window and its attributes # create alex wn.exitonclick() drawSquare(alex, 150) wn.exitonclick() # Call the function to draw the square

  19. from turtle import * def drawSquare(t, size): """Make turtle t draw a square of with side size.""" for i in range(4): t.forward(size) t.left(90) wn = Screen() alex = Turtle() # Set up the window and its attributes # create alex alex.left(20) drawSquare(alex, 150) alex.left(20) drawSquare(alex, 150) alex.left(20) drawSquare(alex, 150) wn.exitonclick()

  20. from turtle import * def drawSquare(t, size): """Make turtle t draw a square of with side size.""" for i in range(4): t.forward(size) t.left(90) wn = Screen() alex = Turtle() # Set up the window and its attributes # create alex alex.left(20) drawSquare(alex, 150) alex.left(20) drawSquare(alex, 150) alex.left(20) drawSquare(alex, 150) for n in range(3): alex.left(20) drawSquare(alex,150) wn.exitonclick()

  21. for loop n = 5 No Yes n > 0 ? n = 5 while n > 0 : print(n) n = n 1 print('Blastoff! ') for n in range(5,0,-1): print(n) print n n = n -1 print('Blastoff! ') print 'Blastoff'

  22. for for n in [5,4,3,2,1] print(n) for n in range(5,0,-1): print(n) print('Blastoff! ) print('Blastoff! )

  23. for for n in [5,4,3,2,1]: print(n) for n in range(5,0,-1): print(n) print('Blastoff! ) print('Blastoff! )

  24. for for n in range(1,5,1) : print(n) for n in range(1,5,1): print('1') print('Finished! ') print('Finished! ')

  25. for for n in range(0,5,1) : print(n) for n in range(5): print(n) print('Finished! ') print('Finished! ')

  26. range(start, condition, increase) range(1,100,1) range(0,100,1) range(100) range(1,100,2) range(2,100,2) range(5,100,3) range(100,1,-1) range(100,0,-2)

  27. for for n in range(0,50,1): drawSquare(alex,50) for n in range(0,50,1) : drawSquare(alex,n) for n in range(0,50,1) : drawSquare(alex,n) alex.right(20)

More Related Content