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:
Loops are commonly used for displaying records, processing data and performing repetitive operations.
The for loop is used to execute a block of code a fixed number of times.
for variable in sequence:
statements
for i in range(1, 6):
print(i)
1
2
3
4
5
for i in range(5):
print("Shree")
Shree
Shree
Shree
Shree
Shree
| Statement | Output |
|---|---|
| range(5) | 0 to 4 |
| range(1,5) | 1 to 4 |
| range(1,6) | 1 to 5 |
The while loop executes a block of code as long as the specified condition is True.
while condition:
statements
i = 1
while i <= 5:
print(i)
i = i + 1
1
2
3
4
5
count = 1
while count <= 3:
print("Shree")
count = count + 1
Shree
Shree
Shree
Note: Always update the loop variable inside a while loop, otherwise the loop may run forever (Infinite Loop).
Break and Continue statements are used to control loop execution.
The break statement immediately terminates the loop.
for i in range(1, 11):
if i == 6:
break
print(i)
1
2
3
4
5
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)
1
2
4
5
| Break | Continue |
|---|---|
| Terminates the loop completely. | Skips current iteration only. |
| Loop execution stops. | Loop continues with next iteration. |
A function is a block of code that performs a specific task. Functions help us avoid code duplication and make programs easier to manage.
def function_name():
statements
function_name()
def greet():
print("Welcome to Python")
greet()
Welcome to Python
def greet():
print("Hello Student")
greet()
greet()
greet()
Hello Student
Hello Student
Hello Student
Note: A function is created using the def keyword and executed by calling its name.
Parameters allow us to pass values to a function. This makes functions more flexible and reusable.
def greet(name):
print("Hello", name)
greet("Shree")
greet("Vinayak")
Hello Shree
Hello Vinayak
def student(name, city):
print(name, city)
student("Shree", "Mumbai")
Shree Mumbai
def add(num1, num2):
print(num1 + num2)
add(10, 20)
30
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.
def add(num1, num2):
return num1 + num2
result = add(10, 20)
print(result)
30
def get_name():
return "Shree"
name = get_name()
print(name)
Shree
def square(num):
return num * num
print(square(5))
25
Note: A function can return data using the return keyword. Once a return statement executes, the function ends.
| 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)