Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Python programming

Rating
-
Sold
-
Pages
44
Uploaded on
05-09-2024
Written in
2024/2025

Lecture notes of 44 pages for the course 22k at ACCA - Oxford Brookes University (Basics of python)

Institution
Course

Content preview

UNIT -2

CONTROL STAMENTS

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

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

,Example of Python 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
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.
 The block of code following the else statement is executed as the condition
present in the if statement is false after calling the statement which is not in the
block(without spaces).
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

, Example for If..Else:
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")

Output:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block

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.
 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

, Example of Python 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")
Output:
i is smaller than 15
i is smaller than 12 too


Python if-elif-else Ladder

 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

Written for

Institution
Course

Document information

Uploaded on
September 5, 2024
Number of pages
44
Written in
2024/2025
Type
Class notes
Professor(s)
Sathya
Contains
All classes

Subjects

$8.29
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sathyaoffici

Get to know the seller

Seller avatar
sathyaoffici Madonna matriculation school
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
16
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions