Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 1 of 23
Go Back
Full Screen
Quit
Conditional Expressions
and Decision Statements
June 1, 2015Brian A. Malloy
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 2 of 23
Go Back
Full Screen
Quit
1. Modulus
• We have introduced 5 operators for addi-tion, subtraction, multiplication, division,and exponentiation:+, -, *, /, **• We now introduce a very useful operator
called modulus, which returns the remain-der after division.
• Use operator for conditional expressions
• The modulus operator is %• For example, 10%3=1, 13%2=1, 1%2=1
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 3 of 23
Go Back
Full Screen
Quit
1.1. What good is modulus?
• Useful for lots of conditions:
– Determine if a number is even
– Limit the range of a number
– Wrapping around
– Finding the value of a digit
– . . .
• How to determine even/odd:
– Assume number is stored in x
– if x % 2 is 0, then x is even,otherwise x is odd.
– Example: 5 % 2 == 1 ⇒ 5 is odd
– Example: 6 % 2 == 0 ⇒ 6 is even
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 4 of 23
Go Back
Full Screen
Quit
2. Boolean Values
• We have described three data types:integer, float, string.
• We now add a fourth data type, bool, whichevaluates to True or False.
• Named after mathematician, George Boole
• A boolean expression evaluates to eitherTrue or False.
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 5 of 23
Go Back
Full Screen
Quit
3. Boolean Expressions
• There are 6 boolean operators:==, !=, <, >, ≤, ≥• These operators are interpreted as follows:
(a) == compares two values; returns Trueif operands are equal, False otherwise.
(b) != compares two values; returns Trueif they are not equal, False otherwise
(c) etc.
• When used in a Python program, ≤ mustbe written <=, and ≥ must be written >=
• Examples:
(a) 3 < 3 is False, but
(b) 3 <= 3 is True
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 6 of 23
Go Back
Full Screen
Quit
3.1. Boolean Expressions in Python
• Can print the type of a Boolean expression:print type(True)<type ’bool’>
• Can print the value of Boolean expression:
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
>>> x = 5
>>> x != 7
True
>>> x >= 12
False
>>> x > 5
False
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 7 of 23
Go Back
Full Screen
Quit
3.2. Determine if a number is evenPython 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits"
>>> x = input("Type a number: ")
Type a number: 7
>>> x % 2
1
>>>
Since x % 2 is 1, this means x is odd
Explanation: for the above program: if the valueof x % 2 == 0 is True then the number is even,otherwise the number is odd.
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 8 of 23
Go Back
Full Screen
Quit
4. Decisions
• Much of the power of computer program-ming, and computers in general, derivesfrom the ability to make decisions and loop.
• Decision structures in Python, and otherlanguages, take many forms:
1. Conditional execution
2. Alternative execution
3. Chained conditionals
4. Nested Conditionals
• We’ll describe each decision structure next
• We’ll save loops for future presentations
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 9 of 23
Go Back
Full Screen
Quit
4.1. Conditional Execution
• Means that a sequence of 1 or more state-ments will execute only if a certain condi-tion is satisfied.
• Provides the ability to change the bahaviorof a program for each execution.
• Format:
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 10 of 23
Go Back
Full Screen
Quit
4.1.1. Conditional Execution: input
• Example, the following program:
– has output if x is greater than zero
– has no output if x is zero or negative
– Notice use of colon :
x = input("Type a number: ")
if x > 0:
print x, "is positive"
$ python if.py
Type a number: 34
34 is positive
$ python if.py
Type a number: -4
$ python if.py
Type a number: 0
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 11 of 23
Go Back
Full Screen
Quit
4.1.2. Conditional Execution: spaces
• In the following, if x > 0, then 2 statementsare executed, numbered (3) & (4)
• Spaces must be the same on, (3) & (4)
This one works:
(1) x = input("Type a number: ")
(2) if x > 0:
(3) print x, "is positive"
(4) print x, "is not zero or negative"
This one doesn’t work:
(1) x = input("Type a number: ")
(2) if x > 0:
(3) print x, "is positive"
(4) print x, "is not zero or negative"
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 12 of 23
Go Back
Full Screen
Quit
4.1.3. Conditional Execution: Flowchart
The flowchart on the title page of these slidesaptly describes conditional execution. That is,statement(s) are executed only if condition is true:
If condition is False, statement(s) are skipped,and execution goes straight to rest of program
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 13 of 23
Go Back
Full Screen
Quit
4.2. Alternative Execution
• The problem with Conditional Execution isthat statement(s) are only executed if thecondition is True.
• We may want output in either case:statement(s) if condition is True.statement(s) if condition is False.
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 14 of 23
Go Back
Full Screen
Quit
4.2.1. Alternative Execution: input
Now we get output in either case:
x = input("Type a number: ")
if x % 2 == 0:
print x, "is even"
else:
print x, "is odd"
$ python ifelse.py
Type a number: 4
4 is even
$ python ifelse.py
Type a number: 7
7 is odd
$ python ifelse.py
Type a number: 0
0 is even
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 15 of 23
Go Back
Full Screen
Quit
4.3. Chained Conditionals
• Use this if more than 2 possible outcomes
• Notice keywords elif and else
• Consider a trichotomy example:
1 x = input("Type a number: ")
2 if x > 0:
3 print x, "is greater than zero"
4 elif x == 0:
5 print x, "is zero"
6 else:
7 print x, "is less than zero"
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 16 of 23
Go Back
Full Screen
Quit
4.4. Nested Conditionals
• Can “nest” conditional within a conditional
• spacing is intended to make it clear whatgets executed
• But Nested Conditionals can be confusing
• Consider an alternative to trichotomy; state-ments 5–8 happen only if x == 0 is false.
1 x = input("Type a number: ")
2 if x == 0:
3 print x, "is zero"
4 else:
5 if x > 0:
6 print x, "is greater than zero"
7 else:
8 print x, "is less than zero"
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 17 of 23
Go Back
Full Screen
Quit
5. Logical Operators
• These include and, or, and not
• They can simplify conditional execution
• The following examples do the same thing;Which is easier to understand:
if x > 0:
if x < 10:
print x, "is a single digit number"
if x > 0 and x < 10:
print x, "is a single digit number"
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 18 of 23
Go Back
Full Screen
Quit
6. return statement• It’s possible, sometimes necessary, to re-
turn from function before last statement.
• Remember, division by 0 is undefined
1 def findAverage(sum, n):
2 if n <= 0:
3 return 0
4 return sum/n
5
6 print findAverage(25, 7)
7 print findAverage(25, 0)
3
0
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 19 of 23
Go Back
Full Screen
Quit
6.1. return statement
• If a function contains no return statement,there is always one implied at the end of afunction
• The following functions are the same:
1 def display(n):
2 print "n is:", n
1 def display(n):
2 print "n is:", n
3 return
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 20 of 23
Go Back
Full Screen
Quit
7. Now What?• Need to write programs
• Use conditionals/decisions tosolve problems
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 21 of 23
Go Back
Full Screen
Quit
7.1. Evaluate
• Try to evaluate the following expressionsin your head, then check your answer withPython interpreter:
1. >>> 5 % 2
2. >>> 9 % 5
3. >>> 15 % 12
4. >>> 12 % 15
5. >>> 6 % 6
6. >>> 0 % 7
7. >>> 7 % 0
• What happened with the last example?
• Explore modulus until you’re convinced youunderstand how it works
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 22 of 23
Go Back
Full Screen
Quit
7.2. True/False ⇒ 1/0
• What’s the output? Why?
1 if ’A’:
2 print ’yes’
3 else:
4 print ’no’
• What’s the output? Why?
1 if 1:
2 print ’yes’
3 else:
4 print ’no’
• 0 ⇒ False
• 1 ⇒ True, 2 ⇒ True, 3 ⇒ True, . . .
Modulus
Boolean Values
Boolean Expressions
Decisions
Logical Operators
return statement
Now What?
JJ II
J I
Slide 23 of 23
Go Back
Full Screen
Quit
7.3. Problems
• Are negative numbers True or False
• Write code to determine if:
1. A number is even
2. If a number is a multiple of 3?
3. If a number is a multiple of 17?
4. If user input is a digit?
5. If user input is a letter?
6. If x is off a screen that’s 640x480?
7. If user input is a vowel?
8. . . .