Introduction to Python Programming
A Deep-Dive Reference Guide on Code Structure, Variables, Numeric Data Types, and
Operators
1. Understanding Python Blocks
Unlike traditional programming languages (such as C++, Java, or JavaScript) that use curly braces {} or
explicit keywords to group blocks of code, Python relies entirely on indentation. The amount of whitespaces
(spaces or tabs) at the beginning of a line defines its structural hierarchy and execution scope.
The Mechanics of Indentation
A Python block always begins immediately following a statement that ends with a colon ( : ). Every
subsequent line that shares the exact same level of indentation belongs to that same code block. When the
indentation decreases, the current block is closed, and the flow returns to the parent scope.
VISUAL LAYOUT: PYTHON STRUCTURAL BLOCKS
INDENTATION (4 SPACES)
def check_threshold(value):
INDENTATION (4 SPACES)
threshold = 100
if value > threshold:
INDENTATION (4 SPACES)
print("Value exceeds safety limit!")
status = "Danger"
else:
print("Value is safe.")
status = "Normal"
return status
Strict Rules & Best Practices
• Standard Sizing: The official Python Style Guide (PEP 8) mandates using exactly 4 spaces per
indentation level.
• The Tab Trap: Mixing tabs and spaces inside a single script will cause a fatal IndentationError:
unindent does not match any outer indentation level . Always configure your editor to convert
tabs to spaces.
Introduction to Python: Complete Handbook 1
2. Python Variables & Dynamic Typing
In Python, a variable is not a statically configured memory storage box of a specific type. Instead, think of a
variable as a pointer or a reference label attached to an object existing in the computer's memory space.
Dynamic Allocation & Assignment
Variables are dynamically typed, meaning you do not declare their data type beforehand. They are instantly
created the moment a value is assigned using the assignment operator ( = ).
# Dynamic reassignment without structural conflict
data_holder = 45 # Initially binds to an Integer
data_holder = "Flexible" # Rebinds to a String object flawlessly
MEMORY REFERENCE MODEL
VARIABLE LABEL MEMORY OBJECT (INT)
x → Points To → 100
Variable Naming Constraints
To write valid Python code, identifier names must adhere to the following strict conventions:
• Must commence with an alphabetical character ( a-z, A-Z ) or an underscore ( _ ).
• Cannot begin with a numeric digit (e.g., 1st_value is invalid).
• Can only contain alphanumeric characters and underscores ( A-z, 0-9, _ ).
• Are strictly case-sensitive: velocity , Velocity , and VELOCITY represent three completely separate
variables.
• Cannot overwrite Python reserved keywords (such as if , while , import , True ).
3. Python Data Types Hierarchy
Python categorizes data into highly optimized built-in types. Understanding these types prevents logical type-
mismatch bugs during execution.
Introduction to Python: Complete Handbook 2
Category Data Type Keyword Mutability Literal Example
Numeric Integer int Immutable 42 , -1050
Numeric Floating-Point float Immutable 3.14159 , 2.0 , 4.1e-3
Numeric Complex complex Immutable 3 + 5j
Sequence String (Text) str Immutable "Python Guide"
Sequence List list Mutable [1, 2, 'three']
Mapping Dictionary dict Mutable {"key": "value"}
Logical Boolean bool Immutable True , False
4. Declaring and Using Numeric Data Types
Python supports high-precision mathematics automatically using its primary numeric variants.
Integers ( int )
Integers represent whole positive or negative numbers with no decimals. In Python 3, integers have arbitrary
precision—meaning they can automatically scale up to occupy as much memory as available on your
machine without suffering from structural overflow errors.
large_factorial = 93284729384729384723948723948723 # Handled natively
Floating-Point Numbers ( float )
Floats represent real numbers containing a decimal point or utilizing scientific engineering notation (using the
e or E delimiter to denote powers of 10).
pi_constant = 3.1415926535
plancks_constant = 6.626e-34 # Evaluates to 6.626 * 10^-34
Type Casting Utilities
You can force-convert variables between numeric classifications using explicit class constructors:
raw_integer = int(5.99) # Cuts off decimals completely, resulting in 5
raw_float = float(12) # Converts to 12.0
Introduction to Python: Complete Handbook 3
5. Core Python Operators
Operators represent the functional logic engine of Python, allowing evaluation and transformation of numerical
data structures.
A. Arithmetic Operators
Symbol Operation Name Mathematical Syntax Example Resulting Value
+ Addition 15 + 4 19
- Subtraction 15 - 4 11
* Multiplication 15 * 4 60
/ True Division 15 / 4 3.75 (Always a float)
// Floor Division 15 // 4 3 (Rounds down to nearest integer)
% Modulus (Remainder) 15 % 4 3
** Exponentiation 2 ** 4 16 (24)
B. Compound Assignment Operators
Compound assignments optimize execution speeds and formatting by updating variable states concurrently
with an inline arithmetic operation.
balance = 1000
balance += 250 # Equivalent to: balance = balance + 250 -> 1250
balance /= 2 # Equivalent to: balance = balance / 2 -> 625.0
C. Comparison Operators
Comparison operations check inequalities and properties, returning a fundamental bool object ( True or
False ).
• == Equal to: 5 == 5 evaluates to True .
• != Not equal to: 5 != 3 evaluates to True .
• > Greater than: 10 > 20 evaluates to False .
• <= Less than or equal to: 4 <= 4 evaluates to True .
Introduction to Python: Complete Handbook 4