Variables & types home

VARIABLES & TYPES IN PYTHON


Variables are containers that can hold data values. Variables in Python are dynamically typed, meaning the datatype is inferred from their assigned value.

Python has the following data types built-in by default, in these categories:

  1. Text Type: str
  2. Numeric Types: int, float, complex
  3. Sequence Types: list, tuple, range
  4. Mapping Type: dict
  5. Set Types: set, frozenset
  6. Boolean Type: bool
  7. Binary Types: bytes, bytearray, memoryview
  8. None Type: NoneType

There is a function to check the datatype of a variable or data structure. The command is:

type(variable_name)
'''
  output:
    <class 'datatype'>
'''

It returns the class the variable belongs to.

Why does this return a class?

In Python, datatypes are implemented using classes. There are classes called int, str, float, complex, and more. When we create a variable of a type, we are effectively creating an object of the class of the datatype.

Simple exercise:

a = 12       # int
b = 12.42      # float
c = "Django tutorial"      # str

print("a: ", type(a))
print("b: ", type(b))
print("c: ", type(c))
'''
    output:
        a: <class 'int'>
        b: <class 'float'>
        c: <class 'str'>
'''