In this article, we’ll be discussing python variables - what they are, and the different types of python variables. By the end of the article, you will have a good understanding of python variables and how to use them in your programs.
What is a variable
In programming, a variable is a name that refers to a value. We can use variables to store all kinds of information, from numbers and strings to more complex data structures like lists and dictionaries.
We create Python variables by assigning a value to them. For example, this code creates a variable called my_var
and assigns it the value “42”:
my_var = 42
You can access the value of a python variable by using its name. For example, if you want to print the value of my_var
, you would use the following code:
print(my_var) # prints "42"
Output
42
You can perform simple arithmetic operations using variables.
var1 = 5
var2 = 10
sum = var1 + var2
print(sum)
Output:
15
Python variables can also store a list’s information.
For example, this code creates a variable called my_list
and assigns it a reference to a list object:
my_list = [2, 4, 6] # create a list with three elements
You can access the elements of a list by using its index. For example, if you want to access the first element of my_list
you would use the following code:
print(my_list[0]) # prints "2" (the first element in the list)
Ouput
2
Importance of variables in python
Variables are essential because they allow us to write code that is more flexible and easier to read.
For example, let’s say we are supposed to store the user’s age in a variable. Rather than hard-coding the age into our program, we can create a variable called age
and set its value to the user’s input.
This way, if the user’s age changes, we only have to change it in one place - in the variable - rather than everywhere else in our code.
Types of variables in python
There are two main types of variables in python: local
and global
.
Local variables are only accessible within the scope of where they’re defined - usually within a function.
Global variables are accessible throughout your entire program. We’ll discuss both types of variables in more detail below.
Local Variables:
As we mentioned, local variables are only accessible within the scope of where they’re defined. So it means that if you create a local variable within a function, you won’t be able to access it outside that function.
Example
def age_checker(age):
if age>=18:
print("You can vote!")
else:
print("You can't vote.")
# calling the function
age_checker(20)
In the above code, we have a function called age_checker
that takes in one parameter - age. We also have an if statement that checks whether the age is greater than or equal to 18. If it is, we print out a message saying, “You can vote!”. Otherwise, we print out a statement saying, “You can’t vote.”
Notice that the variable age
is only accessible within the scope of the age_checker()
function. If we try to access it outside of the function, we’ll get an error:
def age_checker(age):
if age>=18:
print("You can vote!")
else: print("You can't vote")
age_checker(20)
print(age) # causes an error
Output
print(age)
NameError: name 'age' is not defined
We get an error when we try to print the value of age outside of the function. It’s because the variable age
only exists within the scope of the age_checker()
function - it doesn’t exist outside of that function.
Global Variables:
On the other hand, global variables are accessible throughout your entire program, meaning that you can create a global variable in one place and access it from anywhere else in your code.
Example
name = "Geeks" # this is a global variable
def greeting():
print("Hello, " + name + "!") # we can access the name variable here
greeting() # Calling the function will print "Hello, Geeks!"
Ouput
Hello, Geeks!
As you can see, we created a global variable called name
and set it to Geeks
. We then defined a function called greeting(),
which prints out a message using the name variable.
So when we call the greeting function, it prints out “Hello, Geeks!”.
This differs from our previous example because the name variable is accessible outside the function. This is because it’s a global variable, and global variables are accessible throughout your entire program.
Benefits vs Drawbacks of using Global variables
One benefit is that you can access the variable anywhere in your code. Therefore, it is helpful if you need to use the same variable in multiple places.
However, one drawback is that global variables can make your code harder to read. This is because it’s not always clear where the variable is being defined and used. It’s also easy to accidentally change a global variable when you meant to change a local variable - which can cause some unexpected errors in your code!
Overall, it’s generally best to avoid using global variables whenever possible. If you need to use them, try to limit their scope as much as possible so that they’re only accessible from where you need to use them.
Conclusion
In conclusion, Python variables are data types that can be assigned to a name and accessed anywhere in your code. There are two main types of python variables: global and local. Global variables are accessible throughout your entire program, while local variables are only accessible within the scope of their function. When choosing between global and local variables, it’s generally best to limit the scope of your variables as much as possible. Thanks for reading! We hope this article helped you understand python variables. If you have any questions or comments, please leave them below!
Happy coding!