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

Conditional Statments

This document discusses conditional statements in Python including if, if-else, and multi-way if-else statements. It provides examples of using Boolean operators like ==, !=, <, <=, >, >= in conditional expressions. It also covers logical operators like and, or, and not and their order of precedence. One line if statements are demonstrated as an alternative syntax to multiline if/else blocks.

Uploaded by

almulla7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Conditional Statments

This document discusses conditional statements in Python including if, if-else, and multi-way if-else statements. It provides examples of using Boolean operators like ==, !=, <, <=, >, >= in conditional expressions. It also covers logical operators like and, or, and not and their order of precedence. One line if statements are demonstrated as an alternative syntax to multiline if/else blocks.

Uploaded by

almulla7x
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Conditional Statements

Dr. Shereef Abu Al-Maati 1


Boolean expressions
booleanOperand booleanOperator booleanOperand

Boolean operators meaning example Example result


== equals 1 == 2 False
!= Not equal a != b Depends on a & b
< Less than 3<7 True
<= Less than or equal 5 <= (7-2) True
> Greater than d>4 Depends on d
>= Greater than or 1 >= 5 False
equal

Dr. Shereef Abu Al-Maati 2


If statement
If <condition>:
<statments1..n>

Exampe1 Example2
if 1 < 2: If 2 < 2:
print("1<2") print("1<2")
print("progran ends") print("progran ends")
-------- --------
1<2 progran ends
progran ends

Dr. Shereef Abu Al-Maati 3


If else statement
If <condition>:
<statments1..n>
else:
<statments1..n>

Exampe1

age = int(input("enter your age: "))


if age >=0:
print("Age:",age)
else:
print("Invalid Age: age cannot be a negative number")

Dr. Shereef Abu Al-Maati 4


Multi-way If else statement
If <condition1>:
<statements 1..n>
elif <condition2> :
<statements 1..n>
elif <condition n> :
<statments1..n>
else <condition > :
<default statments1..n>

Dr. Shereef Abu Al-Maati 5


Multi-way If else statement, Example

grade= int(input("enter your numeric grade: "))


if grade>=90:
print("Grade: A")
elif grade>=80:
print("Grade: B")
elif grade>=70:
print("Grade: C")
elif grade>=60:
print("Grade: D")
else:
print("Grade: F")

Dr. Shereef Abu Al-Maati 6


Logical operators
a b a and b a b a or b
T T T T T T
T F F T F T
F T F F T T
F F F F F F

a not a
T F
Note, the not operator
F T
has higher precedence
than operator and, or

Dr. Shereef Abu Al-Maati 7


Logical operator example
Example with no Logical operator
grade= int(input("enter your numeric grade: "))
if grade>100:
print("numeric grade cannot be greater than 100")
elif grade < 0:
print("numeric grade cannot be less than 0")
#rest of code for determining letter grade from previous example

Example with or Logical operator


grade= int(input("enter your numeric grade: "))
if grade>100 or grade < 0:
print("numeric grade must be between 0 and 100")
#rest of code for determining letter grade from previous example

Dr. Shereef Abu Al-Maati 8


Order of precedence

order Symbol
1 ** Exponentiation, 2**3 , 2 raised to the power 3
2 - Negation, -7
3 *, /, % Multiply, divide, remainder
4 +, - Plus, minus
5 == , !=, <, >, <=, >= comparison
6 not Logical negation
7 and, or Logical and or
8 = assignment

Dr. Shereef Abu Al-Maati 9


One line if statements

• <expression1> if <condition> else <expression2>

Dr. Shereef Abu Al-Maati 10


One line if statements

grade = 70
print(‘PASS’ if grade >= 60 else ‘FAIL’)

--------------------------
grade = 70
if grade >= 60:
print('PASS')
else:
print('FAIL')

Dr. Shereef Abu Al-Maati 11

You might also like