Conditional Statements
Conditional Statements
ipynb - Colab
Video Outline:
## if statement
age=20
if age>=18:
print("You are allowed to vote in the elections")
age>=18
True
## else
## The else statement executes a block of code if the condition in the if statement is False.
age=16
if age>=18:
print("You are eligible for voting")
else:
print("You are a minor")
## elif
## The elif statement allows you to check multiple conditions. It stands for "else if"
age=17
if age<13:
print("You are a child")
elif age<18:
print("You are a teenager")
else:
print("You are an adult")
# You can place one or more if, elif, or else statements inside another if, elif, or else statement to create nested conditional statements.
if num>0:
print("The number is positive")
if num%2==0:
print("The number is even")
else:
print("The number is odd")
else:
print("The number is zero or negative")
https://colab.research.google.com/drive/1Abta6SgV6ixvNftKGKeS7u5pEIaY6r0Q#printMode=true 1/4
7/16/24, 8:25 PM Conditionalstatements.ipynb - Colab
## Practical Examples
if year%4==0:
if year%100==0:
if year%400==0:
print(year,"is a leap year")
else:
print(year,"is not a leap year")
else:
print(year,"is a leap year")
else:
print(year,"is not a leap year")
## Assignment
## Simple Calculator program
# Take user input
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")
print("Result:", result)
Result: 36.0
https://colab.research.google.com/drive/1Abta6SgV6ixvNftKGKeS7u5pEIaY6r0Q#printMode=true 2/4
7/16/24, 8:25 PM Conditionalstatements.ipynb - Colab
### Determine the ticket price based on age and whether the person is a student.
# Ticket pricing based on age and student status
Calculate an employee's bonus based on their performance rating and years of service.
https://colab.research.google.com/drive/1Abta6SgV6ixvNftKGKeS7u5pEIaY6r0Q#printMode=true 3/4
7/16/24, 8:25 PM Conditionalstatements.ipynb - Colab
# User login system
https://colab.research.google.com/drive/1Abta6SgV6ixvNftKGKeS7u5pEIaY6r0Q#printMode=true 4/4