0% found this document useful (0 votes)
5 views

Python Notes by Ankush Chap09-Conditional Statements

This document is a tutorial on conditional statements in Python, explaining their importance for controlling program flow. It covers five types of conditional statements: 'if', 'if-else', 'if-elif-else', nested 'if-else', and conditional expressions, along with syntax and examples for each type. Additionally, it includes homework questions to reinforce understanding of the concepts presented.

Uploaded by

paidpdfnotes
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)
5 views

Python Notes by Ankush Chap09-Conditional Statements

This document is a tutorial on conditional statements in Python, explaining their importance for controlling program flow. It covers five types of conditional statements: 'if', 'if-else', 'if-elif-else', nested 'if-else', and conditional expressions, along with syntax and examples for each type. Additionally, it includes homework questions to reinforce understanding of the concepts presented.

Uploaded by

paidpdfnotes
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
You are on page 1/ 6

PYTHON TUTORIAL FOR BEGINNERS

Chapter - 09

Conditional Statements in Python


• Conditional Statement definition
• Types of Conditional Statement
• Conditional Statement examples

Conditional Statements in Python


Conditional statements allow you to execute code based on condition evaluates
to True or False. They are essential for controlling the flow of a program and
making decisions based on different inputs or conditions.
Examples
a = 26
b = 108
if b > a:
print("b is greater than a")
Indentation - whitespace at the beginning ofi a line

Types of Conditional Statements


There are 5 types of conditional statements in Python:
1. 'if' Statement
2. 'if-else' statement
3. 'if-elif-else' statement
4. Nested 'if else' statement
5. Conditional Expressions (Ternary Operator)

P y thon No te s by Ankush
1. 'if' Conditional Statement
The if statement is used to test a condition and execute a block of code only if
the condition is true.
Syntax:
if condition:
Code to execute if the condition is true

Example:
age = 26
if age > 19:
print("You are an adult")

'if' statement flow diagram:

P y thon No te s by Ankush
2. 'if-else' Conditional Statement
The if-else statement provides an alternative block of code to execute if the
condition is false.

Syntax:
if condition:
Code to execute if the condition is true
else:
Code to execute if the condition is false

Example:
temperature = 30
if temperature > 25:
print("It's a hot day.")
else:
print("It's a cool day.")

'if-else' statement flow diagram:

P y thon No te s by Ankush
3. 'if-elif-else' Conditional Statement
The if-elif-else statement allows to check multiple conditions and execute
different blocks of code based on which condition is true.

Syntax:
if condition1:
Code to execute if condition1 is true
elif condition2:
Code to execute if condition2 is true
else:
Code to execute if none of the above conditions are true

Example:
Grading system: Let’s write a code to classify the student’s grade based on their
total marks (out of hundred).
score = 85
if score >= 90:
print("Grade - A")
elif score >= 80:
print("Grade - B")
elif score >= 70:
print("Grade - C")
else:
print("Grade - D")

4. ested 'if-else' Conditional Statement


A nested if-else statement in Python involves placing an if-else statement
inside another if-else statement. This allows for more complex decision-making
by checking multiple conditions that depend on each other.

P y thon No te s by Ankush
Syntax:
if condition1:
Code block for condition1 being True
if condition2:
Code block for condition2 being True
else:
Code block for condition2 being False
else:
Code block for condition1 being False
... ..

Example:
Number Classification: Let's say you want to classify a number as positive,
negative, or zero and further classify positive numbers as even or odd.
number = 10
if number > 0: First check ifi the number is positive
if number % 2 == 0:
print("The number is positive and even.")
else:
print("The number is positive and odd.")
else: The number is not positive
if number == 0:
print("The number is zero.")
else:
print("The number is negative.")

5. Conditional Expressions
Conditional expressions provide a shorthand way to write simple if-else
statements. Also known as Ternary Operator.

P y thon No te s by Ankush
Syntax:
value_if_true if condition else value_if_false

Example:
age = 16
status = "Adult" if age >= 18 else "Minor"
print(status)

Conditional Statements- HW
Q1: what is expected output and reason?

value = one

if value:
print("Value is True")
else:
print("Value is False")

Q2: write a simple program to determine if a given year is a leap


year using user input.

Python Tutorial Playlist:

P y thon No te s by Ankush

You might also like