Understanding Variables in Python: Definition, Assignment, and Usage
This article explains Python variables, covering their definition as memory containers, naming conventions, assignment syntax, reassignment, using variables in expressions and functions, and illustrates Python's weak typing compared to strong‑typed languages with code examples.
Any programming language needs to handle data such as numbers, strings, and characters; these data can be used directly or stored in variables for later use.
A variable can be seen as a container that holds program data. Each variable has a unique name that references the memory location where the data is stored.
1. Defining a Variable (Assigning a Value)
In Python, the syntax for defining a variable is:
<code>variable_name = value</code>Guidelines for the variable name:
It must be an identifier and cannot be a keyword.
Avoid naming conflicts with Python built‑in functions.
Follow PEP8 naming conventions (all lowercase, words separated by underscores).
Choose meaningful names that convey the stored data.
The value can be any expression that yields a result, such as a literal, an existing variable, or an arithmetic expression.
Example:
<code>n = 10</code>More assignment examples:
<code># Assign 20 to variable age
age = 20
# Assign a string to variable name
name = '小明'
# Assign a boolean value to variable real
real = True</code>Variable values can be changed by reassigning:
<code>age = 18 # assign 18 to age
age = 30 # assign 30 to age
age = 50 # assign 50 to age
abc = 12.5 # assign a float to abc
abc = 100 # assign an integer to abc
abc = 'http://1000phone.com' # assign a string to abc</code>When a variable is reassigned, the previous value is overwritten and no longer accessible; a variable can hold only one value at a time.
Expressions can also be assigned to variables:
<code># Assign the result of a mathematical operation
sum1 = 10 + 2 + 3
result = 10 / 2 * 3 - 4
# Assign the result of string concatenation
str1 = 'hello' + 'Python!'</code>2. Using Variables
To use a Python variable, you only need its name. Variables can be used almost anywhere in the code:
<code>n = 10
print(n) # pass variable as function argument
m = n * 2 + 10
print(m) # use variable in arithmetic
print(m + n + 100) # pass expression containing variables</code>Output:
<code>10
30
140</code>3. Python Is a Weakly Typed Language
In strongly typed languages like C++, the variable type must be declared and the assigned value must match that type. Example in C++:
<code>// Define an integer variable n with initial value 10
int n = 10;
n = 100; // reassign integer value
n = "http://www.1000phone.com"; // error: cannot assign string to int
url = "http://www.1000phone.com"; // error: variable not declared</code>Python, JavaScript, PHP, and similar scripting languages are weakly typed. Their characteristics are:
Variables can be created simply by assigning a value; no explicit declaration is needed.
The variable’s data type can change at runtime, e.g., a variable can hold an integer one moment and a string the next.
Weak typing does not mean the language lacks types; types still exist internally. You can inspect a variable’s type using the built‑in type() function:
<code>x = 10
print(type(x)) # <class 'int'>
x = 12.5
print(type(x)) # <class 'float'>
</code>Result:
<code><class 'int'>
<class 'float'></code>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.