Python Type Checking

Type checking is a process in programming where you verify or determine the data type of a variable. It involves examining the characteristics of a value to understand its nature, such as whether it represents an integer, a string, a boolean, or another data type. 

Python provides several instructions for type checking, allowing you to determine the data type of a variable or object. 


Using the type() instruction:

   - The type() instruction returns the type of an object.

   

- Example:



print(type(stu_name))  prints the data type of stu_name, which is a string (<class 'str'>).

print(type(stu_age))  prints the data type of stu_age, which is an integer (<class 'int'>).

print(type(stu_height)) prints the data type of stu_height, which is a float (<class 'float'>).

print(type(stu_approved))  prints the data type of stu_approved, which is a boolean (<class 'bool'>).

print(type(stu_hobbies)) prints the data type of stu_hobbies, which is a list (<class 'list'>).

print(type(stu_vehicles)) prints the data type of stu_vehicles, which is a tuple (<class 'tuple'>).

print(type(stu_friend_list)) prints the data type of stu_friend_list, which is a set (<class 'set'>).

print(type(stu_info)) prints the data type of stu_info, which is a dictionary (<class 'dict'>).


So, each print(type()) statement displays the data type of the respective variable, allowing you to verify the types of data stored in those variables.


type()  used for type checking essential for ensuring the correctness and robustness of your Python code, especially in situations where you need to handle different data types or when working with external data sources.

Popular posts from this blog

Python Variables

Operators in Python