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

Unit02_Control Statements (1)

control statement in brief

Uploaded by

gurneetkaur957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Unit02_Control Statements (1)

control statement in brief

Uploaded by

gurneetkaur957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Python If Else Statements – Conditional Statements

Conditional statements (if, else, and elif) are fundamental programming


constructs that allow you to control the flow of your program based on
conditions that you specify. They provide a way to make decisions in your
program and execute different code based on those decisions.

 If-Else statements in Python are part of conditional statements, which decide


the control of code.
 There are situations in real life when we need to make some decisions and
based on these decisions, we decide what we should do next.
 Similar situations arise in programming also where we need to make some
decisions and based on these decisions, we will execute the next block of code.
 Conditional statements in Python languages decide the direction (Control
Flow) of the flow of program execution.
Types of Control Flow in Python
Python control flow statements are as follows:
1. The if statement
2. The if-else statement
3. The nested-if statement
4. The if-elif-else ladder

1. Python if statement

The if statement is the most simple decision-making statement.


It is used to decide whether a certain statement or block of statements will be
executed or not.

Syntax:

if condition:
# Statements to execute if
# condition is true

Here, the condition after evaluation will be either true or false. if the statement
accepts boolean values – if the value is true then it will execute the block of
statements below it otherwise not.
Flowchart of Python if statement

Let’s look at the flow of code in the If statement


Example of Python if Statement

# Python program to illustrate If statement

i = 10

if (i > 15):
print("10 is less than 15")
print("I am Not in if")

Output:

I am Not in if
2. Python If-Else Statement
The if statement alone tells us that if a condition is true, it will execute a block of
statements and if the condition is false, it won’t.

But if we want to do something else if the condition is false, we can use the else
statement with the if statement to execute a block of code when the if condition is
false.

Syntax of Python If-Else:

if (condition):

# Executes this block if

# condition is true

else:

# Executes this block if

# condition is false

Flowchart of Python if-else statement


Let’s look at the flow of code in an if-else statement
Example:

# Python program to illustrate If else statement

i = 20

if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")

else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
3. Nested-If Statement in Python

A nested if is an if statement that is the target of another if statement.

Nested if statements mean an if statement inside another if statement.

“Yes, Python allows us to nest if statements within if statements. i.e., we can place
an if statement inside another if statement.”

Syntax:

if (condition1):

# Executes when condition1 is true

if (condition2):

# Executes when condition2 is true

# if Block is end here

# if Block is end here

Else:

#Executes else statement


Flowchart of Python Nested if Statement

Let’s look at the flow of control in Nested if Statements


Example of Python Nested if statement

# Python program to illustrate nested If statement

i = 10
if (i == 10):
# First if statement
if (i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")

else:
print("i is greater than 15")
4. Python if-elif-else Ladder
Here, a user can decide among multiple options.

The if statements are executed from the top down.

As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed.

If none of the conditions is true, then the final “else” statement will be executed.

Syntax:

if (condition):

statement

elif (condition):

statement

else:

statement
Flowchart of Python if-elif-else ladder
Let’s look at the flow of control in if-elif-else ladder:
Example of Python if-elif-else ladder

# Python program to illustrate if-elif-else ladder

i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
PROGRAMS:

Write a Python Program to Check Leap Year.

year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)


# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))

# not divided by 100 means not a century year


# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))

# if not divided by both 400 (century year) and 4 (not century


year)
# year is not leap year
else:
print("{0} is not a leap year".format(year))
Write a Python Program to Find the Factorial of a Number.

num = int(input("Enter a number: "))

factorial = 1
if num <0:
print("Factorial does not exist for negative numbers")

elif num == 0:

print("Factorial of 0 is 1")

else:

for i in range(1, num+1): factorial = factorial*i

print(f'The factorial of {num} is {factorial}')

You might also like