0% found this document useful (0 votes)
2 views2 pages

Conditional Statements in Python Rupaalife9

Conditional statements in Python allow programs to make decisions based on conditions, executing different code blocks depending on whether a condition is true or false. The main types include if, if-else, if-elif-else, and nested if statements, each serving different decision-making needs. They are essential for various applications, such as checking eligibility or validating inputs.
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)
2 views2 pages

Conditional Statements in Python Rupaalife9

Conditional statements in Python allow programs to make decisions based on conditions, executing different code blocks depending on whether a condition is true or false. The main types include if, if-else, if-elif-else, and nested if statements, each serving different decision-making needs. They are essential for various applications, such as checking eligibility or validating inputs.
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 Python

1. What are Conditional Statements?


Conditional statements in Python are used to make decisions based on conditions. They
allow a program to execute one block of code if a condition is true and another block if the
condition is false.

2. Why Do We Need Conditional Statements?


Conditional statements help programs behave differently based on situations such as
checking marks, validating login details, or making logical decisions.

3. Types of Conditional Statements in Python


1. if statement
2. if-else statement
3. if-elif-else statement
4. Nested if statement

4. The if Statement
Syntax:
if condition:
statement

Example:
age = 20
if age >= 18:
print('Eligible to vote') rupaalife
5. Real-Time Example of if
Marks checking example:
marks = 40
if marks >= 35:
print('Pass')

6. The if-else Statement


Syntax:
if condition:
statement
else:
statement

Example:
age = 16
if age >= 18:
print('Eligible to vote')
else:
print('Not eligible')

7. Real-Time Example of if-else


Login example:
password = 'python123'
if password == 'python123':
print('Login successful')
else:
print('Invalid password')

8. The if-elif-else Statement


Used when multiple conditions are required.
Example:
marks = 75
if marks >= 90:
print('Grade A')
elif marks >= 60:
print('Grade B')
else:
print('Grade C')

9. Nested if Statement
An if statement inside another if statement.
Example:
age = 20
id_card = True
if age >= 18:
if id_card:
print('Entry allowed')

10. Operators Used in Conditions

rupaalife
Comparison operators: ==, !=, >, <, >=, <=
Logical operators: and, or, not

11. Common Mistakes


Forgetting colon, wrong indentation, using = instead of ==.

12. Conclusion
Conditional statements are the foundation of decision-making in Python and are widely
used in real-time applications.

You might also like