A function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, you define it once and simply 'call' it whenever needed.
def greet_student():
print("Welcome to Samantus Web Training Institute!")
greet_student() # calling the function
greet_student() # can be called as many times as neededParameters are placeholders defined in the function itself; arguments are the actual values you pass in when calling the function.
def greet(name): # "name" is the parameter
print(f"Hello, {name}! Welcome to Samantus.")
greet("Priya") # "Priya" is the argument
greet("Rahul")The return statement sends a value back from the function to wherever it was called — unlike print(), which only displays and does not give back a usable value.
def calculate_gst(price):
gst = price * 0.18
return price + gst
final_price = calculate_gst(1000)
print("Final Price:", final_price)You can give a parameter a default value, which is used automatically if the caller doesn't provide one.
def enroll_student(name, course="Digital Marketing"):
print(f"{name} enrolled in {course}")
enroll_student("Anita") # uses default course
enroll_student("Vikram", "SEO") # overrides defaultKeyword arguments let you pass values by explicitly naming the parameter, so order doesn't matter.
def student_info(name, age, course):
print(f"{name}, Age {age}, Course: {course}")
# Using keyword arguments - order does not matter here
student_info(age=24, course="Google Ads", name="Meena")A lambda function is a small, anonymous, one-line function — useful for short operations where defining a full function feels unnecessary.
square = lambda x: x * x
print(square(5))
add = lambda a, b: a + b
print(add(10, 20))
# Common use: sorting a list of tuples by the second value
students = [("Raj", 85), ("Meena", 92), ("Amit", 78)]
students.sort(key=lambda s: s[1], reverse=True)
print(students)Recursion is when a function calls itself to solve a smaller version of the same problem, until it reaches a stopping point called the base case.
def factorial(n):
if n == 0 or n == 1: # base case - stops the recursion
return 1
return n * factorial(n - 1) # recursive call
print(factorial(5)) # 5*4*3*2*1Scope determines where a variable can be accessed. A local variable exists only inside the function where it's created; a global variable exists throughout the program.
institute_name = "Samantus" # global variable
def show_info():
course = "SEO" # local variable
print(institute_name, "-", course) # can read global here
show_info()
# print(course) # This would cause an ERROR - course is local onlyBuild a small toolkit of functions that could be used in a real training institute's billing system.
def calculate_gst(amount, rate=0.18):
return round(amount * rate, 2)
def apply_discount(amount, discount_percent=0):
return round(amount - (amount * discount_percent / 100), 2)
def final_fee(base_fee, discount_percent=0):
discounted = apply_discount(base_fee, discount_percent)
gst = calculate_gst(discounted)
return discounted + gst
print("Final Fee:", final_fee(15000, discount_percent=10))Basic (5 Questions)
1. Write a function that prints your name and course.
2. Write a function that takes a number and returns its square.
3. Write a function with a default parameter for 'city' set to 'Delhi'.
4. Write a function to add two numbers and return the result.
5. Write a lambda function to double a number.
Intermediate (5 Questions)
1. Write a function to check if a number is prime and return True/False.
2. Write a function using keyword arguments to build a student profile.
3. Write a recursive function to calculate the sum of numbers from 1 to n.
4. Write a function that returns the maximum of 3 numbers without using max().
5. Demonstrate local vs global scope with your own example (not from the notes).
Advanced (5 Questions)
1. Write a recursive function to calculate the Fibonacci number at position n.
2. Build a function-based 'Simple Interest Calculator' with principal, rate, and time as parameters (rate has a default value).
3. Use lambda with sorted() to sort a list of dictionaries by a specific key.
4. Write a function that takes any number of arguments using *args and returns their sum.
5. Build a small toolkit of 3 functions for a 'Marks Analyzer' (average, highest, grade) and combine them in one function.
Q1. What keyword is used to define a function in Python?
Q2. What does the return statement do?
Q3. What is a lambda function?
Q4. What must every recursive function have?
Q5. A variable created inside a function is:
Assignment: Build a Student Fee Management Toolkit
Expected Output: A working, modular fee-management script where each function performs one clear job and can be reused independently.
Use this space to write down key points, doubts, and your own examples from today's session.