Python Day 3 (2 Day Project)
Python Day 3 (2 Day Project)
Notes
Data Structures
1. Lists
- Items in a list can be of any data type and lists can contain a mix of different types.
- Lists are defined using square brackets: my_list = [1, 2, 3, 'a', 'b', 'c']
2. Tuples
- Slicing: my_tuple[1:3]
3. Dictionaries
- Defined using curly braces: my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
4. Sets
Functions
python
def my_function():
print("Hello, World!")
python
def greet(name):
print(f"Hello, {name}!")
greet('Alice')
return a + b
result = add(2, 3)
python
print(f"{greeting}, {name}!")
greet('Bob')
greet('Alice', 'Hi')
python
return a + b
4. Variable-Length Arguments
python
def sum(args):
return sum(args)
result = sum(1, 2, 3, 4)
- Use kwargs for variable-length keyword arguments:
python
def print_info(kwargs):
print(f"{key}: {value}")
5. Lambda Functions
- Lambda functions are small anonymous functions defined with the lambda keyword:
python
add = lambda x, y: x + y
result = add(2, 3)
- Often used for short, throwaway functions in higher-order functions like map, filter, and
sorted:
python
numbers = [1, 2, 3, 4, 5]
python
def my_function():
x = 10 Local variable
python
x = 10 Global variable
def my_function():
my_function()
python
x = 10
def my_function():
global x
x = 20
my_function()
print(x) Outputs: 20
7. Recursive Functions
python
def factorial(n):
if n == 0:
return 1
else:
return n factorial(n - 1)
result = factorial(5)
Task 4
Here are some questions related to the topics of data structures and functions in Python:
Data Structures
1. Lists
python
my_list = [1, 2, 3, 4, 5]
my_list[2] = 10
print(my_list)
5. Write a Python program to concatenate two lists and sort the result.
2. Tuples
1. Create a tuple containing a mix of integers and strings. Print the tuple.
3. Write a function that accepts a tuple of numbers and returns the sum of all the numbers.
python
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[1:4])
5. Can you modify a tuple after it has been created? Explain with an example.
3. Dictionaries
1. Create a dictionary with keys as names and values as ages. Print the dictionary.
2. How do you access the value associated with the key 'age' in a dictionary called person?
python
del my_dict['b']
print(my_dict)
4. Sets
1. Create a set of unique integers from a list containing duplicate values. Print the set.
python
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))
1. Write a function greet that takes a name as an argument and prints "Hello, <name>!".
2. How do you call a function named calculate that takes two arguments a and b?
1. Write a function multiply that takes two numbers and returns their product.
python
return a - b
result = subtract(10, 5)
print(result)
3. Write a function that takes a list of numbers and returns their average.
1. Write a function greet with a default argument greeting="Hello" and a name. It should print
the greeting followed by the name.
2. How do you call a function add with keyword arguments a=5 and b=3?
3. Write a function divide with two arguments a and b where b has a default value of 1. It
should return the result of division of a by b.
4. Variable-Length Arguments
1. Write a function sum_all that takes any number of arguments and returns their sum.
def print_args(args):
print(arg)
print_args(1, 2, 3)
3. Write a function print_info that takes any number of keyword arguments and prints them.
5. Lambda Functions
1. Write a lambda function that takes two numbers and returns their difference.
2. How do you use a lambda function with the map function to square each number in a list?
3. Write a lambda function to filter even numbers from a list using the filter function.
1. What is the difference between a local and a global variable? Provide an example.
python
x = 10
def my_function():
x=5
print(x)
my_function()
print(x)
7. Recursive Functions
1. Write a recursive function to calculate the factorial of a number.
These questions cover the key concepts and operations for data structures and functions in
Python. They are designed to test both theoretical understanding and practical application
skills.