Variables in python

First of all, variables in Python don’t denote a specific type and therefore you don’t need a type declaration in Python. So, unlike other programming languages such as C++ or Java, variables do not need to be declared.

In Python, as already mentioned, a variable can be used immediately without a declaration of the data type. Nevertheless, Python assigns a data type, i.e. depending on the data type, the variable is created differently, i.e. as integer, float, string, and so on.

The data type in Python is not bound to the variable, but to the value, which implies that the type can change at runtime.

Code Explanation

LineDescription
1Creates a variable with the value 5 (integer)
2Creates a variable with the value 5.5 (float)
3Creates a variable with the value 1j (complex)
4Creates a variable with the value “hello” (str)
5Creates a variable with the value True (bool)
6Creates a variable with the value [1, 2, 3] (list)
7Creates a variable with the value (1, 2, 3) (tuple)
8Creates a variable with the value {1, 2, 3} (set)
9Creates a variable with the value {1: 2, 2: 3} (dict)
10Creates a variable with the value {“first”, “second”, “third”} (frozenset)
11Creates a variable with the value b”Hello” (bytes)
12Creates a bytearray
13Creates a memoryview
15 – 27Specifies the data types of the previously created variables
a = 5
b = 5.5
c = 1j
d = "hello"
e = True
f = [1, 2, 3]
g = (1, 2, 3)
h = {1, 2, 3}
i = {1: 2, 2: 3}
j = frozenset({"first", "second", "third"})
k = b"Hello"
l = bytearray(2)
m = memoryview(bytes(2))

print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(type(g))
print(type(h))
print(type(i))
print(type(j))
print(type(k))
print(type(l))
print(type(m))
Output
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'frozenset'>
<class 'bytes'>
<class 'bytearray'>
<class 'memoryview'>