Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Display types of several variables with type() - Python Program

Use type() to inspect the data type of any variable.

To display the types of several variables in Python using the type() function, you can declare the variables and then print the result oftype() for each one.

  • Variable Declaration:

Different variables are assigned values of various built-in Python data types (integer, float, string, boolean, list, tuple, dictionary, set).

  • type() Function:

The type() function is called with each variable as an argument. This function returns the class type of the object the variable refers to.

  • print() Function and f-strings:

The print() function is used to display the output. f-strings (formatted string literals) are used for a concise way to embed the variable name and the result of type() directly into the output string. The output will show the class type, for example, <class 'int'> for an integer variable.

 

Type 1 Program

# Declare several variables with different data types integer_var = 10 float_var = 3.14 string_var = "Hello, Python!" boolean_var = True list_var = [1, 2, 3] tuple_var = (4, 5) dict_var = {"name": "Alice", "age": 30} set_var = {7, 8, 9} # Display the type of each variable print(f"Type of integer_var: {type(integer_var)}") print(f"Type of float_var: {type(float_var)}") print(f"Type of string_var: {type(string_var)}") print(f"Type of boolean_var: {type(boolean_var)}") print(f"Type of list_var: {type(list_var)}") print(f"Type of tuple_var: {type(tuple_var)}") print(f"Type of dict_var: {type(dict_var)}") print(f"Type of set_var: {type(set_var)}")

Output

 
OUTPUT :

Type of integer_var: <class 'int'>
Type of float_var: <class 'float'>
Type of string_var: <class 'str'>
Type of boolean_var: <class 'bool'>
Type of list_var: <class 'list'>
Type of tuple_var: <class 'tuple'>
Type of dict_var: <class 'dict'>
Type of set_var: <class 'set'>

 

Type 2 Program

# Displays the data types of various variables name = "Alice" age = 30 height = 5.6 is_student = True print(type(name)) print(type(age)) print(type(height)) print(type(is_student))

Output

 
OUTPUT :

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