Variables in Python: Python For Beginners
Introduction
This tutorial is for Python beginners who are confused with the usage of python variables. I will be first starting with the general definition of the term variable and then I will go on to explain it's usage, how it is created and the advantages of using python variables.
What is a Variable
A Variable is a container or placeholder for storing values while we carry out several tasks inside a Python function. A variable can store integers, float, string,list,tuple and even boolean.
How Variables are Created & Declared
In Python, we do not need any specific method or syntaxes to create variables. There is no need of explicitly declaring a variable. We just create a variable by assigning it a value with the 'equal to' sign and then a variable is created immediately!
For Example:
name = "Tracy Wills"
age = 25
year = 2025
In the above examples, we created a variable by assigning it a value of "Tracy Wills" . We created another variable called age by assigning it a value of 25 and another variable called, year by assigning it a value of 2025.
How to Name a Variable
A variable can be named using alphabets or alpha-numerals, but, it cannot start with a numeral. It can have characters like _(underscore) and could start with an underscore but not hyphen(-) or any other characters. A variable name cannor have any reserved names in python such as if, else, or, etc. If you use more than one word to name a variable, ensure that it is separated by using an underscore( _ ) and not a hyphen( - ) or number or special character. You may have numbers along with alphabets to name a variable but, remember, the variable name cannot start with a number.
For example, a variable can be named as age1 but not as 1age.
While naming a variable, we need to ensure that it is a sensible name and stands for the purpose of its usage or task. For example, we can name a variable that stores the ages of students as students_age or age_students, or age, but not something such as whose_age or age_num etc.
Also, avoid using absurd abbreviations while naming your variables. The abbreviations must be popular or readable , in case, you are using them. For instance, it is alright to use num instead of numbers as it is a popular naming terminology for numbers , but, avoid using abbreviations such as no instead of number or num. How are we supposed to guess that 'no' stands for number?? Another bad example is naming a variable as cnt instead of count. How do I know if I am reading your code that you meant cnt as count. A variable name should be descriptive of its usage, while maintaining its readability, not,just give any name to it, for the sake of naming it.
While naming a variable, you need to ensure that it makes sense,not only to you, but also, to others or rather anyone who reads your code.
Initialization of Variables
Unlike other projects languages, there is no specific syntaxes for initializing python variables. The moment we create a variable, then the variable is automatically initiated.
We have learnt above how the variable is created in the above paragraphs.
Uses of Variables
Variables are used to store data and retrieve it snd use when called for. Therefore it makes readable and re usable and renders the flow of code so efficient and smooth. Another good thing about variables is that we can re-assign values to variables easily, by just changing the value thatcwe assigned to the same variable previously in the program.
Types of Variables
There are two types of variables based on their scope, namely, local variable and global variable.
Local variables are created inside a function and global variables are created outdide the function.
example of local variable:
Consider the code block below.
def sum(a,b):
sum = a+b
return sum
print(sum(100,75))
In the above example, a and b are local variables as they are located and created inside the function sum()
Consider this example of a local variable in the code block below:
def my_function():
variable = "This is local variable"
print(variable)
# possible as local variable is accessible inside the function
my_function()
print(variable)
#wrong since local variable cannot be accessed outside the function.
In the above function variable is a local variable as it is created inside the function, my_function() and in the first case, when rhe program was asking to print(variable) inside the functio , it was possible to do so, as local variable is accessible locally, however,
Let us now consider the example of global variable below:
#global variable
#example
x = "global variable "
def my_function():
print("This variable is", x)
my_function()
in the above case, x is a global variable, created outside the function my_function() hence it can be accessed from outside the function.
We can create a global variable usung global keyword. Please refer to example below:
#use global keyword to create global variable inside a function
def my_function():
global x
x = " global variable ! wow!"
my_function()
print(x)
In the above example even though x is a local variable by using the global keyword, it is rendered as a global variable. Hence, it can now be accessed from outside the function as shown above, we are able to access the variable x by using the print() from outdide the my_function()
Another example:
#to change value of global variable inside a function use global keyword
x = " global variable"
def my_function():
global x
x = "I love programming"
my_function()
print(x)
I will be writing more indepth details on the types of variables in another post, so, please look out for that post soon.
Conclusion
In this post, we have learnt all the essentials about variables in Python. Now we know how to create/declare variables, name them based on the naming convention and all the advantages of using python variables in functions. We also learnt briefly about the two important types of variables, namely, local variable and global variable, based on their location and accessibility, with some basic examples.
Happy coding in Python.
Read more of lessons related to python beginner courses
Test comment
ReplyDelete