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.

Statements are instructions executed by the computer. Control structures govern code execution flow: sequential, decision-making, or repetition.
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.
marks = 75
if marks >= 33:
print("Congratulations! You passed.")
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.
age = 20
if age > 18:
print("You are eligible to vote!")
else:
print("Sorry, you are underage.")
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)")
Conditional statements execute distinct code blocks depending on whether conditions evaluate to True or False.
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.
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.")
Inner conditions evaluate only when the outer condition evaluates to True; otherwise the inner conditional block is skipped entirely.
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.

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.
for num in range(1, 6):
print("Number:", num)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.
count = 1 # Starting value
while count <= 5: # Condition check
print("Count:", count)
count = count + 1 # Crucial update step (prevents an infinite loop)Count: 2
Count: 3
Count: 4
Count: 5
Loops automatically repeat code without duplication. For loops iterate over sequences or ranges, while loops execute as long as a condition remains True.
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.
for num in range(1, 10):
if num == 5:
break # Stops the loop completely at 5
print(num)2
3
4
The break statement immediately terminates and exits the entire enclosing loop early when a specific condition is met.
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.
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.

Functions are named reusable task units declared using 'def function_name(parameters):'. Defined instructions only execute when invoked.
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 ().
def greet(name):
print("Hello,", name)
# Calling the function defined above
greet("Aman")
greet("Priya")Hello, Priya
Invoke a defined function by writing its name followed by parentheses with any required arguments: function_name().
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.
def greet(name): # 'name' is the parameter
print("Hello,", name)
greet("Rahul") # "Rahul" is the argument passed in
Arguments supply concrete data values to parameters so the function can process dynamic inputs.
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.
def add(a, b):
return a + b
result = add(5, 3)
print("Sum:", result)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.