Advanced Python programming Exercise

Tarique Akhtar
3 min readJan 12, 2023

--

Advanced Python programming can be a challenging and rewarding experience for developers. In this article, we will discuss some exercises that can help you improve your skills and become proficient in using the language.

1. Implement a Linked List

A linked list is a data structure that consists of a series of nodes, each of which contains an element and a reference to the next node in the list. The last node in the list typically has a reference to null. Implementing a linked list in Python can help you understand how this data structure works and how it can be used to solve certain problems.

class Node:
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:
def __init__(self):
self.head = None

def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node

def print_list(self):
current = self.head
while current:
print(current.data)
current = current.next

A sample usage of the above implementation:

linked_list = LinkedList()
linked_list.append(1)
linked_list.append(2)
linked_list.append(3)
linked_list.print_list()
#Output:
#1
#2
#3

2. Write a Python decorator

Decorators are a powerful feature of Python that allow you to modify the behavior of a function or class without changing its code. By writing your own decorators, you can learn how they work and how to use them to create more reusable and modular code.

def my_decorator(func):
def wrapper(*args, **kwargs):
print("Before calling the function")
result = func(*args, **kwargs)
print("After calling the function")
return result
return wrapper

@my_decorator
def add(a, b):
return a + b

print(add(2, 3))
# Output:
# Before calling the function
# After calling the function
# 5

3. Create a Python module

Modules are a way to organize and reuse code in Python. By creating your own module, you can learn how to structure your code in a logical and maintainable way, and how to import and use it in other parts of your program.

# module.py
def multiply(a, b):
return a * b

def divide(a, b):
return a / b

You can use the above module by importing it into another python file using the import statement.

# main.py
import module

print(module.multiply(2, 3)) # Output: 6
print(module.divide(6, 3)) # Output: 2.0

4. Implement a custom container

Containers in Python are objects that can hold other objects, such as lists, tuples, and dictionaries. To become proficient in advanced Python programming, try implementing your own custom container class that behaves like one of the built-in container types.

class MyContainer:
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
def __len__(self):
return len(self.items)
def __iter__(self):
for item in self.items:
yield item

5. Create a Python package

A package is a way to organize related modules into a single unit that can be easily imported and used in other parts of a program. Creating your own package can help you understand how to structure and distribute your code, and how to use other packages in your projects.

mypackage/
__init__.py
mymodule1.py
mymodule2.py

in __init__.py you can define the package-level logic and import any modules you want to make available when the package is imported. In mymodule1.py and mymodule2.py, you can define the modules you want to include in your package. To use the package, you can use the import statement to import it, and then use the dot notation to access the modules within it.

import mypackage

mypackage.mymodule1.myfunction1()
mypackage.mymodule2.myfunction2()

Please note that the above code is just an example and not functional for an actual execution.

Conclusion

There are many ways to improve your skills in advanced Python programming. By working through these exercises, you can learn new concepts, improve your coding skills, and become more proficient in using the language. It’s important to remember that the goal is to practice and not getting stuck on the initial failures and becoming demotivated. The best way to learn is by doing and by experimenting with the language.

--

--

Tarique Akhtar
Tarique Akhtar

Written by Tarique Akhtar

Data Science Professional, Love to learn new things!!!We can get connected through LinkedIn (https://www.linkedin.com/in/tarique-akhtar-6b902651/)

No responses yet