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:
def function_name(parameters):
# Code block
return value
📘 Example:
# 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:
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:
# 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
1️⃣ Exercise 1: Greet User
Write a function that takes a name as input and prints a greeting.
Example Output:
Hello, John!
2️⃣ Exercise 2: List Operations
Create a list of 5 numbers. Write a function to:
- Find the sum of all numbers in the list.
- Find the largest number in the list.
- Find the smallest number in the list.
3️⃣ Exercise 3: Check Even or Odd Using a Function
Write a function that takes a number as input and checks if it's even or odd.