Page Contents
Python Variables
Hello Friends. In my last post, we discussed about String manipulation using built-in functions. In this post I will talk about the concept of python variables.
Variables are used to store data values. They are like containers that hold information which can be referenced and manipulated throughout the program. Variables in Python are dynamically typed, meaning you don’t need to explicitly declare the type of a variable before assigning a value to it.
Here’s an explanation with examples:
message = "Hello Proedu!" marks = 100 print(message) print(marks) # Output # Hello Proedu! # 100
In this example, variable message is assigned the String value Hello Proedu!, and variable marks is assigned the integer value 100.
Variable naming rules in Python
Variable name should be descriptive enough to show the actual purpose of variable but you can also create variables with names like x and y. Below are python rules for naming a variable:
- Variable ame must start with a letter or the underscore character but it cannot start with a number.
- Variable name can only contain alpha-numeric characters and underscores i.e (A-z, 0-9, and _ ).
- Variable names are case-sensitive (proedu, Proedu and PROEDU are three different variables).
- Variable name must not be any of the Python keywords.
Here are some examples of legal variables names:
proedu = "Proedu Examples" pro_edu = "Proedu Examples" _pro_edu = "Proedu Examples" proEdu = "Proedu Examples" Proedu = "Proedu Examples" PROEDU = "Proedu Examples" proedu1 = "Proedu Examples" print(proedu) print(pro_edu) print(_pro_edu) print(proEdu) print(Proedu) print(PROEDU) print(proedu1) # Output # Proedu Examples # Proedu Examples # Proedu Examples # Proedu Examples # Proedu Examples # Proedu Examples # Proedu Examples
Here are some examples of Illigal variables names:
2proedu = "Proedu Examples" print(2proedu) # SyntaxError: invalid decimal literal pro-edu = "Proedu Examples" print(pro-edu) # SyntaxError: cannot assign to expression here. pro edu = "Proedu Examples" print(pro edu) # SyntaxError: invalid syntax
Variable reassignment in python
Python allows you to re-assign a different value to a variable, even if it is having a different data type. Here is an example
# Creating a variable. message = "Hello Proedu!" print(message) # Re-assigning a new value of same data type. message = "Hi Again" print(message) # Re-assigning a new value of different data type. message = 100 print(message) # Output # Hello Proedu! # Hi Again # 100
Multiple Assignment
Python allows you to assign values to multiple variables in one line. Here is an example
length, width, height = 10, 8, 6 print("Volume of Cuboid: "+str(length * width * height)) # Output # Volume of Cuboid: 480
We can also assign the same value to multiple variables as shown below
length = width = height = 10 print("Volume of Cuboid: "+str(length * width * height)) # Output # Volume of Cuboid: 1000
Unpacking a collection to multiple variables
Unpacking extracts values of a collection to multiple variables as shown below
days_of_week = [10, 8 , 6] length, width, height = days_of_week print(length) print(width) print(height) # Output # 10 # 8 # 6
Global variables and Local variables in python
Global variables are accessible throughout the program, while local variables are accessible only within the defined function. Here is an example
# Creating a Global variable. global_var = 100 def my_function(): # Local variable local_var = 50 print("Inside the function:", local_var) my_function() print("Outside the function:", global_var) # Output # Inside the function: 50 # Outside the function: 100
In this example, global_var
is a global variable accessible throughout the program, while local_var
is a local variable accessible only within the my_function()
function. If we try to access local_var outside the function, it will result into an error.
# Creating a Global variable. global_var = 100 def my_function(): # Local variable local_var = 50 print("Inside the function:", local_var) my_function() print("Outside the function:", global_var) print("Accessing local variable outside function: ", local_var) # Output # NameError: name 'local_var' is not defined. Did you mean: 'global_var'?
Can we create a global variable inside a function – global keyword
To create a global variable inside a function, you can use the global keyword.
global_var = 100 def my_func(): global global_inside_func global_inside_func = 200 print("Global Variable:", global_var) my_func() print("Global variable defined inside function", global_inside_func) # Output # Global Variable: 100 # Global variable defined inside function 200
In this example, we have created a global variable named global_inside_func inside function my_func. This variable can be accessed outside the scope of the function as well.
Modifying a global variable inside a function
To change the value of a global variable inside a function, refer to the variable by using the global keyword as shown below
message = "Hello Proedu!!!" def my_func(): global message message = "Hi Again!!!" print("Original Message: ", message) my_func() print("Modified Message: ", message) # Output # Original Message: Hello Proedu!!! # Modified Message: Hi Again!!!Output