1 / 17

Day 2 – Lesson 7 Conditionals and Loops

Day 2 – Lesson 7 Conditionals and Loops. Python Mini-Course University of Oklahoma Department of Psychology. Lesson objectives. Use conditional statements in Python, including the “if”, “if…else”, and “if…elif” statements Use Boolean logic to create complex conditionals

zamora
Télécharger la présentation

Day 2 – Lesson 7 Conditionals and Loops

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. Day 2 – Lesson 7Conditionals and Loops Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 2 - Lesson 7

  2. Lesson objectives • Use conditional statements in Python, including the “if”, “if…else”, and “if…elif” statements • Use Boolean logic to create complex conditionals • Use the “for” statement to create loops Python Mini-Course: Day 2 - Lesson 7

  3. Conditional execution (if…) • Syntax: if condition: do_something • Condition must be statement that evaluates to a boolean value (True or False) Python Mini-Course: Day 2 - Lesson 7

  4. Example:Checking user input x = raw_input("X? ") if x.isdigit(): print "You input a number" Python Mini-Course: Day 2 - Lesson 7

  5. Alternative execution • Syntax: if condition: do_something else: do_alternative Python Mini-Course: Day 2 - Lesson 7

  6. Example:Checking user input x = raw_input("x? ") if x.isdigit(): print "You input a number" else: print "Please input a number\ next time" Python Mini-Course: Day 2 - Lesson 7

  7. Example:Avoiding division by zero x = int(raw_input("x? ")) y = int(raw_input("y? ")) if y <> 0: print x / y else: print "Attempted division by\ zero" Python Mini-Course: Day 2 - Lesson 7

  8. Chained conditionals • Syntax: if condition: do_something elif condition: do_alternative1 else: do_alternative2 Python Mini-Course: Day 2 - Lesson 7

  9. Example: x = int(raw_input("x? ")) y = int(raw_input("y? ")) 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' Python Mini-Course: Day 2 - Lesson 7

  10. Notes on conditionals • You have to have at least one statement inside each branch but you can use the pass command while stubbing if x < 0: pass #Handle neg values Python Mini-Course: Day 2 - Lesson 7

  11. Notes on conditionals • You can have as many elif branches as you need • This replaces the select and switch commands in other languages • Conditionals can be nested • Example: nested.py Python Mini-Course: Day 2 - Lesson 7

  12. Complex condition statements • Use Boolean logic operators and, or, and not to chain together simple conditions • Example: boolean.py Python Mini-Course: Day 2 - Lesson 7

  13. The for loop • Used to repeat a section of code a set number of times • Syntax: for target in sequence: do_statements Python Mini-Course: Day 2 - Lesson 7

  14. Example: (power.py) def power(base, exponent): val = base for x in range(1,exponent): val = val * base print val power(3,4) Python Mini-Course: Day 2 - Lesson 7

  15. Example: (power.py) def power(base, exponent): val = base for x in range(1,exponent): print x, val val = val * base print val Python Mini-Course: Day 2 - Lesson 7

  16. Example: (power.py) def power(base, exponent): val = base for x in range(1,exponent): print x, val val = val * base print val Python Mini-Course: Day 2 - Lesson 7

  17. Exercises • Find a major bug in the power function and correct it • Add a docstring to the power function • Add type checking and value checking to the power function Python Mini-Course: Day 2 - Lesson 7

More Related