INTRODUCTION TO PYTHON
AEMON
CONDITIONAL STATEMENTS
• Boolean Expression:
A boolean expression is an expression that is either true or false. The following
examples use the operator ==, which compares two operands and produces
True if they are equal and False otherwise:
COMPARISON OPERATOR
x == y #x is equals to y
x != y # x is not equal to y
x>y # x is greater than y
x<y # x is less than y
x >= y # x is greater than or equal to y
x <= y # x is less than or equal to y
x is y # x is the same as y
x is not y # x is not the same as y
LOGICAL OPERATOR
• There are three logical operators: and, or, and not. The semantics (meaning)
of these operators is similar to their meaning in English.
• and operator
x > 0 and x < 10
is true only if x is greater than 0 and less than 10.
• or operator
n%2 == 0 or n%3 == 0 is true
if either of the conditions is true, that is, if the number is divisible by 2 or 3.
• not operator
not (x > y) is true
If x > y is false.
CONDITIONAL STATEMENTS
• The control flow of a program is the order in which its individual instructions
are executed.
• conditionals: if - else / elif and exception handling: try - except.
• The boolean expression after the if statement is called the condition. We end
the if statement with a colon character (:) and the line(s) after the if statement
are indented.
• If the logical condition is true, then the indented statement gets executed. If
the logical condition is false, the indented statement is skipped.
IF LOGIC
IF LOGIC
• The Python if conditional is similar to that of other languages (C++, etc). Its
syntax is: if condition:
# set of instructions in case condition is met
• Example:
IF-ELSE LOGIC
• And if we had programmed our second block differently, say only with one if
and one else : x=int(input("Write an integer x"))
y=int(input("Write another integer y"))
• Example: if x<y:
print("x is smaller than y")
else:
print("x is larger than or equal to y")
IF-ELSE LOGIC
• Write a program to find maximum between two numbers
• Write a program to find whether number is positive or negative
• Write a program to find whether number is odd or even
• Write a program to find whether a character is vowel or consonant
• Write a program to find whether a number is divisible by 5 and 11 or not
• Write a Python program that inputs an integer number entered by user, and
prints its double if it is less than 1000, or print half of it otherwise.
• Write a Python program that inputs two integer numbers, then checks if the
first number is divisible by the second number or not, and display an
appropriate message.
IF-ELIF-ELSE LOGIC
• Sometimes there are more than two possibilities and we need more than two
branches.
• elif tries to catch the flow if it did not satisfy the conditions in the previous if
or elif keywords. The else keyword catches the flow if none of the preceding
conditions were met:
if condition 1:
# set of instructions in case condition 1 is met
elif condition 2:
# set of instructions if condition 2 is met but condition 1 is not
elif condition 3:
# set of instructions if condition 3 is met but condition 1,2 are not
……..
else:
# set of instructions if none of the preceding condition 1,2,... are met
IF-ELIF-ELSE LOGIC
IF-ELIF-ELSE LOGIC
• Example:
IF-ELIF-ELSE LOGIC
• Write a program to find maximum between three numbers
• Write a program to find whether number is positive, negative or zero
• Write a program to find whether a character is alphabet, digit or special
character
Task 01
• Imagine an alien was just shot down in a game. Create a variable called
alien_color and assign it a value of 'green', 'yellow', or 'red'.
• If the alien is green, print a message that the player earned 5 points.
• If the alien is yellow, print a message that the player earned 10 points.
• If the alien is red, print a message that the player earned 15 points.
Task 02
A school’s examination department wants to automate the process of
assigning grades to students based on their marks.
The grading policy is as follows:
Marks Range Grade
80 – 100 A
70 – 79 B
60 – 69 C
0 – 59 D
You are asked to write a Python program that takes a student’s marks as
input and displays the grade according to the table above.
NESTED CONDITIONALS
• One conditional can also be nested within another. We could have written the
three branches
• The outer conditional contains two branches. The first branch contains a simple
statement. The second branch contains another if statement, which has two
branches of its own
if x == y:
print('x and y are equal')
else:
if x < y:
print('x is less than y')
else:
print('x is greater than y')
NESTED CONDITIONALS
CONDITIONALS - EXERCISE
• Write a program to prompt for a score between 0.0 and 1.0. If the score is
out of range, print an error message. If the score is between 0.0 and 1.0,
print a grade using the following table:
• Score Grade Output
• >= 0.9 A Enter score: 0.95
A
• >= 0.8 B Enter score: perfect
• >= 0.7 C Bad score
Enter score: 10.0
• >= 0.6 D Bad score
• < 0.6 F Enter score: 0.75
C
Enter score: 0.5
F
CONDITIONS WITH LOGIC OPERATOR
• Logical clauses attached to the if and elif keywords
• Example: x=int(input("Write an integer x>1"))
y=int(input("Write another integer y<-2"))
if x <= 1 or y >= -2:
print("Wrong numbers")
x=int(input("Write an integer x>1"))
y=int(input("Write another integer y<-2"))
if (x <= 1 and y < -2) or (x > 1 and y >= -2):
print("One of the numbers is wrong")
Tasks 03
• Write an if-elif-else chain that determines a person’s stage of life. Set a value for
the variable age, and then:
• If the person is less than 2 years old, print a message that the person is a baby.
• If the person is at least 2 years old but less than 4, print a message that the person is a
toddler.
• If the person is at least 4 years old but less than 13, print a message that the person is a kid.
• If the person is at least 13 years old but less than 20, print a message that the person is a
teenager.
• If the person is at least 20 years old but less than 65, print a message that the person is an
adult.
• If the person is age 65 or older, print a message that the person is an elder.
Tasks 04
A city traffic department wants to automate the process of deciding fines for
overspeeding using a Python program.
The system should work as follows:
• First, check if the vehicle’s speed is within a valid range (greater than 0 and
less than or equal to 200).
→ If not, display – “Invalid speed”
• If the speed is valid, check:
• If the speed is 60 or less, print "No Fine".
• If the speed is between 61 and 100, print "Speeding Fine: Rs. 1000".
• If the speed is greater than 100, then:
• If the driver has a license, print "Heavy Fine: Rs. 3000".
• Else, print "Severe Fine: Rs. 5000 and license suspension."