import csv
# Sample CSV data
with open('data.csv', 'r') as file:
reader = csv.reader(file)
# Skipping the header
next(reader)
# Reading into a list
data_list = [row for row in reader]
with open('data.csv', 'r') as file:
reader = csv.reader(file)
next(reader)
# Reading into a tuple
data_tuple = tuple(reader)
with open('data.csv', 'r') as file:
reader = csv.reader(file)
next(reader)
# Reading into a set
data_set = {tuple(row) for row in reader}
with open('data.csv', 'r') as file:
reader = csv.DictReader(file)
# Reading into a dictionary
data_dict = [row for row in reader]
print('List:', data_list)
print('Tuple:', data_tuple)
print('Set:', data_set)
print('Dictionary:', data_dict)
This Python program demonstrates how to read data from a CSV file and load it into different data structures like Lists, Tuples, Sets, and Dictionaries.
def greet(name, msg='Hello'):
return {msg}, {name}!
print(greet('Alice')) # Output: Hello, Alice!
print(greet('Bob', 'Hi')) # Output: Hi, Bob!
# Inline anonymous function
square = lambda x: x * x
print(square(5)) # Output: 25
def decorator_func(func):
def wrapper():
print('Before function call')
func()
print('After function call')
return wrapper
@decorator_func
def say_hello():
print('Hello!')
say_hello()
# Function that returns another function
def make_multiplier(n):
def multiplier(x):
return x * n
return multiplier
times_two = make_multiplier(2)
print(times_two(5)) # Output: 10
# Passing a function as an argument
def apply_func(func, value):
return func(value)
print(apply_func(square, 4)) # Output: 16