Introduction to
Conditional
Statements in
Python
Conditional statements are the building blocks of decision-
making in Python. They allow your program to execute different
code based on specific conditions, making your programs more
dynamic and responsive.
What are conditional
statements?
Conditional statements are a fundamental part of programming
logic, allowing programs to make decisions based on the evaluation
of conditions. They control the flow of execution, determining which
code blocks are executed.
If
Executes a block of code if a
condition is True.
Else
Executes a block of code if a
condition is False.
Elif
Provides additional conditions to be checked if the previous
condition is False.
Comparison Operators in Python
Comparison operators are used to compare values and evaluate to True or False. They are essential for creating conditions in
conditional statements.
Operator Description Example
== Equal to 5 == 5
!= Not equal to 5 != 10
> Greater than 10 > 5
< Less than 5 < 10
>= Greater than or equal to 10 >= 10
<= Less than or equal to 5 <= 10
The if statement
The if statement executes a block of code if a specified condition is True. If the condition is False, the code block is skipped.
Check Condition
Evaluates the condition using comparison operators.
Execute Code Block
If the condition is True, the code block is executed.
Skip Code Block
If the condition is False, the code block is skipped.
The if-else statement
The if-else statement provides an alternative code block to execute if the initial condition is False. This ensures
that some code is always executed, regardless of the condition's truth value.
if Condition
If the condition is True, this code block is executed.
else
If the condition is False, this code block is executed.
The if-elif-else statement
The if-elif-else statement allows for multiple conditions to be checked sequentially. If one
condition is True, its corresponding code block is executed, and the remaining conditions
are skipped. The else block is executed if none of the previous conditions are True.
1 Check Condition 1
If True, execute code block 1.
2 Check Condition 2
If True and condition 1 is False, execute code block 2.
3 Check Condition 3
If True and conditions 1 and 2 are False, execute code block 3.
4 Else
If all conditions are False, execute this code block.
Nested conditional statements
Nested conditional statements allow you to create more complex decision-making structures. They involve placing
an if, elif, or else statement within another conditional statement, allowing for finer control over code execution.
1 Outer Condition
Evaluates the first condition,
controlling the execution of
the nested statements.
2 Inner Condition
Evaluates the second
condition, controlling the
execution of the inner code
block.
3 Nested Logic
Allows for a series of
decisions based on multiple
conditions.
Ternary operator
The ternary operator is a concise way to express conditional logic in a single line. It
provides a shorthand for if-else statements, often making code more readable.
Condition
Evaluates the condition, determining which value is returned.
True Value
Returned if the condition is True.
False Value
Returned if the condition is False.
Common use cases for conditional statements
Conditional statements are used in a wide variety of situations, making your Python programs more interactive and intellig
Navigation
Conditional statements can direct
navigation based on user input,
road conditions, or destination.
Game Logic
Conditional statements define
actions based on player inputs,
game state, or enemy behavior.
User Authentication
Conditional statements check user
credentials, validating login
attempts and granting access to
secured content.
Conclusion and Key
Takeaways
Conditional statements are essential for creating dynamic and
intelligent Python programs. Understanding their structure and
different variations allows you to implement sophisticated logic
and control the flow of execution.
Q1. Write a program to check whether a person is eligible
for voting or not. (accept age from user)
age=int(input("Enter your age"))
if age >= 18:
print("Eligible for voting")
else:
print("not eligible for voting")
Q2. Write a program to check whether a number entered by user
is even or odd.
Ans.
num=int(input("Enter your age"))
if num%2==0:
print("Number is Even")
else:
print("Number is Odd")
Q3. Write a program to check whether a number is
divisible by 7 or not.
num=int(input("Enter your age"))
if num%7==0:
print("Number is divisible")
else:
print("Number is not divisible")
Q4. Write a program to display "Hello" if a number entered
by user is a multiple of five, otherwise print "Bye".
num=int(input("Enter your age"))
if num%5==0:
print("Hello")
else:
print("Bye")
Q5. Write a program to accept percentage from the user and
display the grade according to the following criteria:
Marks Grade
> 90 A
> 80 and <= 90 B
>= 60 and <= 80 C
below 60 D
per = int(input("Enter marks"))
if per > 90:
print("Grade is A")
if per > 80 and per <=90:
print("Grade is B")
if per > =60 and per <= 80: print("Grade is C")
if per < 60:
print("Grade is D")
Q6. Write a program to accept the cost price of a bike and display the road tax
to be paid according to the following criteria:
Cost price (in Rs) Tax
> 100000 15%
> 50000 and <= 100000 10%
<= 50000 5%
tax = 0
pr=int(input("Enter the price of bike"))
if pr > 100000:
tax = 15/100*pr
elif pr > 50000 and pr <=100000:
tax = 10/100*pr
else:
tax = 5/100*pr
print("Tax to be paid ",tax)
Q7. Write a program to accept a number from 1 to 7 and display
the name of the day like 1 for Sunday, 2 for Monday and so on.
Ans.
num=int(input("Enter any number between 1 to 7: "))
if num==1:
print("Sunday")
elif num==2:
print("Monday")
elif num==3:
print("Tuesday")
elif num==4:
print("Wednesday")
elif num==5:
print("Thursday")
elif num==6:
print("Friday")
elif num==2:
print("Saturday")
else:
print("Please enter number between 1 to 7")

Introduction-to-Conditional-Statements-in-Python.pptx

  • 1.
    Introduction to Conditional Statements in Python Conditionalstatements are the building blocks of decision- making in Python. They allow your program to execute different code based on specific conditions, making your programs more dynamic and responsive.
  • 2.
    What are conditional statements? Conditionalstatements are a fundamental part of programming logic, allowing programs to make decisions based on the evaluation of conditions. They control the flow of execution, determining which code blocks are executed. If Executes a block of code if a condition is True. Else Executes a block of code if a condition is False. Elif Provides additional conditions to be checked if the previous condition is False.
  • 3.
    Comparison Operators inPython Comparison operators are used to compare values and evaluate to True or False. They are essential for creating conditions in conditional statements. Operator Description Example == Equal to 5 == 5 != Not equal to 5 != 10 > Greater than 10 > 5 < Less than 5 < 10 >= Greater than or equal to 10 >= 10 <= Less than or equal to 5 <= 10
  • 4.
    The if statement Theif statement executes a block of code if a specified condition is True. If the condition is False, the code block is skipped. Check Condition Evaluates the condition using comparison operators. Execute Code Block If the condition is True, the code block is executed. Skip Code Block If the condition is False, the code block is skipped.
  • 5.
    The if-else statement Theif-else statement provides an alternative code block to execute if the initial condition is False. This ensures that some code is always executed, regardless of the condition's truth value. if Condition If the condition is True, this code block is executed. else If the condition is False, this code block is executed.
  • 6.
    The if-elif-else statement Theif-elif-else statement allows for multiple conditions to be checked sequentially. If one condition is True, its corresponding code block is executed, and the remaining conditions are skipped. The else block is executed if none of the previous conditions are True. 1 Check Condition 1 If True, execute code block 1. 2 Check Condition 2 If True and condition 1 is False, execute code block 2. 3 Check Condition 3 If True and conditions 1 and 2 are False, execute code block 3. 4 Else If all conditions are False, execute this code block.
  • 7.
    Nested conditional statements Nestedconditional statements allow you to create more complex decision-making structures. They involve placing an if, elif, or else statement within another conditional statement, allowing for finer control over code execution. 1 Outer Condition Evaluates the first condition, controlling the execution of the nested statements. 2 Inner Condition Evaluates the second condition, controlling the execution of the inner code block. 3 Nested Logic Allows for a series of decisions based on multiple conditions.
  • 8.
    Ternary operator The ternaryoperator is a concise way to express conditional logic in a single line. It provides a shorthand for if-else statements, often making code more readable. Condition Evaluates the condition, determining which value is returned. True Value Returned if the condition is True. False Value Returned if the condition is False.
  • 9.
    Common use casesfor conditional statements Conditional statements are used in a wide variety of situations, making your Python programs more interactive and intellig Navigation Conditional statements can direct navigation based on user input, road conditions, or destination. Game Logic Conditional statements define actions based on player inputs, game state, or enemy behavior. User Authentication Conditional statements check user credentials, validating login attempts and granting access to secured content.
  • 10.
    Conclusion and Key Takeaways Conditionalstatements are essential for creating dynamic and intelligent Python programs. Understanding their structure and different variations allows you to implement sophisticated logic and control the flow of execution.
  • 11.
    Q1. Write aprogram to check whether a person is eligible for voting or not. (accept age from user) age=int(input("Enter your age")) if age >= 18: print("Eligible for voting") else: print("not eligible for voting")
  • 12.
    Q2. Write aprogram to check whether a number entered by user is even or odd. Ans. num=int(input("Enter your age")) if num%2==0: print("Number is Even") else: print("Number is Odd")
  • 13.
    Q3. Write aprogram to check whether a number is divisible by 7 or not. num=int(input("Enter your age")) if num%7==0: print("Number is divisible") else: print("Number is not divisible")
  • 14.
    Q4. Write aprogram to display "Hello" if a number entered by user is a multiple of five, otherwise print "Bye". num=int(input("Enter your age")) if num%5==0: print("Hello") else: print("Bye")
  • 15.
    Q5. Write aprogram to accept percentage from the user and display the grade according to the following criteria: Marks Grade > 90 A > 80 and <= 90 B >= 60 and <= 80 C below 60 D per = int(input("Enter marks")) if per > 90: print("Grade is A") if per > 80 and per <=90: print("Grade is B") if per > =60 and per <= 80: print("Grade is C") if per < 60: print("Grade is D")
  • 16.
    Q6. Write aprogram to accept the cost price of a bike and display the road tax to be paid according to the following criteria: Cost price (in Rs) Tax > 100000 15% > 50000 and <= 100000 10% <= 50000 5% tax = 0 pr=int(input("Enter the price of bike")) if pr > 100000: tax = 15/100*pr elif pr > 50000 and pr <=100000: tax = 10/100*pr else: tax = 5/100*pr print("Tax to be paid ",tax)
  • 17.
    Q7. Write aprogram to accept a number from 1 to 7 and display the name of the day like 1 for Sunday, 2 for Monday and so on. Ans. num=int(input("Enter any number between 1 to 7: ")) if num==1: print("Sunday") elif num==2: print("Monday") elif num==3: print("Tuesday") elif num==4: print("Wednesday") elif num==5: print("Thursday") elif num==6: print("Friday") elif num==2: print("Saturday") else: print("Please enter number between 1 to 7")