ProgFund Lect Week 4
ProgFund Lect Week 4
(SWE – 102)
Selections
Boolean Data Types
Often in a program you need to compare two values,
such as whether i is greater than j. There are six
comparison operators (also known as relational
operators) that can be used to compare two values.
The result of the comparison is a Boolean value: True
or False.
b = (1 > 2)
2
Comparison Operators
Operator Name
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
3
Comparison Operators
x=5
y=8
Output
print("x == y:", x == y) x == y: False
print("x != y:", x != y) x != y: True
print("x < y:", x < y) x < y: True
print("x > y:", x > y) x > y: False
print("x <= y:", x <= y) x <= y: True
print("x >= y:", x >= y) x >= y: False
4
if Statements
Python has several types of selection
statements:
one-way if statements,
two-way if-else statements,
multi-way if-elif-else statements and
nested if statements,
conditional expressions
5
One-way if Statements
if radius >= 0:
if boolean-expression: area = radius * radius * 3.14159
statement(s)
print("The area for the circle of radius“,
radius, "is“, area)
A one-way if
False
radius >= 0?
False statement
boolean-expression
executes the
True True statements if
Statement(s) area = radius * radius * 3.14159
print("The area for the circle of ",
the condition
"radius", radius, "is", area) is true.
(a) (b)
6
Note
if i > 0: if i > 0:
print("i is positive") print("i is positive")
True False
boolean-expression
Statement(s) for the true case Statement(s) for the false case
8
if...else Example
if radius >= 0:
area = radius * radius * math.pi
print("The area for the circle of radius", radius, "is", area)
else:
print("Negative input")
9
Practice Question
Write a Python program to user integer value
from the user at runtime. The programs
prints whether the given input number is
Odd or Even Number?
Output
Enter an Integer Number = 6
>> Number 6 is Even
Enter an Integer Number = 45
>> Number 45 is Odd
10
Multiway if-elif-else statement
Suppose score is 70.0 The condition is false
11
Multiway if-elif-else statement
Suppose score is 70.0 The condition is false
12
Multiway if-elif-else statement
Suppose score is 70.0 The condition is true
13
Multiway if-elif-else statement
Suppose score is 70.0 grade is C
14
Multiway if-elif-else statement
Suppose score is 70.0 Exit the if statement
15
Multiway if-elif-else statement
Day = int(input(“Enter a number between 1 to 7 to display weekday=“))
If (Day==1):
print(“Monday”)
elif (Day==2):
print(“Tuesday”)
elif (Day==3):
print(“Wednesday”)
elif (Day==4):
print(“Thursday”)
elif (Day==5):
print(“Friday”)
elif (Day==6):
print(“Saturday”)
elif (Day==7):
print(“Sunday”)
else:
print(“Invalid Day Entered!!!”) 16
Common Errors
Most common errors in selection statements
are caused by incorrect indentation. Consider
the following code in (a) and (b).
17
Logical Operators
Operator Description
18
Truth Table for Operator not
p not p Example (assume age = 24, gender = 'F')
True False not (age > 18) is False, because (age > 18) is True.
False True not (gender == 'M') is True, because (grade == 'M') is False.
19
Truth Table for Operator and
p1 p2 p1 and p2 Example (assume age = 24, gender = 'F')
False False False (age > 18) and (gender == 'F') is True, because (age
False True False > 18) and (gender == 'F') are both True.
True False False (age > 18) and (gender != 'F') is False, because
(gender != 'F') is False.
True True True
20
Truth Table for Operator or
p1 p2 p1 or p2 Example (assume age = 24, gender = 'F')
False False False (age > 34) or (gender == 'F') is true, because (gender
False True True == 'F') is True.
True False True (age > 34) or (gender == 'M') is False, because (age >
True True True 34) and (gender == 'M') are both Talse.
21
Boolean/Relational/Logical Expressions
a=6
b=7 Output
c = 42
print(1, a == 6) 1 True
print(2, a == 7) 2 False
print(3, (a == 6 and b == 7)) 3 True
print(4, (a == 7 and b == 7)) 4 False
print(5, not (a == 7 and b == 7)) 5 True
print(6, (a == 7 or b == 7)) 6 True
print(7, a == 7 or b == 6) 7 False
print(8, not (a == 7 and b == 6)) 8 True
print(9, not (a == 7 and b == 6)) 9 False
22
Logical and operator
number = 5
If ((number>0) and (number<11)):
print(“Number between 1 – 10”)
elif((number>10) and (number<21)):
print(“Number between 11– 20”)
elif((number>20) and (number<31)):
print(“Number between 21– 30”)
else:
print(“Number greater than 30”)
Output
>> Number between 1 - 10
23
Logical or operator
a = 10
b = -10
Output
>> Either of the number is greater than 0
24
Practice Question
Write a python program to take a character
input from the user using input() function. The
program then determines whether the given
character Is VOWEL or CONSONANT
Output
Enter a character : A
>> It is a Vowel
Enter a character : D
>> It is a Consonent
25
Conditional Operator
if x > 0:
y=1
else:
y = -1
is equivalent to
y = 1 if x > 0 else -1
26
Conditional Operator
if num % 2 == 0:
print(str(num) + “is even”)
else:
print(str(num) + “is odd”);
27