Introduction to Python Declare Variable
In Python, variables are defined as containers for storing the values. In this article, we will see how to declare variables in Python. We know that every variable created is an object that reserves a memory location that can store value in Python. In Python, the program variable provides data for processing to the computer. The memory location is stored according to the data type of the variable declared. In Python, variables need not be defined or declared any data type to the variable as we do in other programming languages. In this topic, we will learn about the python declare variable.
Working of Variable Declaration in Python
A variable is created as soon as you assign any value to it, and there is no need to declare any data type in Python as there is no such command to declare the variable. In general, the variable is a memory location used to store the values assigned to it, and this variable is given a name used to refer to the value stored in it for the rest of the program.
Variable_name = value_to_store
So in the above syntax, we can see to declare a variable, we directly use the equal to “=” operator, and this is also used to assign the value to the variable. Where the left-hand side of the operator will have the variable name, and the right-hand side of the variable will have the value to store in that variable. There is no need to separately declare the data type for the variable in Python as it takes automatically when you assign the value to the variable.
Certain rules are important for the developers to know before declaring the variables, and they are as follows:
- A variable must always start with either an alphabet or an underscore but not in numerical.
- A variable can contain a combination of alphabets and numbers, making a variable as alpha-numerical, but we should note that it should start with the alphabet. It can also have the variable name with a combination of alphabets and underscores.
- When declared a programmer, a variable should know that they are case sensitive, which means, for example, “a” and “A” these both variables are different and not the same.
Examples of Python Declare Variable
Given below are the examples of Python Declare Variable:
print("Program to demonstrate variable declaration:") x = 5 print("The value of Int variable is as follows:") print(x) y = "John" print("The value of string or char variable is as follows: ") print(y)
In the above program, we have seen we declared variable “x” here we have assigned value as “5’ which is an integer value. Therefore the variable “x” automatically declares it as int variable and stores the value “5” in the memory with data type “int”. In this program we also have declared another variable with name “y” and the value assigned to it is “John” which is of string or character data type. In contrast, this variable also has been declared automatically. The output can be seen in the above screenshot.
There are two types of variables that can be declared in Python such as global and local variables. Now let us see below in detail about these variables with examples. The variables used or can be accessed in the entire program or other functions or modules within the program are called global variables. Whereas the variables that are declared and used within the program’s function or module, such variables are known as local variables. We will see both these variables in one single program below.
print("Program to demonstrate global and local variable decllaration:") f = 20 def func(): f = 'I am learning Python' print("It will print the value of local variable as follows:") print(f) func() print("This will print the value of the global variable as follows:") print(f)
In the above program, we can see that we have declared a variable “f” with the value assigned to it as “20”. This variable is known as a global variable where it can be used in the entire program and has scope for it within the entire program. But then we can see in the function “func()” we have declared another variable “f”, which is a local variable and has its scope only within the function func(). Another way to declare a global variable is by using the keyword “global” whenever you want this variable to be declared inside any function.
Now we will see how to delete variables using the “del” command, which will delete the variable and if we try to print the deleted variable, it will throw an error such as NameError, which will provide a message as this variable is not defined.
print("Program to demonstrate how to delete variable:") f = 11; print("The variable declaration is done and it value is as follows:") print(f) print("The variable is deleted using del and it provides error after deleting as follows:") del f print(f)
In the above program, we can see that we have declared a variable “f”, and then using the “del” command, we are deleting the variable. When we try to print the same variable after using the “del” command, then it will throw the error NameError with the message as the variable with the name “f” is not defined. The result can be seen in the above screenshot.
In this article, we conclude that the declaring variable in Python is similar to other programming languages, but there is no need to declare any data type before any variable. When created in Python, the variable automatically takes a data type according to the value assigned to the variable. In this article, we saw how to declare variables of different data types, and we also saw what are local and global variables and their importance. At last, we also saw how to delete a variable and the error that occurred when trying to reprint the deleted variable.
This is a guide to Python Declare Variable. Here we discuss the introduction, syntax, and working of variable declaration in python along with different examples and code implementation. You may also have a look at the following articles to learn more –