Session Overview

  • Loops
  • For Loop
  • While Loop
  • Break Statement
  • Continue Statement
  • Functions
  • Parameters
  • Return Values

Loops

Loops are used to execute a block of code repeatedly. Instead of writing the same statement multiple times, we can use loops to automate repetitive tasks.

Python provides two main looping statements:

  • for loop
  • while loop

Loops are commonly used for displaying records, processing data and performing repetitive operations.

For Loop

The for loop is used to execute a block of code a fixed number of times.

Syntax
for variable in sequence:
    statements
Example 1 : Print Numbers 1 to 5
for i in range(1, 6):
    print(i)
Output
1
2
3
4
5
Example 2 : Print Student Name 5 Times
for i in range(5):
    print("Shree")
Output
Shree
Shree
Shree
Shree
Shree
Understanding range()
Statement Output
range(5) 0 to 4
range(1,5) 1 to 4
range(1,6) 1 to 5

While Loop

The while loop executes a block of code as long as the specified condition is True.

Syntax
while condition:
    statements
Example 1 : Print Numbers 1 to 5
i = 1

while i <= 5:
    print(i)
    i = i + 1
Output
1
2
3
4
5
Example 2 : Print Student Name 3 Times
count = 1

while count <= 3:
    print("Shree")
    count = count + 1
Output
Shree
Shree
Shree

Note: Always update the loop variable inside a while loop, otherwise the loop may run forever (Infinite Loop).

Break & Continue

Break and Continue statements are used to control loop execution.

Break Statement

The break statement immediately terminates the loop.

for i in range(1, 11):

    if i == 6:
        break

    print(i)
Output
1
2
3
4
5
Continue Statement

The continue statement skips the current iteration and moves to the next iteration.

for i in range(1, 6):

    if i == 3:
        continue

    print(i)
Output
1
2
4
5
Difference Between Break and Continue
Break Continue
Terminates the loop completely. Skips current iteration only.
Loop execution stops. Loop continues with next iteration.

Functions

A function is a block of code that performs a specific task. Functions help us avoid code duplication and make programs easier to manage.

Syntax
def function_name():
    statements

function_name()
Example 1 : Simple Function
def greet():
    print("Welcome to Python")

greet()
Output
Welcome to Python
Example 2 : Calling Function Multiple Times
def greet():
    print("Hello Student")

greet()
greet()
greet()
Output
Hello Student
Hello Student
Hello Student

Note: A function is created using the def keyword and executed by calling its name.

Parameters

Parameters allow us to pass values to a function. This makes functions more flexible and reusable.

Example 1 : Single Parameter
def greet(name):
    print("Hello", name)

greet("Shree")
greet("Vinayak")
Output
Hello Shree
Hello Vinayak
Example 2 : Multiple Parameters
def student(name, city):
    print(name, city)

student("Shree", "Mumbai")
Output
Shree Mumbai
Example 3 : Addition using Parameters
def add(num1, num2):
    print(num1 + num2)

add(10, 20)
Output
30

Return Values

The return statement is used to send a value back from a function. It allows the result of a function to be stored in a variable or used elsewhere in the program.

Example 1 : Returning a Value
def add(num1, num2):
    return num1 + num2

result = add(10, 20)

print(result)
Output
30
Example 2 : Returning Student Name
def get_name():
    return "Shree"

name = get_name()

print(name)
Output
Shree
Example 3 : Returning Square of a Number
def square(num):
    return num * num

print(square(5))
Output
25

Note: A function can return data using the return keyword. Once a return statement executes, the function ends.

print() vs return

print() return
Displays output on screen. Sends value back from function.
Cannot be reused later. Returned value can be stored and reused.
Used for displaying results. Used for processing and calculations.
def add(num1, num2):
    return num1 + num2

result = add(10, 20)

print(result)

Quick Revision

For Loop
  • Fixed Iterations
  • Uses range()
  • Most Common Loop
While Loop
  • Condition Based
  • Runs Until False
  • Update Loop Variable
Break
  • Stops Loop
  • Immediate Exit
  • Used in Search Logic
Continue
  • Skip Iteration
  • Loop Continues
  • Does Not Stop Loop
Functions
  • Reusable Code
  • Created using def
  • Reduces Duplication
Parameters
  • Input to Function
  • Multiple Values Allowed
  • Makes Functions Flexible
Return
  • Sends Value Back
  • Ends Function
  • Result Can Be Reused

Assignment

  1. Print numbers from 1 to 10 using a for loop.
  2. Print numbers from 10 to 1 using a while loop.
  3. Accept a number from the user and display its multiplication table.
  4. Create a function named student() that displays student Name, City and Course.
  5. Create a function named add() that accepts two numbers and returns their addition.