All values in Python are references. What you need to worry about is if a type is mutable. The basic numeric and string types, as well as tuple and frozenset are immutable; names that are bound to an object of one of those types can only be rebound, not mutated.

>>> t = 1, 2, 3
>>> t[1] = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Answer from Ignacio Vazquez-Abrams on Stack Overflow
Top answer
1 of 3
50

All values in Python are references. What you need to worry about is if a type is mutable. The basic numeric and string types, as well as tuple and frozenset are immutable; names that are bound to an object of one of those types can only be rebound, not mutated.

>>> t = 1, 2, 3
>>> t[1] = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
2 of 3
24

Coming from iOS development using strongly typed Swift language, Python reference was a bit confusing so I decided to do a little comparison. Here is the summary: -

  • When assigning a variable to python say a = 10 you are simply pointing/referencing the the object in this case 10 which is stored in a memory. So if that object changes then the value of a variable a also changes but changing a does not change the object 10, This behave similar to Swift Primitive value types such as Int.

To make this clear here is an example: -


 # "a" points to an object in this case 10
a = 10

# "b" points to the same object which a points but does not point to a variable a.
b = a 

# Now if we change "a" to point to another object in memory say 20. 
a = 20

# "b" still points to the old object 10 in other words
# "b == 10" but "a == 20", This is because "b" was never pointing to the variable "a" 
# even though we assigned it as "b = a" instead it was pointing to the object 10
#  which is # the same as writing b = 10. 

Let's check with a more complex data structure List

list1 = [10,20,30,40]
list2 = list1 #[10,20,30,40]

list1 = [3,4] 

# list1 ==> [3,4]
# list2 ==> [10,20,30,40]


Again that behave the same to Swift and other similar languages. Here comes the huge difference Let's try changing value at a certain index ( This gets more tricky)

list1 = [10,20,30,40]
list2 = list1 #[10,20,30,40]

# change value of list 1 at a certain index say index 0
list1[0] = 500

# If you check again the values of list1 and list2 you will be surprised. 
#list1 ==> [500,20,30,40]
#list2 ==> [500,20,30,40]

They both change because they were all pointing to the same object so changing the object changes all list1 and list2. This is very confusing from other Languages such as Swift. In Swift List/Array are value types meaning they are not referenced instead they are copied around, However in python it is another story, changing a value at a certain index results in changing that value for all properties which references that object just like in the example above. This is very important to keep in mind for folks coming from Swift or other similar languages.

So how do we copy in python?

  • If you want to copy the list in python then you have to explicitly do so as shown on the example below: -
list1 = [10,20,30,40]
list2 = list(list1)

# list1 ==> [10,20,30,40]
# list2 ==> [10,20,30,40]

Doing so will avoid undesired effects when list1 changes list2 will remain the same.

As an example

list1[0] = 500
#list1 ==> [500,20,30,40] # Changed
#list2 ==> [10,20,30,40] # Unchanged
🌐
Luis Llamas
luisllamas.es › home › courses › python programming course
Value and Reference Types in Python
November 20, 2024 - Common reference types include lists, dictionaries, sets, and custom objects.
🌐
Better Programming
betterprogramming.pub › beginning-python-programming-part-2-reference-types-pointers-and-collection-types-a806ef6b92cf
Beginning Python Programming — Part 2 | by Bob Roebling | Better Programming
July 31, 2019 - In other programming languages, you have this idea of value types. In Python, this concept doesn’t exist. Instead, we just have reference types.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python references
Python References | Tutorials on How References Works in Python
March 31, 2023 - More specifically to mention the attributes and items are very closely related. the attributes differ from the items by means through which they are referenced. from the object perspective, it will be denoted with an object name and followed by a period (.) and then continues with an attribute name. whereas from the item perspective, the item element holds an item name followed by a square brace [], the braces hold the key which is referenced. virtually mentioning every element in python is an object of a specific class or type.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python
docs.python.org › 2.0 › ref › objects.html
3.1 Objects, values and types
The type() function returns an object's type (which is an object itself). The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter's value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed.
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.8 documentation
Values of n less than 0 are treated as 0 (which yields an empty sequence of the same type as s). Note that items in the sequence s are not copied; they are referenced multiple times. This often haunts new Python programmers; consider:
🌐
Medium
medium.com › geekculture › python-reference-e6458a9a0582
Python — Reference
August 9, 2022 - Variables in Python have a special attribute: identity, that is, “identity identification”. This special property is also called “reference” in many places.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › clarification as to how variables reference to items in memory?
r/learnpython on Reddit: Clarification as to how variables reference to items in memory?
May 20, 2021 -

So, I am still a little new to python, and I came across something that confused me.

When I write something like the following:

x = 10
y = x
y += 1
print(x)

it outputs 10, because I set y to x, and added 1 to it, however I never touched x, so x should stay the same and it does. But when it comes to something like lists, and I have something like:

x = ['a', 'b', 'c']
y = x
y.append('d')
print(x)

The code outputs ['a', 'b', 'c', 'd']. This is a similar structure to the original code I wrote except with lists, and in this case it affects the original.

I am aware this can be worked around by just doing something like y = x[:],

but why does this happen in the first place, and why is it not the same case for everything as in the first example I showed?

Top answer
1 of 1
4

These concepts don't really apply in Python. In my experience "reference type" and "value type" are mostly used in .NET languages such as C#, where they have specific meanings - variables of a reference type store a reference to an object, while variables of a value type store the value of the object directly. You can't obtain a reference to that object stored in the variable (although you may be able to create a "boxed" copy of that value and obtain references to the box). The terms may be used by analogy in other languages - but for example in Java all classes would be what .NET considers "reference types" and only primitives behave as "value types" do in .NET, so there the terms aren't really as necessary. So let's return to Python.

If we use the .NET meaning, then yes, all Python types are effectively "reference" types, in that every variable is a reference* to an object and doing a = b will make a refer to the same object as b. However, the thing about "reference types" and "value types" is that when an object is immutable it no longer really matters whether it's of a reference type or a value type. You can't do the one big thing where the distinction matters - you can't have two references to the same object, modify the object through one of them and then be surprised when the other changes!

This is a long-winded way to say that when people call integers "value types" in Python they are probably thinking about them in terms of C# or maybe Java, and observing that they behave much the same as value types do in C# or primitives do in Java, because they do! But that's really because they're immutable more than anything else.


* Here I'm using "reference" in a more general sense than the specific one found in C#. The general sense is of some kind of handle, address or identifier that specifies a particular object, such that we can have many of these "references" to a single object. Both "reference" and "pointer" are general terms that may have more specific meanings in certain languages. For example a C# reference is a very similar concept to a Java reference, but rather different from a C++ reference. In some languages a "pointer" is specifically a numeric memory address that you can manipulate with arithmetic, in other languages it is indistinguishable from the general sense I described for "reference". I find the term "reference" to be the most neutral and most useful to describe what we have in Python, but be careful with all these terms when comparing one language to another.

🌐
Ancisoft
ancisoft.com › blog › what-does-python-treat-as-reference-types
What Does Python Treat as Reference Types? A Guide to Reference vs Value Types — ancisoft.com
In programming, how a language handles variables and their data can drastically impact behavior—especially when passing variables to functions, copying data, or modifying values. Python, unlike some other languages (e.g., C++ or Java), uses a unique "object reference" model that blurs traditional lines between reference and value types.
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596001886 › ch04s03.html
Variables and Other References - Python in a Nutshell [Book]
March 3, 2003 - Type ObjectsThe Code Object TypeThe frame Type · The gc ModuleThe weakref Module · The site and sitecustomize ModulesUser Customization · Threads in Python · Reference SectionThread ObjectsThread Synchronization ObjectsTimeout parametersLock and RLock objectsCondition objectsEvent objectsSemaphore objects ·
Author: Alex Martelli
Published: 2003
Pages: 656
🌐
Real Python
realpython.com › ref › builtin-types
Python’s Built-in Data Types (Reference) – Real Python
Python Steering Council · queue · raw string · recursion · reference count · relative import · release candidate · REPL · requirements.txt · scope · self (argument) sequence · shallow copy · shebang (#!) slice · slicing · snake case · soft keyword · source code · special method · stack · standard library · statement · static method · static type checker ·
🌐
InformIT
informit.com › articles › article.aspx
Articles | InformIT
if(isEnableGA){ if(currentEnvironment == 'QA'){ · }else{ · Articles · 890 Items · Sort by Date | Title · Page 1 2 3 4 5 Next > · Topics · Business & Management · Certification · Engineering
🌐
Medium
medium.com › @dreamferus › you-need-to-understand-variables-and-references-in-python-a-guide-51f0d480c083
You need to understand variables and references in Python — A guide
October 17, 2022 - A reference can be seen as the connection between a variable/name and a value, it contains the memory address where the value is held.
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
The arguments must be types and there must be at least one. ... However, this does not apply to unions referenced through a type alias, to avoid forcing evaluation of the underlying TypeAliasType:
🌐
Wikipedia
en.wikipedia.org › wiki › Value_type_and_reference_type
Value type and reference type - Wikipedia
March 7, 2026 - In certain computer programming languages, data types are classified as either value types or reference types, where values referenced by reference type variables are always implicitly accessed via references stored in those variables, whereas value type variables directly contain the values ...
🌐
Python documentation
docs.python.org › 3 › reference
The Python Language Reference — Python 3.14.6 documentation
March 16, 2023 - This reference manual describes the syntax and “core semantics” of the language. It is terse, but attempts to be exact and complete. The semantics of non-essential built-in object types and of the built-in functions and modules are described in The Python Standard Library.
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python references
Python References
March 27, 2025 - In Python, a variable is not a label of a value as you may think. Instead, A variable references an object that holds a value.