Python Mini-Course - PowerPoint PPT Presentation

About This Presentation
Title:

Python Mini-Course

Description:

Conditionals and Loops Python Mini-Course University of Oklahoma Department of Psychology Python Mini-Course: Day 2 - Lesson 7 4/18/09 * Lesson objectives Use ... – PowerPoint PPT presentation

Number of Views:119
Avg rating:3.0/5.0
Slides: 18
Provided by: troys9
Learn more at: https://www.ou.edu
Category:

less

Transcript and Presenter's Notes

Title: Python Mini-Course


1
Day 2 Lesson 7Conditionals and Loops
  • Python Mini-Course
  • University of Oklahoma
  • Department of Psychology

2
Lesson objectives
  1. Use conditional statements in Python, including
    the if, ifelse, and ifelif statements
  2. Use Boolean logic to create complex conditionals
  3. Use the for statement to create loops

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

4
ExampleChecking user input
  • x raw_input("X? ")
  • if x.isdigit()
  • print "You input a number"

5
Alternative execution
  • Syntax
  • if condition
  • do_something
  • else
  • do_alternative

6
ExampleChecking user input
  • x raw_input("x? ")
  • if x.isdigit()
  • print "You input a number"
  • else
  • print "Please input a number\
  • next time"

7
ExampleAvoiding division by zero
  • x int(raw_input("x? "))
  • y int(raw_input("y? "))
  • if y ltgt 0
  • print x / y
  • else
  • print "Attempted division by\
  • zero"

8
Chained conditionals
  • Syntax
  • if condition
  • do_something
  • elif condition
  • do_alternative1
  • else
  • do_alternative2

9
Example
  • x int(raw_input("x? "))
  • y int(raw_input("y? "))
  • if x lt y
  • print 'x is less than y'
  • elif x gt y
  • print 'x is greater than y'
  • else
  • print 'x and y are equal'

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 lt 0
  • pass Handle neg values

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

12
Complex condition statements
  • Use Boolean logic operators and, or, and not to
    chain together simple conditions
  • Example boolean.py

13
The for loop
  • Used to repeat a section of code a set number of
    times
  • Syntax
  • for target in sequence
  • do_statements

14
Example (power.py)
  • def power(base, exponent)
  • val base
  • for x in range(1,exponent)
  • val val base
  • print val
  • power(3,4)

15
Example (power.py)
  • def power(base, exponent)
  • val base
  • for x in range(1,exponent)
  • print x, val
  • val val base
  • print val

16
Example (power.py)
  • def power(base, exponent)
  • val base
  • for x in range(1,exponent)
  • print x, val
  • val val base
  • print val

17
Exercises
  1. Find a major bug in the power function and
    correct it
  2. Add a docstring to the power function
  3. Add type checking and value checking to the power
    function
Write a Comment
User Comments (0)
About PowerShow.com