Understanding Python Variables, Assignment, and Weak Typing
This article explains Python variables and constants, how to assign values using the = operator, naming rules, reassignment, expression assignment, variable usage in expressions, and the characteristics of Python as a weakly typed language with type‑checking examples.
All programming languages need to handle data such as numbers, strings, and characters; data can be used directly or stored in variables for later use.
A variable is like a small box that holds data and is identified by a unique name, while a constant is a similar box whose value cannot be changed after it is set.
In Python, assigning a value to a variable uses the equal sign ( = ) with the syntax name = value , where name is the variable name and value is the data to store.
Variable names must follow Python identifier naming conventions and avoid conflicts with built‑in functions or reserved keywords.
Example of assigning an integer to a variable:
<code>n = 10</code>Variables can be reassigned to new values of any type:
<code>n = 95</code>
<code>n = 200</code>
<code>abc = 12.5</code>
<code>abc = 85</code>
<code>abc = "http://c.biancheng.net/"</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, for example:
<code>sum = 100 + 20</code>
<code>rem = 25 * 30 % 7</code>
<code>str = "C语言中文网" + "http://c.biancheng.net/"</code>Using variables in Python code is straightforward—once a variable is defined, its name can be used anywhere. Example interaction:
<code>>> n = 10</code>
<code>>> print(n) # prints 10</code>
<code>>> m = n * 10 + 5 # arithmetic with variables</code>
<code>>> print(m) # prints 105</code>
<code>>> print(m - 30) # prints 75</code>
<code>>> m = m * 2 # double the variable</code>
<code>>> print(m) # prints 210</code>
<code>>> url = "http://c.biancheng.net/cplus/"</code>
<code>>> str = "C++教程:" + url # string concatenation</code>
<code>>> print(str) # prints C++教程:http://c.biancheng.net/cplus/</code>Python is a weakly typed language, meaning variables do not need explicit type declarations and can change type dynamically, unlike strongly typed languages such as C, C++, or Java where the type must be declared and enforced.
Weak typing does not mean the language lacks types; the interpreter still tracks types internally. The built‑in type() function can be used to inspect a variable’s type:
<code>>> num = 10</code>
<code>>> type(num)</code>
<code><class 'int'></code>
<code>>> num = 15.8</code>
<code>>> type(num)</code>
<code><class 'float'></code>
<code>>> num = 20 + 15j</code>
<code>>> type(num)</code>
<code><class 'complex'></code>
<code>>> type(3*15.6)</code>
<code><class 'float'></code>Thus, Python allows flexible variable usage without explicit type declarations while still providing mechanisms to query the actual runtime type.
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.