Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Day8 Conditional Statement in Python

This document provides an overview of conditional statements in Python, explaining their importance in controlling program flow and decision-making. It details 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 related to conditional statements.

Uploaded by

h27803130
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Day8 Conditional Statement in Python

This document provides an overview of conditional statements in Python, explaining their importance in controlling program flow and decision-making. It details 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 related to conditional statements.

Uploaded by

h27803130
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON TUTORIAL FOR BEGINNERS

Source: www.youtube.com/@RishabhMishraOfficial

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 of 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 t h o n N o t e s b y R i s h a b h M i s h ra
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 t h o n N o t e s b y R i s h a b h M i s h ra
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 t h o n N o t e s b y R i s h a b h M i s h ra
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. Nested '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 t h o n N o t e s b y R i s h a b h M i s h ra
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 if 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 t h o n N o t e s b y R i s h a b h M i s h ra
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 = None

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: Click Here


https://www.youtube.com/playlist?list=PLdOKnrf8EcP384Ilxra4UlK9BDJGwawg9

P y t h o n N o t e s b y R i s h a b h M i s h ra

You might also like