How to Declare a Variable in Python?
The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose values can be changed or varied.
In this article, we will discuss how to declare a variable in python.
Declaring a variable in Python programming language is a demonstration of the steps that we discuss in the algorithms.
They are the containers that can hold values of any datatype and occupy a space in the memory.
Let us have a look at the naming convention of our variables that makes them an authentic one-
Following are the rules that we must take care of-
- The name of our variable can have an underscore ( _ ).
- The name of the variable should not contain any special character.
- The name of the variable should not be a keyword.
We will discuss the given examples that will help us to understand the different ways of declaring a variable.
- A simple assignment of values to variables.
- Assigning values to variables in the same line.
- Assigning a single value to three variables at a time.
Let us have a look at the first example where we have assigned an integer value, a float value, and a string value to the variables a,b, and c respectively.
Following program illustrates the same-
INPUT-
#assigning values to variables a=2 b=2.5 c='Python' print(a) print("The type of a is {}".format(type(a))) print(b) print("The type of b is {}".format(type(b))) print(c) print("The type of c is {}".format(type(c)))
OUTPUT-

In the output, we can observe the following things-
- The values of a,b and c are displayed.
- The data type is also printed since we have used type() here.
In the second example, we have type casted the values of a,b and c and then printed their data types.
Let us see what happens when we execute the program given below-
#Typecasting
INPUT-
#assigning values to variables a=2 b=2.5 c='15' print(a) print("The type of a is {}".format(type(a))) print(b) print("The type of b is {}".format(type(b))) print(c) print("The type of c is {}".format(type(c))) #typecasting x=float(a) y=int(b) z=int(c) print(x) print("The type of x is {}".format(type(x))) print(y) print("The type of y is {}".format(type(y))) print(z) print("The type of z is {}".format(type(z)))
OUTPUT-

In the output, we can observe the following things-
- The values of a,b and c are displayed.
- The data type is also printed since we have used type() here.
- The next set of values are the ones that are typecast as float and integer.
- We have verified their data types using type() here.
The second method that we will discuss here illustrates how we can assign values to two variables at a time.
The following program shows how we can implement this-
INPUT-
#assigning values to variables a,b=4,5 print("The sum of a and b is {}".format(a+b)) print("The difference of a and b is {}".format(a-b)) print("The product of a and b is {}".format(a*b)) print("The division of a and b is {}".format(a/b))
OUTPUT-

In the output, we can observe the following things-
- The variables a and b hold the values 4 and 5 respectively.
- The expected results are displayed when the four basic arithmetic operations are applied.
In the last method, we will see how we can assign a single value to the three variables a,b and c at a time.
The following program illustrates the same-
INPUT-
a=b=c=9 print(a) print(b) print(c)
OUTPUT-

In the output, we can observe that a,b and c hold the same value which is 9.
In the next program, we will see what are the results when we apply arithmetic operations on them.
Let’s see what happens when we execute this-
INPUT-
a=b=c=9 print("The sum of a and b is {}".format(a+b)) print("The difference of a and b is {}".format(a-b)) print("The product of a and b is {}".format(a*b)) print("The division of a and b is {}".format(a/b))
OUTPUT-

In the output, we can observe the following things-
- The variables a and b hold the same value which is 9.
- The expected results are displayed when the four basic arithmetic operations are applied.
You can try and check the results when we apply these operations on a and c or a, b and c together.
So, here we discussed the rules for declaring a variable in Python and how we can declare it.