0% found this document useful (0 votes)
5 views

ProgFund Lect Week 4

The document discusses different types of selection statements in Python including if, if-else, if-elif-else statements and logical operators. It explains how to use comparison, logical and conditional operators to write conditional statements. Examples are given to check if a number is even or odd, between a range, or a vowel or consonant based on conditional logic.

Uploaded by

Battle Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ProgFund Lect Week 4

The document discusses different types of selection statements in Python including if, if-else, if-elif-else statements and logical operators. It explains how to use comparison, logical and conditional operators to write conditional statements. Examples are given to check if a number is even or odd, between a range, or a vowel or consonant based on conditional logic.

Uploaded by

Battle Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Programming Fundamentals

(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")

(a) Wrong (b) Correct

• The statement(s) must be


indented at least one space to the
right of the if keyword and each
statement must be indented using
the same number of spaces.
• For consistency, we indent it four
spaces in this book.
7
The Two-way if Statement
if boolean-expression:
statement(s)-for-the-true-case
else:
statement(s)-for-the-false-case

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

if score >= 90.0:


grade = 'A'
elif score >= 80.0:
grade = 'B'
elif score >= 70.0:
grade = 'C'
elif score >= 60.0:
grade = 'D'
else:
grade = 'F'

11
Multiway if-elif-else statement
Suppose score is 70.0 The condition is false

if score >= 90.0:


grade = 'A'
elif score >= 80.0:
grade = 'B'
elif score >= 70.0:
grade = 'C'
elif score >= 60.0:
grade = 'D'
else:
grade = 'F'

12
Multiway if-elif-else statement
Suppose score is 70.0 The condition is true

if score >= 90.0:


grade = 'A'
elif score >= 80.0:
grade = 'B'
elif score >= 70.0:
grade = 'C'
elif score >= 60.0:
grade = 'D'
else:
grade = 'F'

13
Multiway if-elif-else statement
Suppose score is 70.0 grade is C

if score >= 90.0:


grade = 'A'
elif score >= 80.0:
grade = 'B'
elif score >= 70.0:
grade = 'C'
elif score >= 60.0:
grade = 'D'
else:
grade = 'F'

14
Multiway if-elif-else statement
Suppose score is 70.0 Exit the if statement

if score >= 90.0:


grade = 'A'
elif score >= 80.0:
grade = 'B'
elif score >= 70.0:
grade = 'C'
elif score >= 60.0:
grade = 'D'
else:
grade = 'F'

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).

radius = -20 radius = -20

if radius >= 0: if radius >= 0:


area = radius * radius * 3.14 area = radius * radius * 3.14
print("The area is", area) print("The area is", area)
(a) Wrong (b) Correct

17
Logical Operators
Operator Description

not logical negation


and logical conjunction
or logical disjunction

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

if((a > 0) or (b > 0)):


print("Either of the number is greater than 0")
else:
print("No number is greater than 0")

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

expression1 if boolean-expression else expression2

26
Conditional Operator
if num % 2 == 0:
print(str(num) + “is even”)
else:
print(str(num) + “is odd”);

print("number is even" if (number % 2 == 0)


else "number is odd")

27

You might also like