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 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?

Leave a Reply

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