filmov
tv
#33 Python Tutorial for Beginners | Function Arguments in Python

Показать описание
Check out our courses:
Coupon: TELUSKO10 (10% Discount)
For More Queries WhatsApp or Call on : +919008963671
In this lecture we are discussing :
# all variables are actually references to objects in memory. Thus, when a function is called with a parameter, the reference to the object is passed by value.
# For immutable data types like integers, floats, and strings, changes made to the parameter inside the function do not affect the original value of the parameter outside the function. This is because any changes made to the parameter create a new object in memory, and the original reference to the object remains unchanged.
# For mutable data types like lists and dictionaries, changes made to the parameter inside the function do affect the original value of the parameter outside the function. This is because the reference to the object remains the same, but the contents of the object can be modified.
def update(lst):
print(id(lst))
lst[1]=25
print(id(lst))
print("x",lst)
lst = [10,20,30]
print(id(lst))
update(lst)
print(lst)
# The above code defines a Python function called update that takes a list lst as a parameter. The function first prints the memory address of the list using the id() function. It then modifies the second element of the list by assigning it the value 25. The function then prints the new memory address of the list and the updated list.
# The code then creates a list called lst with the values [10, 20, 30] and prints its memory address using the id() function. The update function is then called with the lst parameter. Since lists are mutable objects in Python, the memory address of the list is passed to the function, making it a pass by reference.
# Inside the update function, the second element of the list is modified to 25. Since lists are mutable objects and the memory address of the original list is passed to the function, this change affects the original list outside the function. The new memory address and the updated list are printed inside the function.
Python Tutorial to learn Python programming with examples
Editing Monitors :
Subscribe to our other channel:
Telusko Hindi :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Coupon: TELUSKO10 (10% Discount)
For More Queries WhatsApp or Call on : +919008963671
In this lecture we are discussing :
# all variables are actually references to objects in memory. Thus, when a function is called with a parameter, the reference to the object is passed by value.
# For immutable data types like integers, floats, and strings, changes made to the parameter inside the function do not affect the original value of the parameter outside the function. This is because any changes made to the parameter create a new object in memory, and the original reference to the object remains unchanged.
# For mutable data types like lists and dictionaries, changes made to the parameter inside the function do affect the original value of the parameter outside the function. This is because the reference to the object remains the same, but the contents of the object can be modified.
def update(lst):
print(id(lst))
lst[1]=25
print(id(lst))
print("x",lst)
lst = [10,20,30]
print(id(lst))
update(lst)
print(lst)
# The above code defines a Python function called update that takes a list lst as a parameter. The function first prints the memory address of the list using the id() function. It then modifies the second element of the list by assigning it the value 25. The function then prints the new memory address of the list and the updated list.
# The code then creates a list called lst with the values [10, 20, 30] and prints its memory address using the id() function. The update function is then called with the lst parameter. Since lists are mutable objects in Python, the memory address of the list is passed to the function, making it a pass by reference.
# Inside the update function, the second element of the list is modified to 25. Since lists are mutable objects and the memory address of the original list is passed to the function, this change affects the original list outside the function. The new memory address and the updated list are printed inside the function.
Python Tutorial to learn Python programming with examples
Editing Monitors :
Subscribe to our other channel:
Telusko Hindi :
Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20
Комментарии