■ Assignment – 2
Topics Covered:
1. Functions
2. Lists
3. Dictionary
4. Tuples
■ Topic 1: Functions
A function is a block of code written once but can be used multiple times. It helps avoid repetition
and makes code simple. Functions in Python are defined using the keyword def.
Syntax:
def function_name(parameters):
# block of code
return value
Example:
def greet(name):
return "Hello, " + name
print(greet("Asha"))
Types of Functions:
1. Built-in Functions – Example: len(), max(), print()
2. User-defined Functions – Functions created by programmers
3. Lambda Functions – Anonymous, single-line functions
Programs:
1. Function to check prime number
2. Function to calculate factorial
3. Function to find area of circle
■ Topic 2: Lists
A list is a collection of items that are ordered, changeable (mutable), and can contain duplicates.
Syntax:
list_name = [item1, item2, item3]
Example:
fruits = ["apple", "banana", "cherry"]
[Link]("orange")
print(fruits)
List Operations:
• Accessing elements with index (list[0])
• Adding elements → append(), insert()
• Removing → remove(), pop()
• Sorting → sort()
Programs:
1. Create a list of 10 numbers and find their sum
2. Find the maximum and minimum numbers from a list
3. Write a program to reverse a list
■ Topic 3: Dictionary
A dictionary stores data in key–value pairs. Example: {"name": "Asha", "age": 20}. It is fast for
searching values.
Syntax:
dict_name = {key1: value1, key2: value2}
Example:
student = {"name": "Ali", "age": 21, "course": "BCA"}
print(student["name"])
Dictionary Methods:
• keys(), values(), items()
• get()
• update()
• pop()
Programs:
1. Store roll number and name of 5 students in a dictionary
2. Write a program to count frequency of each word in a string
3. Write a program to update marks of students in a dictionary
■ Topic 4: Tuples
A tuple is like a list but cannot be changed (immutable). Tuples are faster than lists and are defined
using parentheses ().
Syntax:
tuple_name = (item1, item2, item3)
Example:
colors = ("red", "green", "blue")
print(colors[1])
Tuple Uses:
• Data that should not change
• Returning multiple values from a function
Programs:
1. Create a tuple of 10 numbers and find the sum
2. Write a program to check if an element exists in a tuple
3. Convert tuple to list and list to tuple
■ Small Project Ideas
1. Functions Project: Calculator using functions
2. Lists Project: Student marks management system
3. Dictionary Project: Mini phonebook
4. Tuple Project: Store coordinates (x, y, z) and display them
■ Instruction: Answer all theory questions, run all programs, and attach outputs. Submit on
Monday.