Python Conditional Statements
Hey friends 👋 welcome back, In the last lesson, we learned about input and output, Today, we are going to learn about conditional statements, which allow our programs to make decisions and take different actions depending on certain conditions, Conditional statements make programs smart and interactive.
The if Statement
The if statement is used to check a condition, Example:
Explanation:
-
ifchecks whether the condition is True -
If the condition is True, the code inside the block runs
-
If the condition is False, nothing happens
The else Statement
The else statement lets us run code when the if condition is False, Example:
Output:
The elif Statement
The elif statement allows us to check multiple conditions, Example:
Output:
Practice Time
Create a Python file called conditional_statements.py and try these exercises, 1, Ask the user for their age and print if they are a minor or adult, 2, Ask the user for a number and check if it is positive, negative, or zero, 3, Ask the user for their marks and assign a grade using if, elif, else, 4, Ask the user for two numbers and print which one is greater
Great job, You just learned how to make decisions in Python using conditional statements, In the next lesson, we will explore Python Loops, where we will learn how to repeat actions multiple times using for and while loops, We will soon be launching our YouTube channel, where we are going to post step-by-step video tutorials on Python and other tech skills, These videos will make learning easier and more visual, helping beginners follow along confidently.
0 Comments