Python%20(yay!) - PowerPoint PPT Presentation

About This Presentation
Title:

Python%20(yay!)

Description:

If the number is less than 5, print 'that number is less than 5' If the name is 'C.S.Lewis', print 'this book is by C.S.Lewis' If Statement, cont. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 27
Provided by: SarahM100
Category:
Tags: print | python | yay

less

Transcript and Presenter's Notes

Title: Python%20(yay!)


1
Python (yay!)
  • November 16, Unit 7

2
Recap
  • We can store values in variables using an
    assignment statement
  • gtgtgtx 4 8
  • We can get input from the user using raw_input()
  • gtgtgtname raw_input(Enter your name)
  • We can change the data from one type to another
  • gtgtgtnum int(raw_input(Enter a number))

3
Conditional Statements
  • So far the programs weve written are pretty
    boring
  • Like with XSL we can use conditional statements
    to do more
  • Basically conditional statements check to see if
    something is true.
  • If it is true, some code is executed
  • Perhaps if its not true, different code is
    executed
  • Simplest conditional statement is the if
    statement

4
The If Statement
  • The if statement in Python works just like the if
    statement in XSL
  • The syntax is different
  • Concept is the same
  • If the number is less than 5, print that number
    is less than 5
  • If the name is C.S.Lewis, print this book is
    by C.S.Lewis

5
If Statement, cont.
  • num int(raw_input(Enter a number less than
    10))
  • if numgt10
  • print That number is bigger than 10. You cant
    follow directions
  • print Thanks for playing!

6
If, cont.
  • The syntax for the if statement is as follows
  • if expression
  • code
  • The code you want executed if the expression is
    true must be indented
  • This is the body of your if statement
  • Spacing is important in python
  • You should indent consistently
  • Convention is to indent 4 spaces (use the tab)
  • When you stop indenting, the body of the if
    statement is considered to be done
  • The code after the indention will execute no
    matter what

7
In-Class Example
  • Using if statements

8
Boolean Expressions
  • When we use an if statement, the result of the
    expression is either true or false
  • if 4lt3 (false)
  • if 3lt4 (true)
  • The result of the expression must be true or
    false
  • Otherwise it doesnt make sense
  • These are called boolean expressions
  • The two boolean values are true and false

9
Boolean Values, cont.
  • Boolean values in python (like most languages)
    are stored as integers
  • 0 represents false
  • 4lt3 as an expression has the value 0
  • Any other integer (usually 1) represents true
  • 3lt4 has the value of 1
  • You can actually write code like
  • if 1
  • if 5
  • .

10
Boolean Expressions
  • Were pretty used to seeing lt, lt, gt, gt in
    conditional statements
  • They make sense and read just like regular
    English
  • What if we want to check if something is equal to
    something else?
  • if 4-3 1
  • This is an assignment statement
  • Does not check to see if 4 minus 3 equals 1
  • We need to use the operator
  • if 4-3 1
  • If we want to check if two items are not equal
    use the ! operator
  • - if 4-2!1

11
Else Clause
  • In XSL we can use ltxslchoosegt, ltxslwhengt, and
    ltxslotherwisegt
  • This provides us with a sort of if, else if, else
    if.else structure
  • Python has something similar
  • We can use the else clause to provide a sort of
    if not
  • If 3lt4 then print woohoo
  • else print darn. I dont know math
  • Code inside the else clause will be executed only
    if the expression in the if statement evaluates
    to false

12
If Else Example
  • guess int(raw_input(Pick a number between 1
    and 10))
  • if guesslt10
  • print Good job!
  • else
  • print Cant you follow directions!?

13
In-Class Example
  • Example using if/else
  • Example using strings

14
elif block Else if
  • The if/else structure is pretty useful
  • There is one more component to this structure
    the else if block
  • Basically we can now write code that reads
    something like
  • if, else if, else if, ..,else
  • To use an else if structure we need the word
    elif
  • elif requires an expression (just like if)

15
Elif Example
  • if guess number
  • print you guessed it!
  • elif guesslt number
  • print thats too low
  • else
  • print thats too big!

16
In-Class Example with elif
  • Changing the number guessing game to use elif
  • Inventory with elif

17
We Can Nest if Statements
  • We can nest if statements inside other if
    statements
  • Or inside elif statements
  • Or inside else statements
  • Basically anywhere
  • Remember that nested ifs are only executed if the
    outer ifs are evaluated as true

18
In-Class nested If example
  • Checking to be sure the number is within range

19
Random Number
  • So far we have had to pre-select a number for the
    user to guess
  • num 7
  • Its the same every time
  • Pretty boring once you guess the number
  • It would be better if every time we ran the
    program it chose a random number for us to guess
  • In order to use random numbers it requires two
    parts
  • Importing the random module
  • Using a function from the random module to
    generate our random number

20
Random Numbers, cont.
  • import random
  • num random.randint(1,10)
  • Import tells python to include the random module
  • There are lots of modules
  • More on this later
  • random.randint(1,10)
  • In the random module, use the randint function to
    select a random number between 1 and 10
  • This is assigned to the variable num
  • random.randint(5, 50)
  • Select a random integer between 5 and 50

21
In-Class Example
  • Adding a random number to our guessing game

22
Libraries
  • It would stink if every time we needed a random
    number we had to write the code to produce one
  • Or how about something like calculating the sin
    of a number
  • Or the square root
  • With most programming languages some functions
    are included with the language
  • These are usually grouped into libraries
  • We only have to import the libraries or modules
    we need
  • If we imported every module it would take a long
    time to run
  • In python, all of the prepackaged functions are
    considered to be part of the python library
  • The individual parts we will import will be
    modules or libraries
  • Terms are pretty much interchangeable
  • Slightly different terminology from other
    languages

23
Modules
  • When we needed the random number we had to import
    the random module
  • The random module contains a function called
    randint() which is what actually gives us the
    random number
  • The syntax for importing a module is
  • import moduleName
  • We only import the modules we need

24
Accessing Functions in Modules
  • To use a function found in a module we have to
    use the following syntax
  • moduleName.functionName()
  • There are a lot of modules
  • There is a link to the reference on the course
    page
  • Some of the common ones are random, math, and
    cmath
  • Well be covering many more modules

25
In-Class Example
  • Import the string module
  • Use it to uppercase an entire string

26
Questions?
  • What you should get from this lecture
  • If statements
  • else clause
  • elif clause
  • Nesting if statements
  • How to produce a random integer
  • How to access functions in modules
Write a Comment
User Comments (0)
About PowerShow.com