Python Fundamentals
Some important characteristics of Python:- Python is dynamically typed (no need to declare variable types).
- Python uses indentation instead of braces (
{}) to define blocks. - Everything in Python is an object.
- Variables store references to objects.
- Python follows the PEP 8 style guide.
Variables
Variables store references to objects.Multiple Assignment
Variable Swapping
Object Identity
Two variables may reference the same object.== to compare values and is to compare object identity.
Everything is an Object
In Python, every value is an object, including numbers, strings, lists, dictionaries, functions, and classes.Small Integer Caching
Python caches small integers (typically -5 to 256) to improve performance.Never useisto compare numbers or strings. Use==instead.
Floating-Point Precision
Floating-point numbers are stored in binary, so some decimal values cannot be represented exactly.round() or math.isclose() when comparing floating-point numbers.
Data Types
Numeric
intfloatcomplex
Boolean
bool
Text
str
Sequence
listtuplerange
Mapping
dict
Set
setfrozenset
Binary
bytesbytearray
Special
None
Type Conversion
Operators
Arithmetic Operators
Arithmetic Operators
Example:
Comparison Operators
Comparison Operators
Comparison operators compare two values and return eitherTrue or False.
Example:
Logical Operators
Logical operators are used to combine or negate conditions. They return eitherTrue or False based on the operands.
Example:
Truthy and Falsy Values
In Python, every object has a truth value. Falsy values include:FalseNone0,0.0""(empty string)[](){}set()
- Non-zero numbers
- Non-empty strings
- Non-empty lists, tuples, dictionaries, and sets
and and or return one of their operands instead of only True or False.
andreturns the first falsy value, otherwise the last value.orreturns the first truthy value, otherwise the last value.
Assignment Operators
Assignment operators are used to assign values to variables. Some operators also perform an operation before assigning the result back to the variable.
Example:
Identity Operators
Membership Operators
Bitwise Operators
Bitwise Operators
Bitwise operators perform operations on the binary representation of integers. They are commonly used in systems programming, networking, embedded programming, and optimization.
Example:
<<shifts bits to the left, effectively multiplying by2for each shift.>>shifts bits to the right, effectively dividing by2(for positive integers) for each shift.
Note: Bitwise operators work only with integer values and are less commonly used in everyday Python programming compared to arithmetic and logical operators.
Operator Precedence
Use parentheses to improve readability.Control Flow
if
if…else
if…elif…else
match…case (Python 3.10+)
Python supports Structural Pattern Matching usingmatch.
for Loop
while Loop
break
Terminates the loop immediately.continue
Skips the current iteration.pass
Acts as a placeholder when no code is required.for…else
Theelse block executes only if the loop completes without encountering a break.
while…else
Works the same way asfor...else.
Python Nuances
Some Python-specific behaviors every beginner should know:- Everything in Python is an object.
- Variables store references to objects, not values.
- Use
==for value comparison andisfor identity comparison. - Small integers (
-5to256) are cached. - Floating-point arithmetic is not always exact.
- Indentation defines code blocks.
- Lists are mutable; tuples are immutable.
- Functions are first-class objects.
andandorreturn values, not justTrueorFalse.- Truthy and falsy values simplify conditional expressions.
- Multiple assignment and unpacking are built into the language.
- Python emphasizes readability and simplicity (PEP 8).