Previous Tutorial: Python Introduction (Part 4)
Python Variables
Variables are basic concepts in Python. Python variables are used to store values. Variables are like containers to store values. Python has different kinds of data types. Every value represents a data type in Python. We can create and store any kind of values in variables.
Python is a dynamically typed language, so we do not need to declare variables before using them or declare their data type. A variable is created the moment we first assign a value to it.
We can also say, “A Python variable is a name given to a memory location.”
“In real life, we store items in different boxes and label the box with a name to remember.“ Same in Python, we store values/items and label them with names; these labels are called variables, and these variables store values.
How to Create a Variable in Python?
“In other languages, there are commands to assign/declare variables, but in Python, there is no command to declare variables. We created a variable by assigning a value to it. We use the equal sign (=)
to assign value.” For example:
FirstName = 'Python'
Now here we created a variable. We named it FirstName
and that stores a value Python
. Now this
is stored in Python memory as a variable, and whenever we use this variable, it will show the value it stores. For example:FirstName
FirstName = 'Python'
print(FirstName)
Python
So we get Python
in the output because this FirstName
variable stores Python
as a value. Now, if we have multiple variables.
FirstName = 'Python'
SecondName = 'Java'
ThirdName = 'PHP'
print(SecondName)
Java
Now here we have multiple variables, and every variable stores a value. So when we use print(SecondName)
, we get Java
in the output.
Now what if we update a variable value?
FirstName = 'Python'
print(FirstName)
FirstName = 'Programming'
print(FirstName)
Python
Programming
We get the latest updated value in the output. Because Python interpreter reads code from top to bottom, first it will read that FirstName
variable consists of Python
as a value. So it will print Python in the output. But in the 3rd line, the interpreter reads that now FirstName
consists Programming
as value. So it will print Programming
in the output.
So this is how we create a variable in Python; there is no command. We simply write a variable name, then an equal sign (=), and write a value. We don’t have to declare the value type. Now let’s see what kind of values a variable can store in Python.
What Kind of Value Can a Variable Store?
A variable can store any kind of value. Each value represents a data type in Python. Python has lots of data types. For example:
FirstLine = 'Hello'
Here we create a variable named FirstLine
. This variable stores a value, 'Hello'
. This is a simple text, and in Python, text is represented by the string data type. Any value could be a string if it is surrounded by quotes (“”)
. We can use either single quotes (' ')
or double quotes (" ")
. For example:
FirstLine = 'Hello'
SecondLine = "World!"
ThirdLine = '1000'
Here we created different values with variables. We use a single quote value in the first line and a double quote value in the second line. We use a number as a value in the third line, but because we surrounded this number by quotes to makes it a string data type.
FirstLine = 'Hello'
SecondLine = True
ThirdLine = 500
FourthLine = [1, 2, 3]
Here we created four variables that consist of different kinds of values. There is a built-in function in Python named type()
, which is used to find the data type of a value. Let’s see, how we use type() function.
Python type() Function
type() is a built-in function in Python, which is used to find the data type of a value. For example:
FirstLine = 'Hello'
SecondLine = True
ThirdLine = 500
FourthLine = [1, 2, 3]
print(type(FirstLine))
print(type(SecondLine))
print(type(ThirdLine))
print(type(FourthLine))
<class 'str'>
<class 'bool'>
<class 'int'>
<class 'list'>
So we get all the data type of values in the output. The first value is ‘Hello’ which is a string, and it is denoted by the ‘str’ class. The second value is True which is a boolean data type, and it is denoted by the ‘bool’ class. The third value is 500 which is a number, and it is denoted by ‘int’ class. The fourth value is [1, 2, 3] which is a example of sequence data type and in this case it is a list, and it is denoted by ‘list’ class.
One thought on “Python Variables (Part 1): What Are Variables in Python?”