+ All Categories
Home > Documents > Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Date post: 31-Mar-2015
Category:
Upload: elaina-rounsavall
View: 229 times
Download: 1 times
Share this document with a friend
23
Selection in Python If statements
Transcript
Page 1: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Selection in Python

If statements

Page 2: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Control Structures

Sequence Selection Repetition Module

Page 3: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Selection

If or if / else statement choosing between mutually

exclusive possibilities Two forms

see next slide

Page 4: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Selection

Two forms if logical expression:

statement if logical expression:

statement

else:

statement

Page 5: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Logical Expressions

Relational Operators==, !=, <=, >=, >, <

They are binary operators – have two operands

work on any type of data, be careful about matching types

produce a bool result (True or False)

Page 6: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Syntax of if statement

if logical expression:

statement Indentation required if expression is true, then statement is

executed if expression is false, statement is NOT

executed

Page 7: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Syntax of if statement

if x > 0:

y = sqrt(x) if x > y:

t = x

x = y

y = t

Page 8: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

If-Then Statement

Determine whether or not to execute a statement (which can be a single statement or an entire block)

TRUE

FALSEstatement

expression

Page 9: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Examples

output larger of two numbers don't allow sqrt of negative number don't allow overflow determining even or odd give student another chance at a question

Page 10: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Visual Aid

Decision tree “if person is 18 or over and state is KY, they can

have regular license” “if person is 16 or over and under 18 and state is

TN or VA, they can have learner’s permit” “if person is under 16, in any state, they can’t

have license” “if person is over 65 in VA, they have modified

license”

Page 11: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Logical Operators

and or not and, or are binary operators, not is unary used to combine bool values produce a bool result truth tables operator precedence

not then and then or not, and, or are very low, below other operators

Page 12: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Precedence of operators

See http://docs.python.org/py3k/reference/expressions.html#evaluation-order

Note that this table is “upside down”, lowest precedence is on top!

Page 13: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Converting English to logic

"0 is less than x is less than 5" "x is 5 or 6" "x is bigger than 5 and less than 10“

5 < x < 10 IS valid in Python! impossible situations "dead code"

always true – “x < x + 1” “x > 5 or x < 8” always false – “x < 5 and x > 10”

Page 14: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Write an expression for each

taxRate is over 25% and income is less than $20000

temperature is less than or equal to 75 or humidity is less than 70%

age is over 21 and age is less than 60

age is 21 or 22

Page 15: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Some Answers

(taxRate > .25) and (income < 20000)

(temperature <= 75) or (humidity < .70)

(age > 21) and (age < 60)

(age == 21) or (age == 22)

Page 16: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Nested if's

the statement to be executed in an if statement can be another if

an else branch goes with the if which matches its indentation

Page 17: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

what’s the difference?

if a > 5:

if b < 10:

print(“green”)

else:

print(“blue”)

if a > 5:

if b < 10:

print(“green”)

else:

print(“blue”)

Page 18: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

if .. else provides two-way selection

between executing one of 2 clauses (the if clause or the else clause)

TRUE FALSE

if clause else clause

expression

Page 19: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Example Given x, y on Cartesian plane, which quadrant is it

in? (I, II, III, IV)if x > 0:

if y > 0:

quadrant = 1

else:

quadrant = 4

else:

if y > 0: # NOT a redundant test!

quadrant = 2

else:

quadrant = 3

Page 20: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Another form of nested if's

if condition:statement;

elif condition: statement elif condition: statement else:

statement# good for mutually exclusive and exhaustive

#conditions

Page 21: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Comparing Strings

Two objects of type string (or a string object and a C string) can be compared using the relational operators

A character-by-character comparison is made using the

ASCII character set values

If all the characters are equal, then the 2 strings are equal. Otherwise, the string with the character with smaller ASCII value is the “lesser” string

space < digits < uppercase < lowercase “ “ < “0” < … < “9” < “A” < “B” < … < “Z” < “a” < … < “z”

Page 22: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

string myState;

string yourState;

myState = “Texas”;

yourState = “Maryland”;

Expression Value

myState == yourState false

myState > yourState true

myState == “Texas” true

myState < “texas” true

Page 23: Selection in Python If statements. Control Structures Sequence Selection Repetition Module.

Comparing Real Values

Do not compare floating point values for equality, compare them for near-equality.

a = 0for i in range(5000000):

a = a + 0.001;

if abs(a – 5000) < 0.00001: print( “They are close enough!”)


Recommended