Conditional Topic
I. Comparison Operators
- These operators are used to compare different data types to each other.
Operator Meaning Example NOT EQUAL TO !=
== equal to x == y Example Return Value
!= not equal to != 8 != 9 true
> greater than x>y 9 != 9 false
>= greater than or equal to x >= y "happy" != "happy" false
< less than x<y "happy" != "hello" true
<= less than or equal to x <= y
II. Logical Operators
- These operators are used to combine conditional statements.
Operator Meaning Example Return Value
and Returns True if both 4 < 5 and 9 < 10 True
statements are true
or Returns True if one of the 3 < 5 or 6 < 4 True
statements is true
not Returns False if the result is not(4 < 5 and 9 < 10) False
true
III. Control Flow
- A program’s control flow is the order in which the program’s code executes. The control
flow of a Python program is regulated by conditional statements, loops, and function
calls. This covers the if statement and for and while loops.
● The if Statement
You need to execute some statements only if some condition holds, or choose
statements to execute depending on several mutually exclusive conditions. The Python
compound statement if, which uses if, elif, and else clauses, lets you conditionally execute
blocks of statements.
if x < 0:
print "x is negative"
elif x % 2:
print "x is positive and odd"
else:
print "x is even and non-negative"
● The while Statement
The while statement in Python supports repeated execution of a
statement or block of statements that is controlled by a conditional
expression.
count = 0
while x > 0:
x = x // 2
count += 1
print "The approximate log2 is", count
● The for Statement
The for statement in Python supports repeated execution of a
statement or block of statements that is controlled by an iterable expression.
for letter in "ciao":
print "give me a", letter, "..."
IV. Decision Making
- Decision making statements can direct a program to execute on the basis
of instructions if a certain condition is True or another if it's False.
Types of decision-making in python
There are three types of decision making statements in python language,
those are
1. If statements in python
2. If else statement in python
3. Nested if else statement in python
If statements in python
If statements allow for conditions to be set within code so that it behaves
differently depending on whether or not certain conditions are true.
var1 = 100
if var1:
print ("1 - Got a true expression value" , var1 )
Var2=0
if var2:
print "2 - Got a true expression value"
print var2
print "Good bye!"
If else statement in python
If certain conditions are met then a certain action will occur. This statement
can be used to enhance the logic and branching of an existing program or to
create branching within a program that does something different depending on
how it is invoked.
var1 = 100
if var1:
print "1 - Got a true expression value"
print var1
else:
print "1 - Got a false expression value"
print var1
var2 = 0
if var2:
print "2 - Got a true expression value"
print var2
else:
print "2 - Got a false expression value"
print var2
print "Good bye!"
Nested if else statement in python
Nested if else statements serve as an effective decision-making tool in
python programming by allowing developers to code any number of decisions or
conditions within an if else statement.
var = 100
if var < 200:
print "Expression value is less than 200"
if var == 150:
print "Which is 150"
elif var == 100:
print "Which is 100"
elif var == 50:
print "Which is 50"
elif var < 50:
print "Expression value is less than 50"
else:
print "Could not find true expression"
print "Good bye!"
V. If Statement, Elif Statement, and MATCH-CASE Statement and 3 example problems with
solutions (PYTHON Codes)
IF STATEMENT
ELIF STATEMENT
MATCH- CASE