Understanding Python Variables: Names, Assignment, and Values
This article introduces Python variables, describing their three components—name, assignment operator, and value—while covering naming rules, common naming conventions such as snake_case, camelCase, and uppercase constants, and explaining the id, type, and value properties with examples.
In Python, a variable stores a piece of data and consists of three parts: the variable name, the assignment operator, and the variable value.
"Variables are composed of three parts"
The typical structure can be illustrated as name = "Mike" , where name is the variable name, = is the assignment operator, and "Mike" is the variable value.
What is a variable name?
A variable name acts as the address of the memory space allocated for the variable value, allowing the program to locate and retrieve the stored data.
Variable naming rules
Do not use pinyin or Chinese characters.
Avoid Python reserved keywords.
The name cannot start with a digit.
Variable naming conventions
There are three common styles:
Lowercase with numbers and underscores (snake_case) – the most widely used.
CamelCase – each word starts with a capital letter.
All uppercase – typically used for constants.
Assignment operator
The equal sign ( = ) binds the right‑hand value to the left‑hand variable name, enabling the name to reference the value.
Variable value
The value represents the actual state of the data. When a value’s reference count drops to zero, Python’s garbage collector reclaims the memory.
Important variable properties
Each variable has three key attributes:
id : the unique memory address of the value, viewable with id() .
type : the data type of the value, viewable with type() .
value : the actual data stored.
Example
An example problem: calculate a student's average score from three subjects, display the average with one decimal place, and show the percentage of the Chinese score relative to the total.
END
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.