Variables in Python are dynamically typed, Meaning the datatype is inferred frm their assigned value.…

" />
×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION Roadmap
     ❯   

VARIABLES & TYPES IN PYTHON

Variables & types home

Variables are containers that can  hold data values. Variables in Python are dynamically typed, Meaning the datatype is inferred frm 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 by using classes. so there are classes called int, str, float,complex and more. when we create a variable of a type, what we effectively are doing is 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'>

'''