Select a Subtopic
Day 3: Python Functions, Lists, and Basic Operations
Topics to Cover
- Functions in Python
- Lists and Basic Operations
- List Methods
- Exercises for Day 3
1. Functions in Python
A function is a block of reusable code that performs a specific task. Functions make your code modular and readable.
✅ Syntax:
1
2
3
def function_name(parameters):
# Code block
return value
📘 Example:
1
2
3
4
5
6
# Function to greet the user
def greet(name):
print("Hello, " + name + "!")
# Calling the function
greet("John")
2. Lists and Basic Operations
A list is a collection of items that is ordered and mutable (you can change it). Lists can hold different types of data.
✅ Creating a List:
1
2
fruits = ["apple", "banana", "cherry"]
print(fruits)
🔧 Basic Operations on Lists:
Operation | Example | Description |
---|---|---|
Access item | fruits[0] | Access the first item |
Change item | fruits[1] = "grape" | Change the second item |
Add item | fruits.append("mango") | Add item at the end |
Remove item | fruits.remove("apple") | Remove an item |
Length of list | len(fruits) | Find the number of items in the list |
📘 Example:
1
2
3
4
5
6
7
# Working with lists
fruits = ["apple", "banana", "cherry"]
fruits.append("mango")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'mango']
# Accessing items
print(fruits[0]) # Output: apple
Exercises for Day 3
- Exercise 1: Greet User - Write a function that takes a name as input and prints a greeting.
- Exercise 2: List Operations - Create a list of 5 numbers. Write a function to find the sum, largest, and smallest numbers.
- Exercise 3: Check Even or Odd - Write a function to check if a number is even or odd.
- Exercise 4: Working with Lists - Create a list of fruits, add a fruit, remove a fruit, and sort the list.