Professional Documents
Culture Documents
In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global
variable can be accessed inside or outside of the function.
x = "global"
def foo():
print("x inside:", x)
foo()
print("x outside:", x)
Output
x inside: global
x outside: global
In the above code, we created x as a global variable and defined a foo() to print the global variable x. Finally, we call the foo() which
will print the value of x.
What if you want to change the value of x inside a function?
x = "global"
def foo():
x = x * 2
print(x)
foo()
Output
The output shows an error because Python treats x as a local variable and x is also not defined inside foo().
To make this work, we use the global keyword.
Local Variables
A variable declared inside the function's body or in the local scope is known as a local variable.
def foo():
y = "local"
foo()
print(y)
Output
The output shows an error because we are trying to access a local variable y in a global scope whereas the local variable only
works inside foo() or local scope.
Let's see an example on how a local variable is created in Python.
def foo():
y = "local"
print(y)
foo()
Output
local
Let's take a look at the earlier problem where x was a global variable and we wanted to modify x inside foo().
x = "global "
def foo():
global x
y = "local"
x = x * 2
print(x)
print(y)
foo()
Output
global global
local
In the above code, we declare x as a global and y as a local variable in the foo(). Then, we use multiplication operator * to modify
the global variable x and we print both x and y.
After calling the foo(), the value of x becomes global global because we used the x * 2 to print two times global. After that, we print
the value of local variable y i.e local.
x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
Output
local x: 10
global x: 5
In the above code, we used the same name x for both global variable and local variable. We get a different result when we print the
same variable because the variable is declared in both scopes, i.e. the local scope inside foo() and global scope outside foo().
When we print the variable inside foo() it outputs local x: 10. This is called the local scope of the variable.
Similarly, when we print the variable outside the foo(), it outputs global x: 5. This is called the global scope of the variable.
Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable can be neither in the
local nor the global scope.
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
Output
inner: nonlocal
outer: nonlocal
In the above code, there is a nested inner() function. We use nonlocal keywords to create a nonlocal variable. The inner() function is
defined in the scope of another function outer().
Note : If we change the value of a nonlocal variable, the changes appear in the local variable.
All the data in a Python code is represented by objects or by relations between objects. Every object has an identity, a
type, and a value.
Identity
An object’s identity never changes once it has been created; you may think of it as the object’s address
in memory. The is operator compares the identity of two objects; the id() function returns an integer
representing its identity.
Type
An object’s type defines the possible values and operations (e.g. “does it have a length?”) that type supports.
The type() function returns the type of an object. An object type is unchangeable like the identity.
Value
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.
Important note
Some objects contain references to other objects, these objects are called containers. Some examples of containers
are a tuple, list, and dictionary. The value of an immutable container that contains a reference
to a mutable object can be changed if that mutable object is changed. However, the container is still
considered immutable because when we talk about the mutability of a container only the identities of the
contained objects are implied.
In the next section, we’ll see more information and detailed examples to understand more about the differences between
the mutable and immutable objects.
On the other hand, some of the immutable data types are int, float, decimal, bool, string, tuple, and
range.
It’s time for some examples. Let’s start by comparing the tuple (immutable) and list (mutable) data types. We
can define a list using square
brackets [] like this: numbers = [1, 2, 3]. To define a tuple, we just need to replace
the brackets with parentheses () like this: numbers = (1, 2, 3). From both data types, we can access elements by
index and we can iterate over them. The main difference is that a tuple cannot be changed once it’s defined.
Output:
1
10
Output:
[100, 2, 3]--------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-286c46a29f5d> in <module>()
3 list_values[0] = 100
4 print(list_values)
----> 5 set_values[0] = 100
Now, we can try to expand our list and tuple using the += operator. This will operation work for both data types. Let’s see
what will happen.
Output:
2450343168136
2450343205552
2450343168136
2450341742248
We can see that the list identity is not changed, while the tuple identity is changed. This means that we
have expanded our list, but created a completely new tuple. Lists are more memory efficient than tuples.
We have seen that some of the other immutable data types are integers and strings. Once they are initialized, their
values cannot be changed.
Output:
1657696608
1657696640
Output:
2450343168944
2450343426208
We see that both for the number and text variables, their identity is changed. This means that new
variables are created in both cases.
Let’s see what happens if we give two names of the same object for a mutable data types.
Output:
2450343166664
2450343166664
True
[4, 5, 6, 7]
[4, 5, 6, 7]
We can see that the variable names have the same identity meaning that they are referencing to the same
object in computer memory. Reminder: the is operator compares the identity of two objects.
So, when we have changed the values of the second variable, the values of the first one are also changed. This happens
only with the mutable objects. You can see how you can prevent this in one of my previous blog posts.
Copying Immutable Objects
Let’s try to do a similar example with an immutable object. We can try to copy two strings and change the value in any of
them.
Output:
3063511450488
3063511450488
True
3063551623648
3063511450488
False
Python is awesome
Python
Every time when we try to update the value of an immutable object, a new object is
created instead. That’s when we have updated the first string it doesn’t change the value of the second.
The == operator
Sometimes we don’t want to compare the identity of two objects, but to compare the values of these objects. We can do
this using the == operator.
Output:
True
False
We can clearly see the two objects have the same values, but their identities are different.
Output:
<class 'tuple'>
(129392130, ['Programming', 'Machine Learning', 'Statistics'])
(129392130, ['Programming', 'Machine Learning', 'Maths'])
We have changed the value of the skills variable. The other variable person contains a reference to the skills variable
and that’s why its value is updated, too.
Reminder
The object is still considered immutable because when we talk about the mutability of a container only
the identities of the contained objects are implied.
However, if your immutable object contains only immutable objects, we cannot change their value. Let’s see an example.
Output:
1657696608
1657696032
(42, 24, ('Python', 'pandas', 'scikit-learn'))
1657696864
1657696064
(42, 24, ('Python', 'pandas', 'scikit-learn'))
Remember when you try to update the value of an immutable object, a new object is created instead.
Summary
All the data in a Python code is represented by objects or by relations between objects.
Every object has an identity, a type, and a value.
An object’s identity never changes once it has been created. You may think of it as the object’s
address in memory.
An object’s type defines the possible values and operations.
Objects whose value can change are said to be mutable. Objects whose value is unchangeable once
they are created are called immutable.
When we talk about the mutability of a container only the identities of the contained objects are implied.