Hello Friends,
Python Syntax and Indentation
Python uses indentation instead of brackets to define code blocks.
Correct Example
if 10 > 5:
print("10 is greater than 5")
Incorrect Example
if 10 > 5:
print("Error")
=======================================================================Variables and Data Types
Variables store data values in Python.
Common Data Types
- int
- float
- string
- boolean
Example
age = 30 name = "Chandan" price = 99.50 is_active = True print(age) print(name) print(price) print(is_active)
type() in Python – Complete Guide with Examples
The type() function in Python is a built-in function used to determine the data type of a variable, object, or value.
It plays an important role in debugging, type checking, dynamic programming, and object-oriented programming.
🔹 Syntax of type()
type(object)
type(name, bases, dict)
Explanation:
- Single argument: Returns the type/class of the object
- Three arguments: Dynamically creates a class (advanced usage)
🔹 Basic Example
x = 10
y = 3.14
z = "Python"
a = [1, 2, 3]
b = {"name": "Chandan", "role": "Tester"}
print(type(x))
print(type(y))
print(type(z))
print(type(a))
print(type(b))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'dict'>
🔹 type() with User Input
user_input = input("Enter something: ")
print(type(user_input))
Output:
<class 'str'>
👉 Note: input() always returns a string, so conversion is required:
num = int(input("Enter number: "))
print(type(num))
🔹 type() with Custom Class
class Employee:
pass
emp = Employee()
print(type(emp))
Output:
<class '__main__.Employee'>
🔹 Dynamic Class Creation using type()
Person = type("Person", (), {
"name": "Chandan",
"age": 25,
"greet": lambda self: f"Hello, my name is {self.name}"
})
p = Person()
print(p.name)
print(p.greet())
Output:
Chandan
Hello, my name is Chandan
🔹 type() vs isinstance()
| type() | isinstance() |
|---|---|
| Checks exact type | Checks inheritance also |
| Not inheritance-friendly | Inheritance-friendly |
| Less flexible | More flexible |
Example:
class Animal:
pass
class Dog(Animal):
pass
d = Dog()
print(type(d) == Animal) # False
print(isinstance(d, Animal)) # True
🔹 Real-world Use Cases
- ✔ Debugging variable types
- ✔ Data validation
- ✔ Dynamic behavior control
- ✔ Framework development
- ✔ API response validation
- ✔ Serialization & deserialization
🔹 Common Mistakes
- ❌ Using
type()instead ofisinstance()for inheritance checks - ❌ Assuming
input()returns numbers - ❌ Hardcoding type checks instead of polymorphism
🔹 Best Practices
- ✅ Prefer
isinstance()for type checking - ✅ Use
type()for debugging/logging - ✅ Avoid tight coupling with specific types
- ✅ Use duck typing where possible
🔹 Interview Questions
- What is the use of
type()in Python? - Difference between
type()andisinstance()? - Can
type()create classes dynamically? - Is
typea class in Python? - Explain metaclasses in Python
🔹 Conclusion
The type() function is a powerful built-in feature of Python that helps in identifying object types,
creating dynamic classes, debugging applications, and building flexible systems. While it is useful, Python developers
should prefer isinstance() for most type-checking scenarios to support inheritance and polymorphism.
Understanding type() gives you deeper insight into Python’s object model and dynamic nature.
Related Topics:
isinstance(),
id(),
__class__,
metaclasses,
duck typing,
reflection
No comments:
Post a Comment