Python Tutorial > Python Basics > Python Variables (Part 1): What Are Variables in Python?
Posted in

Python Variables (Part 1): What Are Variables in Python?

Python Variables (Part 1)
Python Variables (Part 1)

Previous Tutorial: Python Introduction (Part 4)

Python Variables

We can also say, “A Python variable is a name given to a memory location.”

How to Create a Variable in Python?

CODE:
FirstName = 'Python'
CODE:
FirstName = 'Python'
print(FirstName)
OUTPUT:
Python
CODE:
FirstName = 'Python'
SecondName = 'Java'
ThirdName = 'PHP'
print(SecondName)
OUTPUT:
Java
CODE:
FirstName = 'Python'
print(FirstName)
FirstName = 'Programming'
print(FirstName)
OUTPUT:
Python
Programming

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:

CODE:
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:

CODE:
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.

CODE:
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:

CODE:
FirstLine = 'Hello'
SecondLine = True
ThirdLine = 500
FourthLine = [1, 2, 3]
print(type(FirstLine))
print(type(SecondLine))
print(type(ThirdLine))
print(type(FourthLine))
OUTPUT:
<class 'str'>
<class 'bool'>
<class 'int'>
<class 'list'>

So we get all the data types 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 the ‘int’ class. The fourth value is [1, 2, 3] which is an example of sequence data type, and in this case it is a list, and it is denoted by the ‘list’ class.

So this is how we can check any data type with the help of type() function. We can also use type() function like this:

CODE:
print(type('Python') is str)
print(type(500) is int)
print(type(100) is str)
OUTPUT:
True
True
False

We can directly check value with the help of type() function.

One thought on “Python Variables (Part 1): What Are Variables in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *