In this tutorial, we will learn the Python variables. Variables are used as a container to store various data values. For initializing or declaring a variable, Python has no command. But for other programming languages, it must have some commands to use variables. In Python, when assigning a value to a variable that time a variable is created. It is also treated as a reserved memory location for various types of data. Variables give data to the processer for processing and return to the program by other variables. Every variable must have a datatype. Variables or identifiers can be processed by any name and it can be declared any time in the program. Some rules need to keep in mind while declaring variables and identifiers. It is already discussed in the previous module called Python identifiers.
Python support various data types like integer, float, string
, etc. It is easy to store data in a variable.
x = 10
#Initialize variable x and value 10 (integer)
y = "Hello"
#Initialize variable y and value Hello (string)
print(x) #This will print 10
print(y) #This will print Hello
The data type of a variable depends on the value type and it will change over time. Python always considers the latest input. Sting Input can be declared as single or double-quotes.
x = 10
#Initialize variable x and value 10. It is integer
x = "Hello"
#Initialize variable x and value Hello. It is now string but the variable name is the same
x = ‘Hello’
#Initialize variable x and value Hello. The value is the same but used in singe quote
print(x) #This will print Hello
Python has a feature of storing the same value in various variables by writing a single line statement. It means the same value will be assigned to as much variables as we want.
a=b=c=2
x=y=z=’Python’
print(a) #It will print 2
print(b) #It will print 2
print(c) #It will print 2
print(x) #It will print Python
print(y) #It will print Python
print(z) #It will print Python
By observing the above examples, it is easy to understand that the declaration is based on the equal sign (=)
. The left part of the equal to the operator (=)
is known as variable and the right part of the equal to the operator is known as value.
Value1=12 #integer
Value2=14.5 #float
Value3='Python' #String
print(Value1) #It will print 12
print(Value1) #It will print 14.5
print(Value1) #It will print Python
It can be started with a letter or the underscore character. It cannot start with a number. It can be made through alpha-numeric characters, alphabets, and underscores and these all are case sensitive. A variable can have a short name such as a, b, c, etc. It also can have a descriptive name such as name, result, total, etc.
#Legal variable names are given below:
myvariable = "Python"
my_variable = 100
_my_variable = 11.59
myVariable = "Denny"
MYVARIABLE = 30
myvariable2 = 20.20
#Illegal variable names are given below:
2myvariable = 10
my-variable = "Java"
my variable = 22.1
Python permits us to assign several values to numerous variables by writing a statement.
a, b, c = "Apple", 2, 25.66
print(a) #It will print Apple
print(b) # It will print 2
print(c) # it will print 25.66
Python has an in-build keyword called ‘Global’. These variables must be created outside of a function. So that these variables can be operated in both the external and internal of the program.
a = "Python"
def demofunction():
print("Hello " + a)
demofunction ()
# the output of this code will give be Hello Python