A collection of notes and code snippets for learning Python.
Published
June 3, 2025
This is a collection of notes and code snippets that I find useful when learning Python. It is not meant to be a comprehensive guide, but rather a collection of tips and tricks that I have found helpful.
It will be broken down into sections, each covering a different aspect of Python programming.
Operations
Value Types
Data Structures
Control Flow
Functions
Classes
Modules
Exceptions
File I/O
Debugging
Performance
Operations
Operations in Python are the basic mathematical and logical operations that can be performed on values. These include addition, subtraction, multiplication, division, exponentiation, floor division, and modulus.
# Additionadd=1+1print(f"We add two numbers like 1 + 1 and get {add}")# Subtractionsub =2-1print(f"We subtract two numbers like 2 - 1 and get {sub}")# Multiplicationmul =3*4print(f"We multiply two numbers like 3 * 4 and get {mul}")# Divisiondiv =10/2print(f"We divide two numbers like 10 / 2 and get {div}")# Exponentiationexp =2**3print(f"We raise a number to a powerlike 2 ** 3 and get {exp}")# Floor Divisionfloor_div =7//3print(f"We perform floor division like 7 // 3 and get {floor_div}")# Modulusmod =7%3print(f"We find the modulus of a division like 7 % 3 and get {mod}")
We add two numbers like 1 + 1 and get 2
We subtract two numbers like 2 - 1 and get 1
We multiply two numbers like 3 * 4 and get 12
We divide two numbers like 10 / 2 and get 5.0
We raise a number to a powerlike 2 ** 3 and get 8
We perform floor division like 7 // 3 and get 2
We find the modulus of a division like 7 % 3 and get 1
Value Types
Value types are the basic data types in Python. They include integers, floats, strings, booleans, and NoneType. Each value type has its own characteristics and use cases.
Data structures are used to store and organize data in Python. They include lists, tuples, sets, dictionaries, and strings. Each data structure has its own characteristics and use cases.
Control flow statements allow you to control the execution of your code based on certain conditions. They include if-else statements, loops (for and while), and more.
# If-Else Statementx =10if x >5:print("x is greater than 5")else:print("x is not greater than 5")# For Loopfor i inrange(5): print(f"Iteration {i}")# While Loopcount =0while count <5:print(f"Count is {count}") count +=1
x is greater than 5
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Count is 0
Count is 1
Count is 2
Count is 3
Count is 4
Functions
A function is a reusable block of code that performs a specific task. Functions can take inputs (arguments) and return outputs (results). They help in organizing code, making it more readable and maintainable.
# Function Definitiondef greet(name): returnf"Hello, {name}!"# Function Call print(greet("Alice"))# Function with Default Argumentdef add(a, b=5):return a + b# Function Call with Default Argumentprint(add(10)) # Uses default value for b# Function with Variable Number of Argumentsdef sum_all(*args):returnsum(args)# Function Call with Variable Argumentsprint(sum_all(1, 2, 3, 4, 5))# Lambda Functionadd =lambda x, y: x + yprint(add(5, 10))# Function with Keyword Argumentsdef person_info(name, age):returnf"{name} is {age} years old."print(person_info(name="Bob", age=30))# Function with Keyword-Only Argumentsdef multiply(*, a, b):return a * bprint(multiply(a=3, b=4))# Function with Positional-Only Argumentsdef divide(a, b):return a / bprint(divide(10, 2))# Function with Annotationsdef add_numbers(a: int, b: int) ->int:return a + bprint(add_numbers(5, 10))# Function with Docstringdef subtract(a, b):"""Subtracts b from a."""return a - bprint(subtract(10, 5))
Hello, Alice!
15
15
15
Bob is 30 years old.
12
5.0
15
5
Classes
A class is a blueprint for creating objects. Objects are instances of classes, and they can have attributes (data) and methods (functions) associated with them. Classes allow you to encapsulate data and behavior together, promoting code reusability and organization.
Defining classes in Python allows you to create your own data types and encapsulate data and behavior together. Here are some examples of class definitions
# Class Definitionclass Dog:def__init__(self, name, age):self.name = nameself.age = agedef bark(self):returnf"{self.name} says Woof!"# Class Instantiationdog = Dog("Buddy", 3)print(dog.bark())
Buddy says Woof!
# Class Inheritanceclass Puppy(Dog):def wag_tail(self):returnf"{self.name} is wagging its tail!"# Class Instantiation puppy = Puppy("Charlie", 1)print(puppy.bark())print(puppy.wag_tail())# Class with Class Methodclass Cat: species ="Feline"def__init__(self, name):self.name = name@classmethoddef get_species(cls):return cls.species# Class Method Callprint(Cat.get_species())# Class with Static Methodclass MathUtils:@staticmethoddef add(a, b):return a + b # Static Method Callprint(MathUtils.add(5, 10))
Charlie says Woof!
Charlie is wagging its tail!
Feline
15
# Class with Propertiesclass Person:def__init__(self, name, age):self._name = nameself._age = age@propertydef name(self):returnself._name@propertydef age(self):returnself._age@age.setterdef age(self, value):if value <0:raiseValueError("Age cannot be negative")self._age = value# Class Instantiationperson = Person("Alice", 30)print(person.name)print(person.age)person.age =35print(person.age)
Alice
30
35
Modules
Modules are files containing Python code that can be imported and used in other Python programs. They allow you to organize your code into reusable components, making it easier to manage and maintain.
# Importing a Moduleimport mathprint(math.sqrt(16))print(math.pi) # Importing Specific Functions from a Modulefrom math import sin, cosprint(sin(math.pi /2))print(cos(0))
4.0
3.141592653589793
1.0
1.0
Exceptions
Exceptions are used to handle errors and exceptional conditions in Python. They allow you to gracefully handle errors and prevent your program from crashing. You can raise exceptions, catch them, and handle them appropriately.
# Exception Handlingtry: result =10/0exceptZeroDivisionError:print("Error: Division by zero is not allowed.")# Raising an Exceptiontry: raiseValueError("This is a custom error message.")exceptValueErroras e:print(f"Caught an exception: {e}")
Error: Division by zero is not allowed.
Caught an exception: This is a custom error message.
File I/O
File I/O (Input/Output) allows you to read from and write to files in Python. You can open files, read their contents, write data to them, and close them when done. This is useful for persisting data or reading configuration files.
Debugging is the process of finding and fixing errors in your code. Python provides various tools and techniques for debugging, such as using print statements, logging, and using debuggers like pdb.
# Debugging with Print Statementsdef add(a, b):print(f"Adding {a} and {b}")return a + bresult = add(5, 10)print(f"Result: {result}")# Debugging with Loggingimport logging logging.basicConfig(level=logging.DEBUG)def add(a, b): logging.debug(f"Adding {a} and {b}")return a + bresult = add(5, 10)logging.info(f"Result: {result}")
DEBUG:root:Adding 5 and 10
Adding 5 and 10
Result: 15
INFO:root:Result: 15
Performance
Performance optimization is crucial for writing efficient Python code. You can use profiling tools to identify bottlenecks in your code and optimize them. Techniques like using built-in functions, avoiding unnecessary computations, and using efficient data structures can help improve performance.
# Performance Optimization Exampledef fibonacci(n): a, b =0, 1for _ inrange(n): a, b = b, a + breturn a# Using the Functionn =10print(f"Fibonacci of {n} is {fibonacci(n)}")# Profiling Exampleimport cProfiledef profile_fibonacci(n): cProfile.run(f"fibonacci({n})")print(f"Fibonacci of {n} is {fibonacci(n)}")profile_fibonacci(10)
Fibonacci of 10 is 55
4 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 3126826253.py:2(fibonacci)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Fibonacci of 10 is 55