2 - Python-Variables-Beginner-Learning-Module
2 - Python-Variables-Beginner-Learning-Module
A comprehensive, beginner-friendly guide to understanding variables in Python — the foundational concept behind every Python
program ever written. This module builds directly on Structure of a Python Program and prepares you for Data Types, Operators,
and beyond.
Developed by Talenciaglobal
Python vs Java
Memory Readability
Store information for later use without recalculating it tax_rate is infinitely clearer than the bare number 0.18
every time. buried in an expression.
Reusability Computation
Change a value in one place and every calculation that uses Combine and transform stored values to derive new,
that variable updates automatically. meaningful results.
Link
Create 25 Register name
name→25
Understanding these three steps demystifies much of what confuses beginners early on. The value exists independently of the name
— the name is simply a convenient handle Python uses to find it.
The key insight: the type belongs to the value, not to the
x=5 # x is now an integer variable. The number 5 is always an integer. The string "hello"
print(x) #5
is always a string. The variable x is merely a label that can be
x = "hello" # x is now a string!
moved from one value to another.
print(x) # hello
x = 3.14 # x is now a float Dynamic Typing Trade-offs
print(x) # 3.14
✅ Pros ⚠️ Cons
✅ This works perfectly in Python. In Java, this would cause a Less code to write Harder to catch type bugs
compile error immediately.
early
Checking the Current Type — type()
Faster prototyping Errors appear only at
runtime
x=5
print(type(x)) # <class 'int'> More flexible code Type confusion in large
x = "hello" codebases
print(type(x)) # <class 'str'>
x = 3.14
print(type(x)) # <class 'float'> For beginners: enjoy the simplicity. As your programs
x = True grow larger, you will learn tools like type hints and
print(type(x)) # <class 'bool'> linters to manage types more carefully.
Naming Rules & Conventions
Choosing the right name for a variable is one of the most undervalued skills in programming. A 2021 study by the University of Bern
found that well-named variables reduced the average time a developer spent understanding unfamiliar code by up to 19%. Python's
naming system has two tiers: rules you must follow (or Python raises an error) and conventions you should follow to write
professional, readable code.
Meaningless — tell you nothing about the data. Self-documenting — instantly readable.
camelCase is out of place in Python. snake_case is the Python standard (PEP 8).
Multiple Assignment Patterns & Reassignment
Python offers several elegant assignment patterns that go well beyond the basic name = value form. These patterns are heavily
used in real-world Python code — particularly the variable swap, which appears frequently in sorting algorithms and data
manipulation scripts. Mastering these patterns early will make your code cleaner and more idiomatic from day one.
a=b=c=0
print(a, b, c) # 0 0 0
All three names point to the same value 0. Perfect for initializing counters or scores.
The number of names on the left must exactly match the number of values on the right.
In Java, a variable is like a physical box that contains a value. In Python, a variable is more like a sticky note (a name tag)
The box age literally holds the number 25 inside it. When you attached to a value. The value 25 exists independently in
copy a variable, you copy the contents of the box into a new memory; the name age is simply a label pointing to it. You can
box. move that label to a different value at any time.
a = 100
b=a # b now points to the same value as a
print(a) # 100
print(b) # 100
Memory aid: "Label, not Box" — variables are names attached to values, not containers that hold values. This single phrase
will save you from a surprising number of debugging headaches.
Constants, Deletion, & Inspecting Variables
Constants in Python
Most programming languages provide a const or final keyword to declare values that should never change. Python has no such
keyword. Instead, the Python community relies on a naming convention: variables written in UPPER_SNAKE_CASE are understood
by everyone to be constants — values that should not be modified after their initial assignment.
PI = 3.14159
MAX_USERS = 100
SITE_URL = "[Link]
Python will not prevent you from reassigning these — but by strong convention, you must not. This reflects Python's philosophy of
trusting developers rather than enforcing restrictions through the language itself.
PI = 3.14159
# PI = 4
Python Variables vs Java Variables
For developers transitioning from Java, understanding the precise differences between the two languages' variable systems
prevents a large class of early confusion. The table below provides a comprehensive side-by-side comparison across eight key
dimensions. Notably, according to a JetBrains 2023 ecosystem survey, over 31% of Python developers come from a Java
background — making this comparison one of the most practically important summaries in any Python beginner curriculum.
Memory model Stack/heap; primitives vs objects All values are objects; variables are
labels
Default values 0, false, null (fields only) None — must always initialize
#
Complete Beginner Examples
The following four complete code examples demonstrate variables in progressively more sophisticated contexts. Running each of
these programs in your Python environment and observing the output is the single most effective way to build genuine confidence
with variables. According to learning science research (Sweller, 2011), worked examples studied before practice reduce cognitive
load and accelerate skill acquisition for novice programmers.
BEGINNER BEGINNER+
Objective: Practice creating, naming, reassigning, and Objective: Master Python's three assignment patterns.
inspecting variables.
Tasks:
Tasks:
1. Part A: Swap a = "morning" and b = "evening" in a single
1. Create a file called variables_showcase.py line — no temp variable
2. Create variables for your full name (string), age (integer), 2. Part B: Assign apple = banana = cherry = 0 in one line
height in feet (float), and whether you like coffee (boolean) 3. Part C: Assign name = "Alice", age = 28, city = "Pune" in
— all in proper snake_case one line
3. Print each variable and its type using type()
Expected Output
4. Reassign age to next year using age = age + 1
total_price not tp. customer_age not c. Names are Avoid x, y, z for anything meaningful. Use descriptive
documentation. names.
The PEP 8 standard. Every Python project in the world Never name a variable list, dict, str, type, id, sum,
follows this. input, min, max.
Signals to every reader: "do not change this value." That is Java convention. Python uses snake_case.
userName → user_name.
Initialize before using
Don't change type mid-program
Always assign a value before reading a variable. Never
assume it exists. Reassigning a variable to a different type without good
reason creates confusing code.
Add spaces around =
age = 25 ✅ is far more readable than age=25 ❌. Don't use cryptic abbreviations
01
"Label, not Box"
A variable is a name that points to a value
Variables point to values, they don't contain
Not a box. Not a container. A label — detachable and redirectable. them.
02
03
"Name = Value"
Syntax is simply name = value
Name first. Equals middle. Value last.
No keyword. No type. No semicolon. Dead simple. Always.
04
"NUDE"
Use snake_case and UPPER_SNAKE_CASE
Named meaningfully, Unique, Descriptive,
For variables and constants respectively. PEP 8 is the law of the Python
Easy to read.
world.
int(), str(),
Avoid list, dict, str, type, id, sum, input as variable names.
Operators float(), bool()
&
Expression
s
Arithmetic,
comparison, Input /
logical, Output
augmented
Reading user
assignment
input with
input()
Conditions
Developed by Talenciaglobal. This module is part of the Python Language Fundamentals learning path. Continue to the
Data Types module to build on everything you have learned here about variables and dynamic typing.