Scope of Variable(local and global),Mutable vs Immutable Objects in Python,Passing a List as an Argument,Use of global keyword
Run Your Python Programs Online
Scope of Variable:
Part of program where a variable is visible or accessible
Local :
Variable declared in side the function called local variable
With local scope, variable can be used in the function where it is declared
def fun():
a=10
print(a) # a is local variable
fun()
Global:
Variable declared outside of all function called global variable
With global scope , variable can be used or accessed any where in the program
b=20 # global variable
def fun():
a=10 # local variable
print(a)
print(b)
fun()
output
10
20
global keyword
If we want to change the value of global variable inside the function we have to use global keyword
It is used to create global variables from a non-global scope i.e inside a function.
It is used When we want to do assign value to global variable inside function.
We can access or print global variable any where in program without using global keyword
b=20
def fun():
a=10
print(a) # a is local variable
print(b) # b is a global variable can be access any where in the program
fun()
print(b)
ouput:
10
20
20
When we change the value of global variable inside local scope. global keyword is used
b=20
def fun():
a=10
print(a)
global b
b=b+5
fun()
print(b)
output
10
25
Mutable vs Immutable Objects in Python
Python represents all its data as objects . every object can be either mutable or immutable based on the type of data they hold.
Every variable in python holds an instance of an object.
There are two types of objects in python i.e. Mutable and Immutable objects.
Generally Primitive-like types are probably immutable and Customized Container-like types are mostly mutable.
Mutable object are changeable while Immutable are not changeable
Mutable objects
Can change their state or contents.
These are of type like list, dict, set . Custom classes
Use of mutable objects is recommended when there is a need to change the size or content of the object.
# Python code to test that lists are mutable
color = ["udaigiri", "arawali", "shivalik"]
print(color)
color[0] = "Neelgiri"
color[-1] = "Holinding-2"
print(color)
Output without error
["Neelgiri", "arawali", "Holinding-2"]
Immutable Objects :
These are of in-built types like int, float, bool, string, unicode, tuple.
An immutable object can’t be changed after it is created.
Whenever an object is instantiated, it is assigned a unique object id.
Defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.
Quicker to access and are expensive to change because it involves the creation of a copy.
Whereas mutable objects are easy to change.
e.g
# Python code to test that tuples are immutable
t = (1, 2, 3,4)
t[0] = 4
print(t)
Give TypeError: 'tuple' object does not support item assignment
e.g Python code to test that strings are immutable
message = "Welcome to Meraprayas"
message[0] = 'p'
print(message)
TypeError: 'str' object does not support item assignment
Important Fact
Consider a tuple
tuple = ([1, 2, 3], 'meraprayas')
The tuple consists of a string and a list.
Strings are immutable so we can’t change its value.
But the contents of the list can change.
The tuple itself isn’t mutable but contain items that are mutable.
Passing a List as an Argument
def houses(house):
for x in house:
print(x)
house = ["udaigiri", "arawali", "shivalik","Neelgiri","H-1","H-2"]
houses(house)
output:
udaigiri
arawali
shivalik
Neelgiri
H-1
H-2
Passing a Dictionary as an Argument
A dictionary in Python is a collection of data which is unordered and mutable.
# A function that takes dictionary as an argument
def func(d):
for key in d:
print("key:", key, "Value:", d[key])
# Driver's code
D = {'one':1, 'two':2, 'three':3,"four":4}
func(D)
Output
key: one Value: 1
key: two Value: 2
key: three Value: 3
key: four Value: 4
Conclusion
Local variable : Variable declared inside function
Global variable: Variable declared outside of all functions
global keyword: Used to change the value of global variable in local scope
Mutable object: can be changed after creation. E.g list,dictionary, set, custom classes
Immutable objects: can not change after creation e.g tuple, int, bool, string etc
Comments
Post a Comment