Python Language Structure
Introduction
This guide covers the fundamental structure and features of Python, including literals, operations, and exception handling.
Literals
Literals are fixed values in Python code.
Numeric Literals
| # Integer literals
42 # Decimal
0b101010 # Binary
0o52 # Octal
0x2a # Hexadecimal
# Numeric separators
123_456_789 # Decimal with separators
0x1234_5678 # Hexadecimal with separators
0b111_00_101 # Binary with separators
123.789_012 # Float with separators
|
String Literals
| # String literals
'Hello' # Single quotes
"World" # Double quotes
'''Multi-line
string''' # Triple quotes
|
Operations
Iterable Operations
| # Basic iteration
for item in sequence:
print(item)
# Variable unpacking
a, b, c = [1, 2, 3]
a, *rest = [1, 2, 3, 4] # a=1, rest=[2,3,4]
a, _, c = [1, 2, 3] # a=1, c=3
# Membership testing
x in sequence
x not in sequence
# Expansion
[a, *sequence, b] # List
(a, *sequence, b) # Tuple
{a, *sequence, b} # Set
|
Set Operations
| # Set creation and operations
s1 = {'a', 'b', 'c'}
s2 = {'b', 'c', 'd'}
# Union
s1 | s2 # {'a', 'b', 'c', 'd'}
s1.union(s2)
# Intersection
s1 & s2 # {'b', 'c'}
s1.intersection(s2)
# Difference
s1 - s2 # {'a'}
s1.difference(s2)
# Symmetric difference
s1 ^ s2 # {'a', 'd'}
s1.symmetric_difference(s2)
# Set methods
s1.add('d')
s1.remove('a') # Raises KeyError if not found
s1.discard('a') # No error if not found
s1.pop() # Removes and returns arbitrary element
s1.clear() # Removes all elements
|
Dictionary Operations
| # Dictionary operations
d = {'a': 1, 'b': 2}
# Access
value = d['a']
value = d.get('a', 0) # With default value
# Modification
d['c'] = 3
d.update({'d': 4})
del d['a']
# Keys and values
keys = d.keys()
values = d.values()
items = d.items()
# Dictionary comprehension
d = {k: v for k, v in zip(keys, values)}
|
List Operations
| # List comprehension
[expression for item in iterable if condition]
# Nested comprehension
[expression for item1 in iterable1 if condition1
for item2 in iterable2 if condition2]
# Generator expression
(x*x for x in range(10))
# List methods
lst.append(x)
lst.extend(iterable)
lst.insert(i, x)
lst.remove(x)
lst.pop(i)
lst.clear()
lst.index(x)
lst.count(x)
lst.sort()
lst.reverse()
|
Enumerate
| # Basic enumeration
for i, item in enumerate(sequence):
print(i, item)
# Custom start
for i, item in enumerate(sequence, start=1):
print(i, item)
|
Zip
| # Basic zip
for x, y in zip(sequence1, sequence2):
print(x, y)
# Unzipping
pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
numbers, letters = zip(*pairs)
|
Exception Handling
Exception Hierarchy
| BaseException
├── SystemExit
├── KeyboardInterrupt
├── GeneratorExit
└── Exception
├── StopIteration
├── ArithmeticError
├── AssertionError
├── AttributeError
├── BufferError
├── EOFError
├── ImportError
├── LookupError
├── MemoryError
├── NameError
├── OSError
├── ReferenceError
├── RuntimeError
├── SyntaxError
├── SystemError
├── TypeError
├── ValueError
└── Warning
|
Custom Exceptions
| class CustomError(Exception):
"""Base class for custom exceptions."""
pass
class ValidationError(CustomError):
"""Raised when validation fails."""
pass
# Usage
try:
raise ValidationError("Invalid input")
except ValidationError as e:
print(f"Error: {e}")
|
Exception Chaining
| try:
# Some code that may raise an exception
raise ValueError("Invalid value")
except ValueError as e:
# Chain the exception
raise RuntimeError("Failed to process") from e
|
Exception Attributes
| try:
raise ValueError("Invalid value")
except ValueError as e:
print(e.args) # Tuple of arguments
print(e.__cause__) # Original exception
print(e.__context__) # Exception context
|
Best Practices
- Use appropriate literals
- Choose the right data structure
- Use list comprehensions for simple transformations
- Use generator expressions for large datasets
- Handle exceptions appropriately
- Use custom exceptions for domain-specific errors
- Document exception handling
- Follow PEP 8 style guide
Common Pitfalls
- Modifying collections during iteration
- Using mutable default arguments
- Ignoring exceptions
- Catching too broad exceptions
- Not using context managers
- Forgetting to close resources
- Using global variables
-
Not handling edge cases
-
Data Types
- Functions
- Error Handling
- Style Guide