Other

Table of contents

The copy module

The copy module provides tools for creating shallow and deep copies of objects, allowing for efficient object duplication and manipulation without affecting the original object.


# A shallow copy (both lists reference the same object)
list1 = [1, 2, 3]
list2 = list1
list2.append(4)
print(list1)

# A shallow copy created using slicing (creates a new list but does not copy nested objects)
list3 = [1, 2, 3]
list4 = list3[:]
list4.append(4)
print(list3)

# The same as the example above but using the copy module
import copy
list5 = [1, 2, 3]
list6 = copy.copy(list5)
list6.append(4)
print(list5)

# A deep copy (creates a completely independent copy, including nested objects)
list7 = [[1, 2], [3, 4]]
list8 = copy.deepcopy(list7)
list8[0].append(5)
print(list7)
                                    

In terms of classes, a shallow copy (copy.copy()) creates a new instance that shares references to nested objects (attributes), whereas a deep copy (copy.deepcopy()) creates a fully independent clone, including all nested objects.

The itertools module

The itertools module provides a collection of tools for working with iterators that operate on sequence items. These tools help create iterators for efficient looping.


import itertools

# Returning an iterator that repeats a given value a specific number of times
repeated = itertools.repeat(10, 3) # value, number of times
for x in repeated:
    print(x)

# Combining multiple iterables into one continuous sequence
chained = itertools.chain([1, 2], ["a", "b"], [True, False])
for item in chained:
    print(item) 

# Returning n-length tuples with combinations of elements from the iterable (sorted in lexicographical order)
combs = itertools.combinations("ABCD", 2)
for comb in combs:
    print(comb)

# The same as combinations(), but each arrangement of elements is treated as a unique permutation (order matters)
perms = itertools.permutations("ABCD", 2)
for perm in perms:
    print(perm)

# Returning the Cartesian product of input iterables (like nested loops)
prod = itertools.product("AB", "12") # ('A','1'), ('A','2'), ('B','1'), ('B','2')
for p in prod:
    print(p)

# Using "repeat" to generate all n-length tuples with repetition allowed
prod_repeat = itertools.product("AB", repeat = 3)
for p in prod_repeat:
    print(p)
                                    

The shelve module

The shelve module allows us to store Python objects in a file using a dictionary-like interface. It automatically serializes objects so that we can save most Python data types, including lists and dictionaries, in a file. The data is persistent, meaning it stays available across program runs. When using shelve.open(), we specify the name of the binary file in which the data will be stored (binary files are more efficient than text files). The main benefits of shelve are its simplicity and automatic persistence, so objects can be easily saved, retrieved, and updated.


import shelve

with shelve.open("mydata") as db:
    db["name"] = "Alice"
    db["age"] = 30
    
with shelve.open("mydata") as db:
    print(db["name"])
    print(db["age"])
                                    

The pyinputplus module

This module is used to simplify input validation by providing functions that ensure user input meets specified criteria, such as type, range, or format.


import pyinputplus as pyip

response = pyip.inputNum(prompt = "Enter a number between 1 and 10: ", min = 1, max = 10)
print("You entered:", response)

response = pyip.inputYesNo(prompt = "Do you want to continue? (yes/no): ")
if response == "yes":
    print("You chose to continue.")
else:
    print("You chose not to continue.")
                                    

The pprint module

This module is used to format and print (pretty-print) complex data structures in a more readable and aesthetically pleasing way.


import pprint

data = {
    "name": "Alice",
    "age": 25,
    "hobbies": ["reading", "hiking", "coding"],
    "details": {
        "location": "New York",
        "languages": ["Python", "JavaScript"],
    },
}

pprint.pprint(data)
                                    

The turtle module

The turtle module is designed for creating drawings and serves as a tool for learning how to code.


from turtle import *

forward(100) # moving forward the given number of pixels (also: fd())
right(90) # turning right by 90 degrees
backward(100) # moving backward the given number of pixels (also: bk() or back())
left(90) # turning left by 90 degrees

circle(100) # drawing a circle
undo() # undoing the last action

for x in range(4):
    left(90)
    dot(20) # placing a dot
    penup() # lifting the pen up
    forward(100)
    pendown() # lowering the pen down

speed(3) # setting the drawing speed (fastest: 0, fast: 10, normal: 6, slow: 3, slowest: 1)
shape("turtle") # setting the turtle's shape (arrow, turtle, circle, square, triangle, and classic)
for x in range(4):
    penup()
    forward(120)
    pendown()
    stamp() # placing a stamp that looks like that the turtle's shape
    if x < 2:
        left(90)
    else:
        right(90)

goto(0, 0) # moving to the specified coordinates
forward(100)

print(position()) # retrieving the current coordinates

pencolor("red") # changing the pen color
forward(100)

fillcolor("red") # setting the fill color
begin_fill() # starting the filling procedure
for x in range(4):
    forward(100)
    right(90)
end_fill() # ending the filling procedure

home() # moving to the (0, 0) coordinates