Download presentation
Presentation is loading. Please wait.
Published byLee Lawrence Modified over 9 years ago
1
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
2
Outline Logic expression General concept behind the Python statement syntax IF statement
3
Some Logic Expression Computer only understand ‘1’ and ‘0’ In computer logic: 1: true 0: false
4
Some Logic Expression Equal: “==” NOT Equal: “!=” Greater: “>”, “>=” Less than: “<”, “<=”
5
Some Logic Expression example >>> a=10 >>> b=10 >>> a==b >>> a=10 >>> b=5 >>> a==b
6
Some Logic Expression example >>> a=10 >>> b=10 >>> a!=b >>> a=10 >>> b=5 >>> a!=b
7
Some Logic Expression example >>> a=10 >>> b=10 >>> a>=b >>> a=10 >>> b=5 >>> a<b
8
Outline Logic expression General concept behind the Python statement syntax IF statement
9
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 --- ‘:’
10
Outline Logic expression General concept behind the Python statement syntax IF statement Simple If statement If... else statement If… else if … else statement
11
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
12
Simple If statement It takes the form of an if test if :
13
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”
14
Truth Statement In Python: True means any nonzero number or nonempty object False means not true: a zero number of empty object
15
If Statement “login” example
16
If... else statement It takes the form of an if test, and ends with an optional else block if : else:
17
If Statement “login” example
18
If... else statement >>> a=10 >>> b=5 >>> if a>b: print “a is larger” else: print “b is larger”
19
If... else statement a=10 if a==10: print “a=10” else: print “a is not equal to 10” (also try >, < )
20
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 : elif : else:
21
If… else if … else statement a=10 if a>10: print “a > 10” elif a==10: print “a = 10” else: print “a < 10”
22
If… else if … else statement example number = 23 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'
23
In class practice import random number=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
24
Some More Logic Expression And (True if both are true) Examples: >>> 5>4 and 8>7 True >>> 5>4 and 8>9 False ABA and B 000 010 100 111
25
Example temp=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"
26
Some More Logic Expression Or (True if either one is true) Examples: >>> 5>4 or 8>7 True >>> 5>4 or 8>9 True ABA or B 000 011 101 111
27
Example a=10 b=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"
28
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:
29
Nested IF statement
30
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..."
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.