In Python, Data types represent the type or category of data that a variable or object can hold.
Python is dynamically typed, which means that we don't need to declare the type of a variable explicitly.
Instead, Interpreter binds the value implicitly to its type based on the value assigned to it.
We can check the type of the variable in Python using "type()" function and it returns the type of the passed variable.
Here are the primary data types in Python:
Represents integers (whole numbers), e.g., 10, -5, 1000.
age = 55 print("Type of age Variable is:", type(age)) # Output: Type of age Variable is: <class 'int'>
totalAmount = 40.5 print("Type of totalAmount Variable is:", type(totalAmount)) # Output: Type of totalAmount Variable is: <class 'float'> # Type Checking function print(isinstance(totalAmount, float)) # Output: True
Represents floating-point numbers (decimal numbers), e.g., 3.14, -0.5, 2.0.
equationVar = 3 + 4j print("Type of equationVar Variable:", type(equationVar)) # Output: Type of equationVar Variable: <class 'complex'> # Type Checking function print(isinstance(equationVar, complex)) # Output: True
Represents complex numbers in the form a + bj, where a and b are real numbers, and j is the imaginary unit, e.g., 3 + 4j, 1.5 - 2.3j.
isStudent = True; print("Type of isStudent Variable:", type(isStudent)) # Output: Type of isStudent Variable: <class 'bool'> isAdult = False; print("Type of isAdult Variable:", type(isAdult)) # Output: Type of isAdult Variable: <class 'bool'>
Represents boolean values, True or False, which are used for logical operations and control flow.
stringValue = "string declaration and initialization";
marks = [45, 25, 66, 88, 50] # Type of variable marks print(type(marks)) # Output: <class 'list'> # Type Checking function print(isinstance(marks, list)) # Output: True
marks = (45, 25, 66, 88, 50) # Type of variable marks print(type(marks)) # Output: <class 'tuple'> # Type Checking function print(isinstance(marks, tuple)) # Output: True
marksRange = range(33, 99) # Type of variable marks print(type(marksRange)) # Output: <class 'range'> # Type Checking function print(isinstance(marksRange, range)) # Output: True
Represents strings, which are sequences of characters enclosed within single quotes ', double quotes ", or triple quotes ''' or """.
Represents ordered collections of items, which can be of different data types and mutable (modifiable).
# Create a dictionary dictObject = {'name': 'Alice', 'age': 20, 'isStudent': True} # Type of variable dictObject print(type(dictObject)) # Output: <class 'dict'> # Type checking function print(isinstance(dictObject, dict)) # Output: True
Similar to lists but immutable (unchangeable), denoted by parentheses ().
marks = {45, 25, 66, 88, 50} # Type of variable marks print(type(marks)) # Output: <class 'set'> # Type Checking function print(isinstance(marks, set)) # Output: True
Represents a sequence of numbers, commonly used in for loops and iterations.
Represents a collection of key-value pairs enclosed within curly braces {}, where keys must be unique and immutable (e.g., strings, numbers, tuples).
# Assigning None to a variable noneVar = None; print(type(noneVar)) # Output: <class 'NoneType'>
# bytes object bytesObject = bytes([65, 66, 67, 68, 69])
Represents an unordered collection of unique elements, enclosed within curly braces {}.
# bytearray object byteArray = bytearray([65, 66, 67, 68, 69])
Similar to sets but immutable.
Represents the absence of a value or a null value.
Represents sequences of bytes, typically used for handling binary data.
Mutable version of bytes.