Python - Variables
In Python, objects can be categorized as either mutable or immutable based on whether their values can be changed after creation. Mutable objects allow modifications, while immutable objects do not.
Mutable Objects: Objects whose state or value can be changed after creation are considered mutable. Examples include lists and dictionaries.
# Mutable object example: List
original_list = [1, 2, 3]
print("Original list:", original_list)
# Modifying the list
original_list[0] = 99
print("Modified list:", original_list)
Immutable Objects: Objects whose state cannot be changed after creation are considered immutable. Examples include strings and tuples.
# Immutable object example: String
original_string = "Hello"
print("Original string:", original_string)
# Attempting to modify the string would result in an error
# Uncommenting the next line would raise an error
# original_string[0] = 'C'
In the example, the list (mutable object) can be modified by changing its elements, while attempting to modify the string (immutable object) would result in an error. Understanding the mutability of objects is crucial for effective programming and preventing unintended side effects.
global
keyword in Python.
In Python, the global
keyword is used to indicate that a variable declared within a function should be treated as a global variable. It allows you to modify the value of a variable defined outside the current function scope.
Here's an example program demonstrating the use of the global
keyword:
# Using the global keyword
global_variable = 10
def modify_global_variable():
global global_variable # Indicating that 'global_variable' is a global variable
global_variable = 20
modify_global_variable()
print("Modified global variable:", global_variable)
In this example, the function modify_global_variable
uses the global
keyword to indicate that it will modify the global variable global_variable
. After calling the function, the value of the global variable is changed, and the updated value is printed.
locals()
and globals()
functions in Python?
In Python, the locals()
and globals()
functions are used to access local and global symbol tables, respectively. These functions return dictionaries representing the current local and global symbol tables, allowing you to view or modify variable names and their values.
Here's an example program illustrating the use of locals()
and globals()
:
# Using locals() and globals() functions
global_variable = 10
def example_function():
local_variable = 20
print("Local variables using locals():", locals())
example_function()
print("\nGlobal variables using globals():", globals())
In this example, the example_function
prints the local variables using the locals()
function. The global variables are printed outside the function using the globals()
function. These functions are useful for debugging, introspection, and dynamic code generation scenarios.
In Python, you can check the type of a variable using the type()
function. The type()
function returns the type of the specified object.
Here's an example program demonstrating how to check the type of a variable:
# Checking the type of a variable using type()
variable_1 = 42
variable_2 = "Hello, Python!"
variable_3 = [1, 2, 3]
print("Type of variable_1:", type(variable_1))
print("Type of variable_2:", type(variable_2))
print("Type of variable_3:", type(variable_3))
In this example, the type()
function is used to check the types of three different variables: an integer, a string, and a list. The output will display the respective types of these variables.