Master Python Variables and Constants: Key Differences and Practical Examples
This tutorial explains the fundamental differences between variables and constants in Python, demonstrates how variables are created, assigned, and typed, shows multiple assignment techniques, and clarifies why constants are merely a naming convention, all with clear code examples and illustrations.
1. Difference Between Variables and Constants
In programming, a variable holds a value that can change during execution, while a constant’s value remains unchanged.
Both allocate memory space when created.
2. Variables in Python
1. No Type Declaration Needed
Python’s dynamic nature allows variables to be created without specifying a type. For example:
a = 4
b = "haha"
c = []
d = 9-52. Assignment with “=”
The “=” operator assigns a value to a variable, not a mathematical equality.
a = 1003. Variables Must Be Assigned Before Use
Using a variable before assignment raises a NameError.
# a # NameError
a = 1 # OK
c.append(1) # NameError because c is undefined4. Variables Have No Intrinsic Data Type
The type belongs to the object the variable references.
a = 1
a = "haha"
a = [1, 2, 3]
a = {"k1":"v1"}5. Assignment Evaluates Right‑to‑Left
The right‑hand side is evaluated first, then the result is stored in the left‑hand variable.
a = 1
b = 2
c = a + b # c becomes 3
print(c)6. Multiple Assignment
Python can assign the same value to several variables or unpack tuples.
a = b = c = 1
a, b, c = 1, 2, 3Remember that “=” is assignment, not mathematical equality.
x = 1
x = x + 2 # x becomes 3When executing a = 'ABC', Python creates a string object and a variable that references it. Assigning one variable to another makes the second reference the same object, but reassigning the first does not affect the second.
a = 'Jack'
b = a
a = 'Tom'
print(b) # Jack
print(a) # TomAll objects are references; variables are just names pointing to objects.
3. Constants
Constants are conventionally written in uppercase, e.g., PI = 3.14159265359, but Python does not enforce immutability.
They are usually defined at the top of a module and used globally.
4. Summary
This article introduced the basic differences between variables and constants in Python, demonstrated variable usage with numerous examples, and highlighted that everything in Python is an object reference.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
