In Python, the `type()` function is a built-in function that returns the type of an object or a variable. This function is very useful when you want to determine the kind of value or expression during debugging or when you're writing dynamic code that needs to handle different types differently.
Syntax:
type(object)
Parameters:
object: The variable or value whose type needs to be determined.
Return Value:
The `type()` function returns a type object representing the type of the given object.
Examples:
# For an integer
num = 5
print(type(num)) # Outputs: <class 'int'>
# For a string
string = "Hello, World!"
print(type(string)) # Outputs: <class 'str'>
# For a list
mylist = [1, 2, 3]
print(type(mylist)) # Outputs: <class 'list'>
# For a tuple
mytuple = (1, 2, 3)
print(type(mytuple)) # Outputs: <class 'tuple'>
# For a dictionary
mydict = {"name": "Alice", "age": 25}
print(type(mydict)) # Outputs: <class 'dict'>
In Python, everything is an object. This means that Python views even functions, modules, and classes as objects. Consequently, you can use the `type()` function to determine the type of almost everything in Python.