Lecture
How to Check Variable and Value Types
The type() function is used to check the type of a variable or value.
How to Use the type() Function
The type() function returns the data type of a variable or value passed inside the parentheses.
Using the type() Function
number = 10 # Assign integer 10 to variable number print(type(number)) # <class 'int'> : Integer type text = "Python Programming" # Assign string to variable text print(type(text)) # <class 'str'> : String type
Checking Types
The type() function works with both built-in and user-defined data types in Python.
Checking Various Data Types
is_active = True print(type(is_active)) # <class 'bool'> : Boolean type my_list = [1, 2, 3] print(type(my_list)) # <class 'list'> : List type my_dict = {"key": "value"} print(type(my_dict)) # <class 'dict'> : Dictionary type
Previous lessonHow to Represent Text, Numbers, and True/False in PythonNext lessonWhat to Watch Out for When Creating String Data?
Lessons in this chapter · Data Types in Pythons
- 1. Data Types Representing Kinds of Data
- 2. How to Represent Text, Numbers, and True/False in Python
- 3. How to Check Variable and Value Types
- 4. What to Watch Out for When Creating String Data?
- 5. Understanding and Using Escape Characters
- 6. How to Create Multiline Strings?
- 7. Multiple-choice quiz
- 8. Concatenating Strings with the `+` Operator
- 9. Using the (*) Operator for String Repetition
- 10. Selecting Specific Parts of a String Using Indexing
- 11. How to Select Specific Ranges from a String
- 12. Handling Exceptions with try and except
- 13. String IndexError(index out of range) Exception
- 14. Python String Indexing Coding Quiz
- 15. Multiple-choice quiz
- 16. Fill-in-the-blank quiz
Quiz
0 / 1
Which of the following is a correct description of the type() function?
This function prints a variable or value.
This function deletes a variable or value.
This function checks the data type of a variable or value.
This function sorts a variable or value.
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help