Python CSV Data Structures

Reading CSV Data into Different Data Structures

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)
    

Explanation

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.

Python Functions Example

Python Functions Example

Defining Functions

def greet(name, msg='Hello'):
    return {msg}, {name}!

print(greet('Alice'))  # Output: Hello, Alice!
print(greet('Bob', 'Hi'))  # Output: Hi, Bob!
    

Lambda Functions

# Inline anonymous function
square = lambda x: x * x
print(square(5))  # Output: 25
    

Decorators

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()
    

Closures and Higher-Order Functions

# 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
    

Python Object-Oriented Programming

Classes and Objects

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def bark(self):
        return 'Woof!'

my_dog = Dog('Buddy', 5)
print(my_dog.name)  # Output: Buddy
print(my_dog.bark())  # Output: Woof!
    

Inheritance and Polymorphism

class Animal:
    def speak(self):
        return 'Some sound'

class Cat(Animal):
    def speak(self):
        return 'Meow'

my_cat = Cat()
print(my_cat.speak())  # Output: Meow
    

Encapsulation

class Person:
    def __init__(self, name, age):
        self.__name = name  # Private attribute
        self.age = age

    def get_name(self):
        return self.__name

person = Person('Alice', 30)
print(person.get_name())  # Output: Alice
# print(person.__name)  # This would raise an AttributeError
    

Dunder Methods

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

    def __str__(self):
        return f'{self.title} by {self.author}'

book = Book('1984', 'George Orwell')
print(book)  # Output: 1984 by George Orwell
    

Python Iterators and Generators Example

Using Iterators and Generators


my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_set = {1, 2, 3, 4, 5}
my_dict = {'a': 1, 'b': 2, 'c': 3}

# Iterating over a list
for item in my_list:
    print(item)

# Iterating over a tuple
for item in my_tuple:
    print(item)

# Iterating over a set
for item in my_set:
    print(item)

# Iterating over a dictionary
for key, value in my_dict.items():
    print(key, value)

Using Generators

# Basic generator function
def my_generator():
    yield 1
    yield 2
    yield 3

for value in my_generator():
    print(value)

# Generator expression
gen_exp = (x for x in range(4))
for value in gen_exp:
    print(value)

Explanation

This Python program demonstrates how to iterate over different data structures i
(Lists, Tuples, Sets, and Dictionaries) and how to use generators.

Python Concurrency

Multithreading vs. Multiprocessing

# Multithreading: Multiple threads within the same process
import threading

def print_numbers():
    for i in range(5):
        print(i)

thread = threading.Thread(target=print_numbers)
thread.start()

# Multiprocessing: Multiple processes with separate memory spaces
import multiprocessing

def print_numbers_mp():
    for i in range(5):
        print(i)

process = multiprocessing.Process(target=print_numbers_mp)
process.start()
process.join()
    

Asyncio

# Asyncio: Writing asynchronous code with async and await
import asyncio

async def async_print_numbers():
    for i in range(5):
        print(i)
        await asyncio.sleep(1)

async def main():
    await async_print_numbers()

asyncio.run(main())