Transcript ppt

Python (yay!)
November 16, Unit 7
Recap
• We can store values in variables using an
assignment statement
>>>x = 4 + 8
• We can get input from the user using raw_input()
>>>name = raw_input(“Enter your name”)
• We can change the data from one type to
another
>>>num = int(raw_input(“Enter a number”))
Conditional Statements
• So far the programs we’ve 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
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”
If Statement, cont.
num = int(raw_input(“Enter a number less
than 10”))
if num>10:
print “That number is bigger than 10. You
can’t follow directions”
print “Thanks for playing!”
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
In-Class Example
• Using if statements
Boolean Expressions
• When we use an if statement, the result of the
expression is either true or false
– if 4<3: (false)
– if 3<4: (true)
• The result of the expression must be true or
false
• Otherwise it doesn’t make sense
• These are called boolean expressions
• The two boolean values are “true” and “false”
Boolean Values, cont.
• Boolean values in python (like most languages)
are stored as integers
• 0 represents false
– 4<3 as an expression has the value 0
• Any other integer (usually 1) represents true
– 3<4 has the value of 1
• You can actually write code like:
if 1:
……
if 5:
…….
Boolean Expressions
• We’re pretty used to seeing <, <=, >, >= 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:
Else Clause
• In XSL we can use <xsl:choose>, <xsl:when>,
and <xsl:otherwise>
– 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 3<4 then print “woohoo”
else print “darn. I don’t know math”
• Code inside the else clause will be executed
only if the expression in the if statement
evaluates to false
If Else Example
guess = int(raw_input(“Pick a number
between 1 and 10”))
if guess<=10:
print “Good job!”
else:
print “Can’t you follow directions!?”
In-Class Example
• Example using if/else
• Example using strings
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)
Elif Example
if guess== number:
print “you guessed it!”
elif guess< number:
print “that’s too low”
else:
print “that’s too big!”
In-Class Example with elif
• Changing the number guessing game to
use elif
• Inventory with elif
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
In-Class nested If example
• Checking to be sure the number is within
range
Random Number
• So far we have had to pre-select a number for
the user to guess
– num = 7
– It’s 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
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
In-Class Example
• Adding a random number to our guessing
game
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
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
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
• We’ll be covering many more modules
In-Class Example
• Import the string module
• Use it to uppercase an entire string
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