0% found this document useful (0 votes)
9 views3 pages

Python Conditional Statements

The document explains conditional statements in Python, which allow for decision-making in programs based on true or false conditions. It covers key concepts such as indentation, if, elif, else statements, nested ifs, multiple if statements, and logical operators. Each concept is illustrated with code examples to demonstrate their usage.

Uploaded by

storagemathura
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)
9 views3 pages

Python Conditional Statements

The document explains conditional statements in Python, which allow for decision-making in programs based on true or false conditions. It covers key concepts such as indentation, if, elif, else statements, nested ifs, multiple if statements, and logical operators. Each concept is illustrated with code examples to demonstrate their usage.

Uploaded by

storagemathura
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

Conditional statements in Python are used to make decisions in a program. They allow the
program to execute different blocks of code based on whether a condition is true or false. This
helps in controlling the flow of execution depending on inputs or situations.

age = 18

if age >= 18:


print( "You are eligible to vote" )

1. Indentation
Indentation in Python refers to the spaces at the beginning of a line of code. It is very important
because Python uses indentation to define blocks of code (instead of braces like other
languages). Proper indentation ensures the code runs correctly.

if True:
print("This is inside the block") # indented
print("This is outside the block") # not indented

2. if Statement
The if statement is used to check a condition. If the condition is true, the code inside the if
block executes. If it is false, the code is skipped.
number = 10

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

3. elif Statement
The elif (else if) statement is used to check multiple conditions. If the first if condition is
false, Python checks the elif condition. You can use multiple elif statements.
marks = 75

if marks >= 90:


print("Grade A")
elif marks >= 70:
print("Grade B")
4. else Statement
Conditional statements in Python are used to make decisions in a program. They allow the
program to execute different blocks of code based on whether a condition is true or false. This
helps in controlling the flow of execution depending on inputs or situations.

age = 18

if age >= 18:


print("You are eligible to vote")

5. Nested if
A nested if means using an if statement inside another if statement. It is useful when you
need to check multiple levels of conditions.

age = 20
citizen = True

if age >= 18:


if citizen:
print("Eligible to vote")

6. Multiple if Statements
Multiple if statements are used when you want to check several independent conditions. Each
if runs separately, even if previous ones are true.

number = 10

if number > 5:
print("Greater than 5")

if number % 2 == 0:
print("Even number")
7. Logical Operators (and, or, not)
The and operator returns True only if both conditions are true.

age = 25
salary = 30000

if age > 18 and salary > 20000:


print("Eligible for loan")

OR Operator
The or operator returns True if at least one condition is true

day = "Sunday"

if day == "Saturday" or day == "Sunday":


print("Weekend")

NOT Operator

The not operator is used to reverse the result. If a condition is True, not makes it False, and
vice versa.

is_logged_in = False

if not is_logged_in:
print("Please login first")

One Liners

if → check condition
elif → check another condition
else → default case
nested if → condition inside condition
multiple if → independent conditions
and → all true
or → any one true
not → reverse condition

You might also like