Python Programming Notes
Python Programming Notes
Python Fundamentals
Basic Syntax
Operators
Control Structures
Conditional Statements
if condition:
# code block
elif another_condition:
# code block
else:
# code block
Loops
# For loop
for item in iterable:
# code block
# While loop
while condition:
# code block
Control Statements
Data Structures
Lists
Dictionaries
Key-value pairs
Methods: keys(), values(), items(), get(), update()
Dictionary comprehensions: {x: x**2 for x in range(5)}
Tuples
Sets
Functions
Function Definition
def function_name(parameters):
"""Docstring"""
# code block
return value
Parameters
Lambda Functions
def method(self):
# code block
Inheritance
class Child(Parent):
def __init__(self, parameters):
super().__init__(parameters)
# additional initialization
Special Methods
Common Libraries
Exception Handling
try:
# code that might raise exception
except ExceptionType:
# handle exception
else:
# runs if no exception occurred
finally:
# runs regardless of exception
File Operations
with open('filename.txt', 'r') as file:
content = file.read()