• Home
  • 2.7 Type Checking

2.7 Type Checking

View Categories

2.7 Type Checking

3 min read

Every value in Python has a data type. Sometimes it is necessary to determine the type of a value before performing an operation.

Python provides the built-in type() function to determine the data type of an object.

Type checking is commonly used for:

  • Debugging programs
  • Verifying user input
  • Performing different actions based on data type
  • Preventing runtime errors

Syntax #

type(object)

Parameter #

Parameter Description
object The value or variable whose data type is to be determined.

Return Value #

The type() function returns the data type (class) of the specified object.


Example 1: Checking the Type of an Integer #

main.py

number = 100

print(type(number))

Output #

<class 'int'>

Explanation #

  • The variable number stores the integer value 100.
  • The type() function determines the data type of the value stored in the variable.
  • Since 100 is an integer, Python returns <class 'int'>.

Example 2: Checking Different Data Types #

main.py

print(type(25))
print(type(3.14))
print(type("Python"))
print(type(True))

Output #

<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

Explanation #

  • 25 is an integer.
  • 3.14 is a floating-point number.
  • "Python" is a string.
  • True is a Boolean value.
  • The type() function correctly identifies the data type of each value.

Example 3: Checking the Type of Variables #

main.py

name = "Alice"
age = 25
salary = 45000.50

print(type(name))
print(type(age))
print(type(salary))

Output #

<class 'str'>
<class 'int'>
<class 'float'>

Explanation #

  • name stores a string.
  • age stores an integer.
  • salary stores a floating-point number.
  • type() works with variables as well as literal values.

Example 4: Type Changes After Assignment #

main.py

value = 100

print(type(value))

value = "Python"

print(type(value))

Output #

<class 'int'>
<class 'str'>

Explanation #

  • Initially, value stores an integer.
  • Later, a string is assigned to the same variable.
  • Since Python is dynamically typed, the variable now refers to a string object.
  • The type() function reflects this change.

Example 5: Checking the Type of User Input #

main.py

age = input("Enter your age: ")

print(type(age))

Sample Input #

Enter your age: 25

Output #

<class 'str'>

Explanation #

  • The user enters 25.
  • Even though the input looks like a number, input() always returns a string.
  • Therefore, the data type displayed is str.

Example 6: Checking the Type After Conversion #

main.py

age = int(input("Enter your age: "))

print(type(age))

Sample Input #

Enter your age: 25

Output #

<class 'int'>

Explanation #

  • input() first returns a string.
  • The int() function converts the string into an integer.
  • After conversion, the variable stores an integer.
  • Therefore, type() returns <class 'int'>.

Example 7: Checking Collection Types #

main.py

numbers = [10, 20, 30]
student = ("Alice", 20)
marks = {"Math": 95}

print(type(numbers))
print(type(student))
print(type(marks))

Output #

<class 'list'>
<class 'tuple'>
<class 'dict'>

Explanation #

  • A list has the type list.
  • A tuple has the type tuple.
  • A dictionary has the type dict.
  • Every built-in collection in Python has its own data type.

Example 8: Comparing Data Types #

main.py

number = 50

print(type(number) == int)
print(type(number) == float)

Output #

True
False

Explanation #

  • type(number) returns int.
  • The first comparison checks whether the type is int.
  • Since it is an integer, the result is True.
  • The second comparison checks against float, so the result is False.

Note: This method works, but Python generally recommends using isinstance() for type checking, especially when working with classes and inheritance.


Example 9: Using isinstance() #

main.py

number = 100

print(isinstance(number, int))
print(isinstance(number, float))

Output #

True
False

Explanation #

  • isinstance() checks whether an object belongs to a specified type.
  • Since number is an integer, the first statement returns True.
  • It is not a floating-point number, so the second statement returns False.
  • isinstance() is the preferred way to check types in most Python programs.

Example 10: Checking Multiple Types #

main.py

value = 25.5

print(isinstance(value, (int, float)))

Output #

True

Explanation #

  • The second argument to isinstance() can be a tuple of data types.
  • Python checks whether the object belongs to any of the specified types.
  • Since value is a floating-point number, the result is True.

Common Mistakes #

1. Assuming input() Returns an Integer #

Incorrect

age = input("Enter age: ")

print(type(age))

Sample Input #

25

Incorrect Expectation #

<class 'int'>

Actual Output #

<class 'str'>

Reason

The input() function always returns a string.


2. Comparing a Value Instead of Its Type #

Incorrect

number = 10

print(number == int)

Output #

False

Reason

The value 10 is being compared with the class int, not its data type.

Correct

print(type(number) == int)

3. Confusing type() with isinstance() #

Incorrect Assumption

type(number)

returns

True

Reason

The type() function returns the object’s data type, not a Boolean value.

To perform a type check, use:

isinstance(number, int)

Best Practices #

  • Use type() while learning Python or debugging programs.
  • Prefer isinstance() when checking the type of an object in real-world applications.
  • Avoid unnecessary type checking if Python’s built-in operations already handle the required behavior.
  • Remember that input() always returns a string.

Key Points to Remember #

  • type() returns the data type of an object.
  • Every Python object has a data type.
  • type() can be used with literals, variables, and objects.
  • input() always returns a value of type str.
  • isinstance() is generally preferred over type() for checking an object’s type.
  • isinstance() can check against multiple data types using a tuple.
  • Type checking is useful for debugging, validation, and writing reliable programs.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *