Fundamentals 10 min read

Understanding Dynamic vs. Static Typing and Variable Assignment in Python

This article explains the difference between dynamic and static typed languages, illustrates how Python's simple assignment works at the memory level, describes shared references, and shows how to use the built‑in type() function to inspect object types.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Dynamic vs. Static Typing and Variable Assignment in Python

1. Dynamic Typed Languages vs. Static Typed Languages

In static languages such as C, a variable’s type must be declared at definition time and cannot change during its lifetime. Python, however, is a dynamic typed language: a variable’s type can change at any moment, allowing any data type to be assigned to it.

Dynamic typing means the type is determined at runtime; no prior declaration is required. Typical dynamic languages include Python, JavaScript, PHP, Ruby, and Perl. In contrast, static typing checks types at compile time; examples are C/C++, C#, and Java.

Advantages

Disadvantages

Static Typed Language

Highly structured, easy to debug, type‑safe

Requires more type‑related code, making code harder to read

Dynamic Typed Language

More readable, less boilerplate

Harder to debug, can lead to unclear code when naming is inconsistent

To help understand why Python variables appear to change type, we next examine the principle of a simple assignment statement.

2. Principle of Simple Assignment Statements

In Python the equal sign performs assignment: the expression on the right‑hand side is evaluated and the resulting object is bound to the name on the left‑hand side.

<code>>> temp = 123  # assign integer 123 to variable temp
>>> temp = '123'  # assign string '123' to variable temp
>>> temp = 100 + 23  # assign the result of 100 + 23 to temp</code>

When temp = 123 is executed, Python creates an integer object 123 in memory, creates a name temp (if it does not already exist), and stores the memory address of the integer object in temp . The variable therefore holds a reference to the object, not the object itself.

Because the variable stores only the address, assigning a different type simply changes the stored address:

Allocate memory for the data object 123 .

If temp does not exist, create the name temp .

Bind temp to the address of the object 123 .

Thus the same variable can later be bound to a string, a list, or any other object, giving the impression that its type has changed.

3. Shared References

Assigning one variable to another copies the reference, not the object:

<code>>> tempA = 123  # assign integer 123 to tempA
>>> tempB = tempA  # copy the reference of tempA to tempB</code>

Both tempA and tempB now point to the same integer object 123 . This situation is called a shared reference. If one of the variables is reassigned, the shared link is broken for that variable only, while the other variable continues to reference the original object.

For example, reassigning tempB to 456 creates a new link between tempB and the integer 456 , but tempA still references 123 .

4. Inspecting Object Types with type()

The built‑in type() function returns the class of any object, allowing you to determine the actual type of a variable or literal.

<code>>> type("123")
<class 'str'>
>>> type(123)
<class 'int'>
>>> result = input("Enter a number: ")
Enter a number: 123
>>> type(result)
<class 'str'>
</code>

Common Python data types and their corresponding classes are listed below:

Integer

Float

Complex

Boolean

String

List

Tuple

Set

Dictionary

NoneType

To check whether a variable is an instance of a particular type, Python also provides the isinstance() function.

— Liu Wenfei

PythonfundamentalstypeVariable AssignmentDynamic TypingReference
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.