0% found this document useful (0 votes)
17 views5 pages

Class

The document provides examples of conditional statements in Python, including if-else blocks, nested conditions, and the use of logical operators. It demonstrates how to check eligibility for voting, calculate grades, and perform basic arithmetic operations based on user input. Additionally, it covers input methods and checks for even or odd numbers.

Uploaded by

Saquib Mahmood
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)
17 views5 pages

Class

The document provides examples of conditional statements in Python, including if-else blocks, nested conditions, and the use of logical operators. It demonstrates how to check eligibility for voting, calculate grades, and perform basic arithmetic operations based on user input. Additionally, it covers input methods and checks for even or odd numbers.

Uploaded by

Saquib Mahmood
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

Conditional Statements

In [ ]:
age = 19

if age > 18:


#indentation is important in Python
print("you are eligible to vote")

you are eligible to vote

In [ ]:
age = -180

#if-else block
if age >= 18: #this is my condition
#indentation is important in Python
print("you are eligible to vote")
else:
print("you are not eligible to vote")

you are not eligible to vote

In [14]:
x = 10
y = 100
z = 1000

if x==y:
print("x is equal to y")
elif x==z: #else if (Additonal conditions)
print("x is equal to z")
else: # will only execute if above conditons are not
#satisfied
print("x is not equal to y or z")

x is not equal to y or z

In [21]: x = 10
y = 10
z = 1000

if x==y:
print("x is equal to y")
elif x==y:
print("x is equal to y")
# if x==z:
# print("x is equal to z")
# if x!=z:
# print("x is not equal to z")
x is equal to y

In [28]:
# grade calculator
marks = 94
# Single if elif else block
if marks >= 90:
print("A+")
elif marks >= 80:
print("A")
elif marks >= 70:
print("B+")
elif marks >= 60:
print("B")
elif marks >= 50:
print("C")
else:
print("Fail")

A+

In [27]:
# grade calculator
marks = 94
# 5 if else block
if marks >= 90:
print("A+")
if marks >= 80:
print("A")
if marks >= 70:
print("B+")
if marks >= 60:
print("B")
if marks >= 50:
print("C")
else:
print("Fail")

A+
A
B+
B
C

In [ ]:
# checking conditions blocks
number = 7

if number > 5:
print("Number is greater than 5")
elif number > 8:
print("Number is greater than 8")

elif number > 5:


print("Number is greater than 5")
elif number > 8:
print("Number is greater than 8")
Number is greater than 5

In [43]:
# input method in python

name = str(input("Enter your name: "))


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

print("your name is : ",name)


print("your age is : ",age)

your name is : jamal


your age is : 23

In [42]:
has_value = bool(input("Enter True or False: "))
print("The boolean value is : ",has_value)
print(type(has_value))

The boolean value is : False


<class 'bool'>

In [46]:
# check if a number is even or odd
number = int(input("Enter a number: "))
print("The number you entered is: ",number)
if number % 2 == 0: # remainder/modulus operator
print("The number is even")
else:
print("The number is odd")

The number you entered is: 2


The number is even

In [ ]:
# calculator without nested
number1 = float(input("enter any number: "))
number2 = float(input("enter another number: "))
operator = input("enter operator (+, -, *, /): ")

if operator == '+':
print("The sum is: ", number1 + number2)
elif operator == '-':
print("The difference is: ", number1 - number2)
elif operator == '*':
print("The output is: ",number1 * number2)
elif operator == '/':
print("The output is: ",number1 / number2)
else:
print("Invalid operator")

The sum is: 20.0

In [54]:
# nested conditonal statements
number1 = float(input("enter any number: "))
number2 = float(input("enter another number: "))
operator = input("enter operator (+, -, *, /): ")
if operator == '+':
print("The sum is: ", number1 + number2)
elif operator == '-':
print("The difference is: ", number1 - number2)
elif operator == '*':
print("The output is: ",number1 * number2)
elif operator == '/':
if number2 != 0:
print("The output is: ", number1/number2)
else:
print("Calculation is not possible due to zero division")
else:
print("Invalid operator")

Calculation is not possible due to zero division

Online resource:
[Link]

.ipynb -> i python notebook

In [63]:
# and / or operator in conditional statements

username = input("Enter your username: ")


password = input("Enter your password: ")
# and -> &
# or -> |

if username == "admin" and password == "1234":


print("Login successful")
else:
print("Login failed")

Login failed

In [62]:
day = input("Enter day of the week: ")
print([Link]())

if [Link]() == "saturday" or [Link]() == "sunday":


print("It's weekend")
else:
print("It's a weekday")

saturday
It's weekend

In [ ]:
name = "jamal khan"
[Link]() # camel case

Out [64]: 'Jamal Khan'

In [ ]:

You might also like