What are Python variables?
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. 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. 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, label with name; those labels are called variables, and that variables store values.”
How To Create A Variable?
“In other languages, there are commands to assign/declare variables, but in Python, their 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:
Code:
first_name1 = "Python"
Now here we created a variable. We name it first_name1
and that stores a value Python
. Now this first_name1
is stored in Python memory as a variable, and whenever we use this variable, it will show the value it stores. For example:
Code:
first_name1 = "Python"
print(first_name1)
Output:
Python
So we get Python in the result because the first_name1
variable stores Python as a value. Same if we have multiple variables.
Code:
first_name1 = "Python"
first_name2 = "Java"
first_name3 = "JavaScript"
Now here we created three variables that store different values, and if we try to print the value of first_name1
, the result will show "Python"
.
Code:
first_name1 = "Python"
first_name2 = "Java"
first_name3 = "JavaScript"
print(first_name1)
Output:
Python
Now if we update the values of these variables:
Code:
first_name1 = "Python"
print(first_name1)
first_name1 = "Java"
print(first_name1)
Output:
Python
Java
So if we update the value of a variable, the Python interpreter shows the last updated value as a result. Interpreter reads code from up to down. So first it prints Python
as a result, then it prints Java
as a result.
Variable Consist Different Kind of Values
Now in the upper example, when we store Python as a value in the first_name1
variable. At that time, Python is represented as a string. String is a data type in Python. Anything could be string if they are surrounded by quotes (“”). We can use either single quotes (‘ ‘) or double quotes (” “). For Example:
Code:
first_name = "Python"
first_num = '1000'
Now here we declare two variables, which store strings as values. However, in the second variable (first_num)
, we have a number, but we surrounded the number by quotes, which makes it a string.
type () function:
type() is a built-in function in Python. which helps us to find the data type of any values. For example:
Code:
first_name = 'Python'
first_num = '1000'
print(type(first_name))
print(type(first_num))
Output:
<class 'str'>
<class 'str'>
We get 'str'
class in the result, which means both values have a string data type. 'str'
is short for string. Now for the same example, if we did:
Code:
first_name = 'Python'
first_num = 1000
print(type(first_name))
print(type(first_num))
Output:
<class 'str'>
<class 'int'>
Now in the second variable, we change our value type. We remove the surrounding quotes. Now this is not a string. This is an integer number and integer value represented by the 'int'
class. Here are some other data types:
Code:
first_num = 1000
first_name = 'Python'
another_num = 55.48
some_fact = True
print(type(first_num))
print(type(first_name))
print(type(another_num))
print(type(some_fact))
Output:
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>
All four values have different kinds of data types. We get 'float'
class; for decimal numbers, 'float'
is short of floating-point. We get 'bool'
class. 'bool'
is short for boolean values, which are either True
or False
. So a variable can consist of any kind of value.
Only String Uses Quotes
We already saw different kinds of data types, and we can see only string values that use quotes. If we use other data type values with quotes, it will make them strings. For example:
Code:
first_name = "Python"
first_num = "1000"
another_num = 1000
print(type(first_name))
print(type(first_num))
print(type(another_num))
Output:
<class 'str'>
<class 'str'>
<class 'int'>
In the second and third variables, we used the same value but got a different kind of data type because we used "1000"
with quotes in the second and 1000
without quotes in the third.
See the below examples:
Example 1:
Code:
first_name = "Python"
print(first_name)
Output:
Python
Example 2:
Code:
first_name = "Python"
print('first_name')
Output:
first_name
Now see the difference between examples 1 and 2. In example 1, we are trying to print the value of the variable (first_name)
and we get Python
in the output. But in example 2, we are not trying to print the variable values but we make first_name
a string by surrounding quotes around them. We are simply trying to print a string in output.
End of Python Variable Part 1
2 thoughts on “Python Variables (Part 1): What Are Variables In Python?”