0% found this document useful (0 votes)
4 views15 pages

Python Functions Lecture

This document provides comprehensive study material on Python functions, covering fundamental concepts, types of functions, parameters, variable scope, recursion, and lambda functions. It includes definitions, syntax, examples, and exam tips to aid in understanding and retention for exam success. Key points emphasize the importance of functions, their structure, and various ways to define and use them effectively.

Uploaded by

shankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Python Functions Lecture

This document provides comprehensive study material on Python functions, covering fundamental concepts, types of functions, parameters, variable scope, recursion, and lambda functions. It includes definitions, syntax, examples, and exam tips to aid in understanding and retention for exam success. Key points emphasize the importance of functions, their structure, and various ways to define and use them effectively.

Uploaded by

shankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Python Functions - Complete Exam Study Material

🎯 EXAM SUCCESS STRATEGY


Study this material systematically. Each section contains definitions, syntax,
examples, and key points that directly appear in exams.

1. FUNCTIONS - FUNDAMENTAL CONCEPTS


✍️DEFINITION (Write this exactly in exams)
A function is a named block of reusable code that performs a specific task. It takes input
parameters, processes them, and optionally returns a result.

WHY FUNCTIONS ARE IMPORTANT (Common 5-mark question)


1. Code Reusability: Write once, use multiple times
2. Modularity: Break complex problems into smaller manageable parts

3. Maintainability: Easy to debug and modify


4. Abstraction: Hide implementation details
5. Organization: Makes code readable and structured

BASIC SYNTAX (Must memorize)


def function_name(parameters):
"""Optional docstring"""
# Function body
return value # Optional

🔥 EXAM TIP: Always include docstring in exam answers for extra marks!

2. TYPES OF FUNCTIONS
A) Built-in Functions (Definition + Examples)
Definition: Pre-defined functions provided by Python that are ready to use.
Common Examples for Exams:
len("Python") # Returns 6
max(10, 20, 5) # Returns 20
min(10, 20, 5) # Returns 5
abs(-15) # Returns 15
round(3.14159, 2) # Returns 3.14
sum([1,2,3,4]) # Returns 10
B) User-defined Functions (Most Important for Exams)
Definition: Functions created by programmers to solve specific problems.
📝 EXAM ANSWER FORMAT:
def calculate_area(length, width):
"""Calculate area of rectangle"""
area = length * width
return area

# Function call
result = calculate_area(5, 3)
print(result) # Output: 15

C) Functions with Return Statement


def add_numbers(a, b):
"""Add two numbers"""
return a + b

result = add_numbers(10, 20) # result = 30

D) Functions without Return Statement (Void Functions)


def display_message(name):
"""Display welcome message"""
print(f"Welcome {name}")

display_message("Alice") # Output: Welcome Alice

🎯 EXAM PATTERN: Questions often ask to differentiate between functions with and without
return statements.

3. PARAMETERS AND ARGUMENTS


✍️DEFINITIONS (Write exactly)
• Parameter: Variable in function definition
• Argument: Actual value passed to function during call
def greet(name): # 'name' is parameter
print(f"Hello {name}")

greet("Alice") # "Alice" is argument

3.1 POSITIONAL ARGUMENTS ⭐⭐⭐


Definition: Arguments passed in specific order matching parameter positions.
📝 EXAM EXAMPLE:
def student_info(name, age, grade):
"""Display student information"""
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Grade: {grade}")

# Correct order - IMPORTANT!


student_info("John", 18, "A")

# Output:
# Name: John
# Age: 18
# Grade: A

🚨 COMMON EXAM MISTAKE: Wrong order gives wrong results


student_info(18, "John", "A") # WRONG! Age appears as name

3.2 KEYWORD ARGUMENTS ⭐⭐⭐


Definition: Arguments passed by explicitly naming the parameter.
📝 EXAM EXAMPLE:
def create_account(name, balance, account_type):
"""Create bank account"""
print(f"Account holder: {name}")
print(f"Balance: ${balance}")
print(f"Type: {account_type}")

# Order doesn't matter with keyword arguments


create_account(balance=1000, account_type="Savings", name="Alice")

# Mixing positional and keyword (positional must come first)


create_account("Bob", balance=500, account_type="Current")

🎯 EXAM TIP: In mixed arguments, positional arguments MUST come before keyword
arguments.

3.3 DEFAULT PARAMETERS ⭐⭐⭐


Definition: Parameters with default values, making them optional during function call.
📝 EXAM ANSWER FORMAT:
def calculate_interest(principal, rate=5.0, time=1):
"""Calculate simple interest with default rate and time"""
interest = (principal * rate * time) / 100
return interest

# Using defaults
print(calculate_interest(1000)) # Output: 50.0
# Overriding some defaults
print(calculate_interest(1000, rate=7.0)) # Output: 70.0

# Overriding all parameters


print(calculate_interest(1000, rate=8.0, time=2)) # Output: 160.0

🔥 IMPORTANT RULE: Default parameters must come after non-default parameters.


# CORRECT
def func(a, b=10, c=20):
pass

# WRONG - SyntaxError
def func(a=5, b, c=20): # Non-default after default
pass

3.4 ARBITRARY ARGUMENTS ⭐⭐⭐

**A) *args (Variable Positional Arguments)**


Definition: Allows function to accept any number of positional arguments.
📝 COMPLETE EXAM ANSWER:
def calculate_average(*numbers):
"""Calculate average of any number of values"""
if len(numbers) == 0:
return 0

total = sum(numbers)
average = total / len(numbers)
return average

# Can pass any number of arguments


print(calculate_average(10, 20, 30)) # Output: 20.0
print(calculate_average(5, 15, 25, 35)) # Output: 20.0
print(calculate_average(100)) # Output: 100.0

**How *args works**: - Collects extra positional arguments into a tuple - Can be used with
regular parameters - Must come after regular parameters

B) kwargs (Variable Keyword Arguments)**


Definition: Allows function to accept any number of keyword arguments.
📝 COMPLETE EXAM ANSWER:
def create_student_record(name, grade, **details):
"""Create student record with flexible additional details"""
print(f"Student: {name}, Grade: {grade}")
print("Additional Information:")
for key, value in details.items():
print(f" {key}: {value}")

# Can pass any number of keyword arguments


create_student_record("Alice", "A", age=18, city="Delhi",
hobby="Reading")

# Output:
# Student: Alice, Grade: A
# Additional Information:
# age: 18
# city: Delhi
# hobby: Reading

How kwargs works**: - Collects extra keyword arguments into a dictionary - Parameter
name can be anything (not just ‘kwargs’) - Must come after *args if both are used

C) Combining All Parameter Types


📝 EXAM STANDARD FORMAT:
def complete_function(required, default_param="default", *args,
**kwargs):
"""Function demonstrating all parameter types"""
print(f"Required: {required}")
print(f"Default: {default_param}")
print(f"Args: {args}")
print(f"Kwargs: {kwargs}")

# Example calls
complete_function("A")
complete_function("A", "B", 1, 2, 3, name="John", age=25)

🔥 PARAMETER ORDER RULE (MEMORIZE THIS): 1. Regular parameters 2. Default


parameters
3. *args 4. **kwargs

4. SCOPE OF VARIABLES ⭐⭐⭐


✍️DEFINITION
Scope determines the visibility and accessibility of variables in different parts of a program.

4.1 LOCAL SCOPE


Definition: Variables defined inside a function are local and only accessible within that
function.
📝 EXAM EXAMPLE:
def calculate_tax():
"""Demonstrate local scope"""
salary = 50000 # Local variable
tax_rate = 0.1 # Local variable
tax = salary * tax_rate

print(f"Salary: {salary}")
print(f"Tax: {tax}")

calculate_tax()

# This will give NameError


# print(salary) # Error: name 'salary' is not defined

Key Points for Exam: - Local variables exist only inside the function - Cannot be accessed
outside the function - Different functions can have variables with same names

4.2 GLOBAL SCOPE


Definition: Variables defined outside functions are global and accessible throughout the
program.
📝 EXAM EXAMPLE:
# Global variables
company_name = "TechCorp" # Global
total_employees = 100 # Global

def display_info():
"""Access global variables"""
print(f"Company: {company_name}") # Accessing global
print(f"Employees: {total_employees}") # Accessing global

def add_employee():
"""Modify global variable"""
global total_employees # Must use 'global' keyword
total_employees += 1
print(f"Total employees now: {total_employees}")

display_info() # Works fine


add_employee() # Modifies global variable
display_info() # Shows updated value

4.3 GLOBAL KEYWORD ⭐⭐⭐


📝 EXAM RULE: To modify a global variable inside a function, use the global keyword.
Complete Example for Exams:
balance = 1000 # Global variable

def deposit(amount):
"""Deposit money - modifies global variable"""
global balance # MUST declare global
balance += amount
print(f"Deposited: ${amount}")
print(f"New balance: ${balance}")

def withdraw(amount):
"""Withdraw money - modifies global variable"""
global balance # MUST declare global
if balance >= amount:
balance -= amount
print(f"Withdrawn: ${amount}")
print(f"Remaining balance: ${balance}")
else:
print("Insufficient funds!")

def check_balance():
"""Check balance - only reads global variable"""
print(f"Current balance: ${balance}") # No 'global' needed for
reading

# Function calls
check_balance() # Current balance: $1000
deposit(500) # Deposited: $500, New balance: $1500
withdraw(200) # Withdrawn: $200, Remaining balance: $1300
check_balance() # Current balance: $1300

🚨 COMMON EXAM ERROR:


counter = 0

def increment():
counter = counter + 1 # ERROR! UnboundLocalError
print(counter)

# CORRECT VERSION:
def increment():
global counter
counter = counter + 1
print(counter)
5. RECURSION ⭐⭐⭐⭐
✍️DEFINITION (Write exactly in exam)
Recursion is a programming technique where a function calls itself to solve a problem by
breaking it down into smaller, similar subproblems.

COMPONENTS OF RECURSION (Must memorize)


1. Base Case: Condition that stops recursion (prevents infinite loop)
2. Recursive Case: Function calls itself with modified parameters

5.1 FACTORIAL USING RECURSION (Most Common Exam Question)


Mathematical Definition: - factorial(0) = 1 - factorial(1) = 1
- factorial(n) = n × factorial(n-1) for n > 1
📝 COMPLETE EXAM ANSWER:
def factorial(n):
"""Calculate factorial using recursion"""
# Base case
if n == 0 or n == 1:
return 1

# Recursive case
return n * factorial(n - 1)

# Test cases
print(f"5! = {factorial(5)}") # Output: 5! = 120
print(f"0! = {factorial(0)}") # Output: 0! = 1
print(f"1! = {factorial(1)}") # Output: 1! = 1

How factorial(5) works (Explain in exam):


factorial(5) = 5 * factorial(4)
factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
factorial(2) = 2 * factorial(1)
factorial(1) = 1 (base case)

Working backwards:
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
factorial(4) = 4 * 6 = 24
factorial(5) = 5 * 24 = 120

5.2 FIBONACCI SEQUENCE (Second Most Common)


Mathematical Definition: - fibonacci(0) = 0 - fibonacci(1) = 1 - fibonacci(n) = fibonacci(n-
1) + fibonacci(n-2) for n > 1
📝 COMPLETE EXAM ANSWER:
def fibonacci(n):
"""Calculate nth Fibonacci number using recursion"""
# Base cases
if n == 0:
return 0
elif n == 1:
return 1

# Recursive case
return fibonacci(n - 1) + fibonacci(n - 2)

# Generate Fibonacci sequence


print("First 8 Fibonacci numbers:")
for i in range(8):
print(f"F({i}) = {fibonacci(i)}")

# Output:
# F(0) = 0, F(1) = 1, F(2) = 1, F(3) = 2
# F(4) = 3, F(5) = 5, F(6) = 8, F(7) = 13

5.3 SUM OF DIGITS (Common 5-mark question)


📝 EXAM SOLUTION:
def sum_of_digits(n):
"""Calculate sum of digits using recursion"""
# Base case
if n == 0:
return 0

# Recursive case
return (n % 10) + sum_of_digits(n // 10)

# Test
print(sum_of_digits(12345)) # Output: 15 (1+2+3+4+5)
print(sum_of_digits(987)) # Output: 24 (9+8+7)

5.4 POWER CALCULATION


📝 EXAM SOLUTION:
def power(base, exponent):
"""Calculate base^exponent using recursion"""
# Base case
if exponent == 0:
return 1

# Recursive case
return base * power(base, exponent - 1)
print(power(2, 5)) # Output: 32 (2^5)
print(power(3, 4)) # Output: 81 (3^4)

🎯 EXAM TIPS FOR RECURSION:


1. Always identify and write base case first
2. Ensure recursive case moves toward base case
3. Test with small values
4. Explain the flow step by step

6. LAMBDA FUNCTIONS ⭐⭐⭐⭐


✍️DEFINITION (Write exactly)
Lambda functions are small, anonymous functions defined using the lambda keyword.
They can have multiple arguments but only one expression.

SYNTAX (Must memorize)


lambda arguments: expression

6.1 BASIC LAMBDA EXAMPLES


📝 EXAM ANSWERS:
# Simple lambda functions
square = lambda x: x ** 2
print(square(5)) # Output: 25

add = lambda a, b: a + b
print(add(10, 20)) # Output: 30

# Multiple arguments
calculate = lambda x, y, z: (x + y) * z
print(calculate(2, 3, 4)) # Output: 20

# String operations
full_name = lambda first, last: f"{first} {last}"
print(full_name("John", "Doe")) # Output: John Doe

6.2 LAMBDA WITH BUILT-IN FUNCTIONS (Very Important for Exams)

A) Lambda with map()


Definition: map() applies a function to all items in a list.
📝 EXAM EXAMPLE:
# Convert Celsius to Fahrenheit
celsius = [0, 20, 30, 40]
fahrenheit = list(map(lambda c: (c * 9/5) + 32, celsius))
print(fahrenheit) # Output: [32.0, 68.0, 86.0, 104.0]

# Square all numbers


numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
print(squared) # Output: [1, 4, 9, 16, 25]

B) Lambda with filter()


Definition: filter() selects items from a list based on a condition.
📝 EXAM EXAMPLE:
# Filter even numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even = list(filter(lambda x: x % 2 == 0, numbers))
print(even) # Output: [2, 4, 6, 8, 10]

# Filter strings with length > 5


words = ["python", "java", "html", "javascript", "css"]
long_words = list(filter(lambda word: len(word) > 5, words))
print(long_words) # Output: ['python', 'javascript']

C) Lambda with sorted()


Definition: sorted() sorts a list using a custom key function.
📝 EXAM EXAMPLE:
# Sort list of tuples by second element
students = [("Alice", 85), ("Bob", 92), ("Charlie", 78)]

# Sort by marks
by_marks = sorted(students, key=lambda student: student[1])
print(by_marks) # Output: [('Charlie', 78), ('Alice', 85), ('Bob',
92)]

# Sort by name length


by_name_length = sorted(students, key=lambda student: len(student[0]))
print(by_name_length) # Output: [('Bob', 92), ('Alice', 85),
('Charlie', 78)]

6.3 LAMBDA vs REGULAR FUNCTIONS (Common Exam Question)


📝 COMPARISON TABLE FOR EXAMS:

Lambda Functions Regular Functions


Anonymous (no name) Named
Single expression only Multiple statements
Lambda Functions Regular Functions
Return automatically Explicit return statement
Used for simple operations Used for complex logic
Cannot have docstring Can have docstring

Example Comparison:
# Lambda function
square_lambda = lambda x: x ** 2

# Equivalent regular function


def square_function(x):
"""Square a number"""
return x ** 2

# Both produce same result


print(square_lambda(5)) # Output: 25
print(square_function(5)) # Output: 25

6.4 PRACTICAL LAMBDA EXAMPLES (For 10-mark questions)


📝 COMPLETE EXAM SOLUTION:
# Employee data processing
employees = [
{"name": "Alice", "salary": 50000, "dept": "IT"},
{"name": "Bob", "salary": 60000, "dept": "HR"},
{"name": "Charlie", "salary": 55000, "dept": "IT"},
{"name": "Diana", "salary": 65000, "dept": "Finance"}
]

# 1. Filter employees with salary > 55000


high_earners = list(filter(lambda emp: emp["salary"] > 55000,
employees))
print("High earners:", [emp["name"] for emp in high_earners])

# 2. Sort employees by salary


sorted_by_salary = sorted(employees, key=lambda emp: emp["salary"])
print("Sorted by salary:", [emp["name"] for emp in sorted_by_salary])

# 3. Calculate annual bonus (10% of salary)


with_bonus = list(map(lambda emp: {**emp, "bonus": emp["salary"] *
0.1}, employees))
for emp in with_bonus:
print(f"{emp['name']}: Salary {emp['salary']}, Bonus
{emp['bonus']}")
7. EXAM-STYLE PRACTICE QUESTIONS & ANSWERS
Question 1 (5 marks): Write a function with default parameters
📝 ANSWER:
def calculate_bill(units, rate_per_unit=5.0, tax_percent=10.0):
"""Calculate electricity bill with default rate and tax"""
basic_amount = units * rate_per_unit
tax_amount = (basic_amount * tax_percent) / 100
total_amount = basic_amount + tax_amount

print(f"Units consumed: {units}")


print(f"Rate per unit: ₹{rate_per_unit}")
print(f"Basic amount: ₹{basic_amount}")
print(f"Tax ({tax_percent}%): ₹{tax_amount}")
print(f"Total amount: ₹{total_amount}")

return total_amount

# Test with different parameter combinations


calculate_bill(100) # Using all defaults
calculate_bill(150, rate_per_unit=6.0) # Custom rate
calculate_bill(200, 7.0, 15.0) # All custom values

Question 2 (8 marks): Write recursive function for GCD


📝 ANSWER:
def gcd(a, b):
"""Find Greatest Common Divisor using Euclidean algorithm"""
# Base case
if b == 0:
return a

# Recursive case
return gcd(b, a % b)

# Test cases
print(f"GCD of 48 and 18: {gcd(48, 18)}") # Output: 6
print(f"GCD of 100 and 25: {gcd(100, 25)}") # Output: 25
print(f"GCD of 17 and 13: {gcd(17, 13)}") # Output: 1

# How gcd(48, 18) works:


# gcd(48, 18) → gcd(18, 48%18) → gcd(18, 12)
# gcd(18, 12) → gcd(12, 18%12) → gcd(12, 6)
# gcd(12, 6) → gcd(6, 12%6) → gcd(6, 0)
# gcd(6, 0) → 6 (base case)
Question 3 (10 marks): Lambda functions with data processing
📝 ANSWER:
# Student data
students = [
{"name": "Rahul", "marks": [85, 90, 78], "city": "Delhi"},
{"name": "Priya", "marks": [92, 88, 95], "city": "Mumbai"},
{"name": "Amit", "marks": [76, 82, 80], "city": "Chennai"},
{"name": "Sneha", "marks": [89, 91, 87], "city": "Delhi"}
]

# 1. Calculate average marks for each student


students_with_avg = list(map(
lambda student: {
**student,
"average": sum(student["marks"]) / len(student["marks"])
},
students
))

# 2. Filter students with average > 85


top_students = list(filter(lambda s: s["average"] > 85,
students_with_avg))

# 3. Sort students by average marks (descending)


sorted_students = sorted(students_with_avg, key=lambda s:
s["average"], reverse=True)

# 4. Get names of students from Delhi


delhi_students = list(map(
lambda s: s["name"],
filter(lambda s: s["city"] == "Delhi", students)
))

# Display results
print("All students with averages:")
for student in students_with_avg:
print(f"{student['name']}: {student['average']:.2f}")

print(f"\nTop students (avg > 85): {[s['name'] for s in


top_students]}")
print(f"Delhi students: {delhi_students}")

8. KEY POINTS FOR EXAM SUCCESS


🔥 MUST REMEMBER SYNTAX
1. Function Definition:
def function_name(parameters):
"""docstring"""
return value

2. Global Variable:

global variable_name

3. Lambda Syntax:

lambda arguments: expression

4. Recursive Structure:

if base_case:
return base_value
return recursive_call

🎯 COMMON EXAM MISTAKES TO AVOID


1. ❌ Forgetting global keyword when modifying global variables
2. ❌ Wrong parameter order in function definition (default after non-default)
3. ❌ Missing base case in recursion
4. ❌ Using return in lambda (it’s automatic)
5. ❌ Forgetting to convert map/filter results to list

⭐ GUARANTEED MARKS STRATEGY


1. Always write docstrings in function definitions (+1 mark)
2. Include test cases with your answers (+2 marks)
3. Explain recursion flow step by step (+2 marks)
4. Show output for your examples (+1 mark)
5. Use meaningful variable names (+1 mark)

📚 TOPIC WEIGHTAGE FOR PREPARATION


• Recursion: 25% (Most important - practice factorial, fibonacci, GCD)
• Lambda functions: 25% (Focus on map, filter, sorted)
• Parameter types: 20% (Default, *args, **kwargs)
• Scope: 15% (Global keyword usage)
• Function basics: 15% (Types, syntax, return values)
🎯 FINAL EXAM TIP: Practice writing complete programs, not just functions. Exams often
ask for full solutions with function calls and output display.**

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy