Transcript
Page 1: Chapter 9  IF Statement

Chapter 9 IF Statement

Dr. Bernard Chen Ph.D.University of Central Arkansas

Spring 2012

Page 2: Chapter 9  IF Statement

Outline

Logic expression General concept behind the Python

statement syntax IF statement

Page 3: Chapter 9  IF Statement

Some Logic Expression

Computer only understand ‘1’ and ‘0’

In computer logic: 1: true 0: false

Page 4: Chapter 9  IF Statement

Some Logic Expression

Equal: “==”

NOT Equal: “!=”

Greater: “>”, “>=”

Less than: “<”, “<=”

Page 5: Chapter 9  IF Statement

Some Logic Expression example

>>> a=10>>> b=10>>> a==b

>>> a=10>>> b=5>>> a==b

Page 6: Chapter 9  IF Statement

Some Logic Expression example

>>> a=10>>> b=10>>> a!=b

>>> a=10>>> b=5>>> a!=b

Page 7: Chapter 9  IF Statement

Some Logic Expression example

>>> a=10>>> b=10>>> a>=b

>>> a=10>>> b=5>>> a<b

Page 8: Chapter 9  IF Statement

Outline

Logic expression General concept behind the Python

statement syntax IF statement

Page 9: Chapter 9  IF Statement

Python’s Syntax

In general, Python has a simple syntax: Block and statement boundaries are

detected automatically: python use indention of statement under a header to group the statement

Header --- ‘:’

Page 10: Chapter 9  IF Statement

Outline

Logic expression General concept behind the Python

statement syntax IF statement

Simple If statement If... else statement If… else if … else statement

Page 11: Chapter 9  IF Statement

If Statement

The main statement used for selecting from alternative actions based on test results

It’s the primary selection tool in Python and represents the Logic process

Page 12: Chapter 9  IF Statement

Simple If statement

It takes the form of an if test

if <test>: <statement>

Page 13: Chapter 9  IF Statement

Simple If statement if 4>3: print “it is true that 4>3”

if 1: print “true”

a=10 if a==10:

print “a=10”

Page 14: Chapter 9  IF Statement

Truth Statement

In Python: True means any nonzero number or

nonempty object False means not true: a zero number

of empty object

Page 15: Chapter 9  IF Statement

If Statement “login” example

Page 16: Chapter 9  IF Statement

If... else statement

It takes the form of an if test, and ends with an optional else block

if <test>: <statement1> else: <statement2>

Page 17: Chapter 9  IF Statement

If Statement “login” example

Page 18: Chapter 9  IF Statement

If... else statement

>>> a=10>>> b=5>>> if a>b: print “a is larger” else: print “b is larger”

Page 19: Chapter 9  IF Statement

If... else statement a=10 if a==10:

print “a=10” else: print “a is not equal to 10”

(also try >, < )

Page 20: Chapter 9  IF Statement

If… else if … else statement It takes the form of an if test, followed

by one or more optional elif tests, and ends with an optional else block

if <test1>: <statement1>elif <test2>:

<statement2>else: <statement3>

Page 21: Chapter 9  IF Statement

If… else if … else statement

a=10 if a>10:

print “a > 10” elif a==10:

print “a = 10” else: print “a < 10”

Page 22: Chapter 9  IF Statement

If… else if … else statement examplenumber = 23guess = int(raw_input('Enter an integer : '))

if guess == number:print 'Congratulations, you guessed it.'print "(but you do not win any prizes!)"

elif guess < number:print 'No, it is a little higher than that'

else:print 'No, it is a little lower than that'

Page 23: Chapter 9  IF Statement

In class practiceimport randomnumber=random.randint(1,5)

guess = int(raw_input('Enter an integer : '))

if guess == number:print 'Congratulations, you guessed it.'print "(but you do not win any prizes!)"

elif guess < number:print 'No, it is a little higher than that'

else:print 'No, it is a little lower than that'

print 'The answer is:',number

Page 24: Chapter 9  IF Statement

Some More Logic Expression And (True if both are true)

Examples:>>> 5>4 and 8>7True>>> 5>4 and 8>9False

A B A and B

0 0 0

0 1 0

1 0 0

1 1 1

Page 25: Chapter 9  IF Statement

Exampletemp=int(raw_input("Please enter today's tempeture:"))rain=int(raw_input("Is is raining? 1:yes, 0:no"))

if temp>86 and rain==0: print "Today is a hot sunny day"elif temp>86 and rain==1: print "Today is a hot raining day"elif temp<32 and rain==0: print "Today is a cold sunny day"elif temp<32 and rain==1: print "Today is a cold raining day"else: print "Today's weather is not special"

Page 26: Chapter 9  IF Statement

Some More Logic Expression Or(True if either one is

true)

Examples:>>> 5>4 or 8>7True>>> 5>4 or 8>9True

A B A or B

0 0 0

0 1 1

1 0 1

1 1 1

Page 27: Chapter 9  IF Statement

Example

a=10b=20

if a%2==0 and b%2==0: print "Both a and b are even numbers"elif a%2==0 or b%2==0: print "Either a or b is an even number"else: print "Both a and b are odd numbers"

Page 28: Chapter 9  IF Statement

Nested IF statement

Sometimes you may want to check one condition after another condition. In such situation, we call it “Nested IF Statement”

The Code would looks something like:

Page 29: Chapter 9  IF Statement

Nested IF statement

Page 30: Chapter 9  IF Statement

Example:deposit=int(raw_input('Clerk: How much do you want to deposit? '))

if deposit > 100000: print "Wow, that's a lot of money" if deposit < 500000: print "I need to call my manager" elif deposit < 1000000: print "Let me open an VIP room for you" else: print "Can you merry me?"

elif deposit > 1000: print "Sure, let me do it right now, do you want to consider a CD"elif deposit > 1: print "Sure, let me do it right now"else: print "Are you kidding me, I am busy right now..."


Top Related