ProgFund Lect Week 4

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Programming Fundamentals

Department of CS & IT

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
C omparison O perators
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- area = radius * radius * 3.14159
expression:
print("The area for the circle of
statement(s)
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
"radius", radius, "is", area) condition 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
S tatement
if boolean-expression:
statement(s)-for-the-true-case
else:
statement(s)-for-the-false-case

Tru Fals
e boolean-expression e

Statement(s) for the Statement(s) for the


true case false case

8
if...else
E0:xample
if radius >=
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 N umber = 6
>> Number 6 is Even
Enter an Integer N umber = 45
>> Number 45 is Odd
10
num = 3
if not num > 0:
print(num, "is a negative
number.")
else:
print(num, "is a positive
number.")
Output :
3 is a positive number.
Write a python program to check whether you
are eligible to vote or not?
Your program should get the age of the voter
from the user and if their age is 18 and above
let them vote otherwise deny them from voting.
elif statement:
age = int(input("Enter your age:"))
if age <= 12:
print("You are a kid!")
elif age == 18:
print("you are 18 and in adulthood.")
elif age >= 19:
print("You are above 18.")
# Possible output

Enter your age:10


You are a kid!
=================
Enter your age:18
you are 18 and in adulthood.
=================
Enter your age:20
You are above 18.
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'

15
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'

16
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'

17
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 grade
score =>= 70.0:
'C'
elif score >= 60.0:
grade = 'D'
else:
grade = 'F'

18
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'

19
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(“W ednesday
”) elif (Day==4):
print(“Thursday
”) elif (Day==5):
print(“Friday
”) elif (Day==6):
print(“Saturday
”) elif (Day==7):
print(“
Sunday”)
print(“Invalid D ay 16
else:
Entered!!!”)
Com m on E rrors
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

21
Logical Operators
Operator Description

not logical negation


and logical conjunction
or logical disjunction

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

23
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 > 18) and (gender == 'F') are both True.
False True
True False False (age > 18) and (gender != 'F') is False, because
True (gender != 'F') is False.
True True

24
Truth Table for O perator
p1 or p2 p1 or p2 Example (assume age = 24, gender = 'F')

False False False (age > 34) or (gender == 'F') is true, because (gender
True True == 'F') is True.

False True (age > 34) or (gender == 'M') is False, because (age >
True False True 34) and (gender == 'M') are both Talse.

True True

25
Boolean/Relational/Logical Expressions

a=6
b=7 Output
c = 42
print(1, a == 6) 1 True
print(2, a == 2 False
7) 3 True
print(3, (a == 6 and b == 7)) 4 False
print(4, (a == 7 and b == 7)) 5 True
print(5, not (a == 7 and b == 6 True
7)) print(6, (a == 7 or b == 7)) 7 False
print(7, a == 7 or b == 6) 8 True
print(8, not (a == 7 and b == 9 False
6)) print(9, (a == 7 and b ==
6)) 26
Logical and operator
number = 5
If ((number>0) and (number<11)):
print(“N umber between 1 –
10”)
elif((number>10) and (number<21)):
print(“N umber between 11–
20”)
elif((number>20) and (number<31)):
print(“Number between
21– 30”)
else:
print(“Number greater than 30”)

Output 27
Logical or operator
a = 10
b = -10

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


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

Output
>> Either of the number is greater than 0

28
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 VO W E L or
CONSONANT

Output
Enter a character :A
>> It is a Vowel
Enter a character :
D 29
Conditional Operator
if x >
0:
y=1
else:
y=-
1

is
equiva
lent to

y=1
if x > 30
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")
31

You might also like