BP101T • Unit 2: Control Structure and Functions
TOPIC 01

Control Structures & Statements Overview

Control structure and functions:

Control Structures: Rules in code that control the order or flow in which instructions are executed (such as making decisions with if or repeating actions with loops).

Functions: Reusable, named blocks of code designed to perform a specific task whenever you call them.

Statement:

Statement in python is simply instructions written in some lines of code which we provide to our system to perform some actions what we wanted, and computer follows those lines of instructions and then start acting accordingly.

Python Control Structures Overview Cheat Sheet
Figure 2.1: Python control structures — Sequential, Selection (Decision Making), and Repetition (Loops).
✓
Key Takeaway• Quick Recap

Statements are instructions executed by the computer. Control structures govern code execution flow: sequential, decision-making, or repetition.

TOPIC 02

Conditional Decision Making: If, If-Else & If-Elif-Else

Conditional statements:

Conditional statements in Python are decision-making structures that allow a program to execute different blocks of code depending on whether a specific condition evaluates to True or False.

If statement:

It decides whether the given statement or the codes are going to be executed or not based on some defined conditions. If condition satisfies then statements inside If block get executed, otherwise it will not be executed.

Ex:
marks = 75
if marks >= 33:
print("Congratulations! You passed.")
output:
Congratulations! You passed.
Python If Condition Formula Diagram
Figure 2.2: Labelled Python 'if' condition formula diagram.

If-else:

The if-else contains if and else block of code conditions, if the code inside the if block is not satisfied, means if it is false, then the else block of code will get executed.

Ex:
age = 20
if age > 18:
print("You are eligible to vote!")
else:
print("Sorry, you are underage.")
output:
You are eligible to vote!         # because age 20 is greater than 18 hence if block becomes true.
Python If-Else Condition Formula Diagram
Figure 2.3: Python 'if-else' condition formula diagram.

If-elif-else:

In if-else statement there are two options two made decision, If the if block is false then we have 2nd option that is written in else block.

In the if-elif-else the decisions have to be made based on more than two available options or choices.

marks = 70
if marks >= 90:
print("Grade: A+")
elif marks >= 75:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
elif marks >= 33:
print("Grade: C")
else:
print("Grade: F (Fail)")
output:
Grade: A
Python If-Elif-Else Condition Formula Diagram
Figure 2.4: Python 'if-elif-else' condition formula diagram.
✓
Key Takeaway• Quick Recap

Conditional statements execute distinct code blocks depending on whether conditions evaluate to True or False.

TOPIC 03

Nested Conditions (Decisions Inside Decisions)

Nested conditions:

A nested condition is a conditional statement (if, if-else, or if-elif-else) placed inside another conditional block. In this the inner condition is evaluated only, if the outer condition becomes True. If the outer condition is False, Python skips the entire inner block completely.

Ex: Two-Step Login Verification (Username & Password):
username = "admin"
password = "123"

if username == "admin": # Outer check: Is the username right?
if password == "123": # Inner check: Only checked if username matched
print("Login Successful!")
else:
print("Incorrect Password.")
else:
print("User not found.")
output:
Login Successful!
Python Nested Condition Formula Diagram
Figure 2.5: Python 'nested' condition formula structure diagram.
✓
Key Takeaway• Quick Recap

Inner conditions evaluate only when the outer condition evaluates to True; otherwise the inner conditional block is skipped entirely.

TOPIC 04

Loops in Python: Definite (For) and Indefinite (While) Loops

Loops in python:

A loop in Python is a control flow structure used to repeat a block of code multiple times automatically without writing the same lines over and over again.

Instead of running code once, a loop keeps executing it either for a fixed number of items or as long as a specific condition remains True.

Python Loops For and While Cheat Sheet
Figure 2.6: Overview of Python loops — 'for' loop and 'while' loop.

For loop:

A for loop is used to repeat over a sequence (such as a list, string, tuple, or a range() of numbers). It automatically takes one item at a time from the sequence, stores it in a loop variable, and executes the indented code block once for each item until the sequence ends.

Ex:
for num in range(1, 6):
print("Number:", num)
output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

In above ex. we provide range value using range function to the for loop, the loop here takes values from that range starting 0 to last value which 6 and loops ends at 6-1, because in python the end value never get return as the result.

While loop:

A while loop repeatedly executes a block of code as long as a given condition returns True. it will keeps running until the condition becomes False.

Ex: Counting from 1 to 5
count = 1 # Starting value

while count <= 5: # Condition check
print("Count:", count)
count = count + 1 # Crucial update step (prevents an infinite loop)
output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
✓
Key Takeaway• Quick Recap

Loops automatically repeat code without duplication. For loops iterate over sequences or ranges, while loops execute as long as a condition remains True.

TOPIC 05

Loop Control: The Break Statement

Break and continuous statements:

The break statement immediately stops and exits the entire loop when a specific condition is satisfied, even if the loop still has more items or reparations task are left.

Ex:
for num in range(1, 10):
if num == 5:
break # Stops the loop completely at 5
print(num)
output:
1
2
3
4
✓
Key Takeaway• Quick Recap

The break statement immediately terminates and exits the entire enclosing loop early when a specific condition is met.

TOPIC 06

Defining Functions (def keyword)

Definition functions:

Defining a function means instructing Python a new, reusable task and giving it a name using the def keyword. Python use these instruction at runtime.

Ex:
def greet(name):
print("Hello,", name)

greet("Aman")

Here, we only told Python how to greet someone; nothing prints on the screen until we call it.

Python Function Formulas Defining and Calling Diagram
Figure 2.7: Python function formulas — defining a function and calling a function.
✓
Key Takeaway• Quick Recap

Functions are named reusable task units declared using 'def function_name(parameters):'. Defined instructions only execute when invoked.

TOPIC 07

Calling and Executing Functions

Calling functions:

Calling a function means use of the functions that we may had defined by writing the function's name followed by parentheses ().

Ex: Like in previous example we already defined to greet someone.
def greet(name):
print("Hello,", name)

# Calling the function defined above
greet("Aman")
greet("Priya")
output:
Hello, Aman
Hello, Priya
✓
Key Takeaway• Quick Recap

Invoke a defined function by writing its name followed by parentheses with any required arguments: function_name().

TOPIC 08

Passing Arguments into Functions

Passing arguments:

Passing arguments is the process of sending values into a function when you call it, so the function can work with that specific information.

Ex:
def greet(name): # 'name' is the parameter
print("Hello,", name)

greet("Rahul") # "Rahul" is the argument passed in
output:
Hello, Rahul
Passing Arguments and Returning Values in Python Functions
Figure 2.8: Visual breakdown of passing arguments and returning values in Python.
✓
Key Takeaway• Quick Recap

Arguments supply concrete data values to parameters so the function can process dynamic inputs.

TOPIC 09

Returning Values from Functions (return keyword)

Returning values:

It is the process of sending the final result back from a function using the return keyword so it can be saved or reused.

Ex:
def add(a, b):
return a + b

result = add(5, 3)
print("Sum:", result)
✓
Key Takeaway• Quick Recap

The return statement passes the final computed result back from the function so it can be captured in a variable or used in subsequent calculations.