3.6. Type checking
Table of Contents
Why `type()` Is Useful
When you write Python programs, you often need to know what kind of data you are working with:
- Is this a number I can add?
- Is this text I should join together?
- Is this a list I should loop over?
Python can tell you the data type of any value or variable using the built‑in function type().
type() is especially helpful when:
- You’re learning and want to see what type something is.
- You’re debugging and something doesn’t behave as expected.
- You want to confirm that an input was converted to the right type.
The Basic Use of `type()`
The type() function takes one value (or variable) and returns its type.
General pattern:
$$\text{result} = \text{type}(\text{value\_or\_variable})$$
Example with values directly:
print(type(5)) # integer
print(type(3.14)) # float
print(type("hello")) # string
print(type(True)) # bool (boolean)Example with variables:
age = 25
price = 19.99
name = "Alice"
is_member = True
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(is_member)) # <class 'bool'>The output is usually shown as something like:
<class 'int'><class 'float'><class 'str'><class 'bool'>
For now, you can read these as “this value is of type int/float/str/bool”.
Using `type()` to Explore Different Types
As you meet new types in later chapters (lists, dictionaries, etc.), you can use type() to see what they are:
numbers = [1, 2, 3]
person = {"name": "Bob", "age": 30}
unique_numbers = {1, 2, 3}
point = (10, 20)
print(type(numbers)) # <class 'list'>
print(type(person)) # <class 'dict'>
print(type(unique_numbers)) # <class 'set'>
print(type(point)) # <class 'tuple'>This is a quick way to become familiar with new data structures: create one, then check its type.
Checking Types After Conversions
When you convert between types (like turning text into a number), type() helps confirm that the conversion worked.
Example with user input and conversion:
user_input = input("Enter a number: ") # always a string
print(type(user_input)) # <class 'str'>
number = int(user_input) # convert to int
print(type(number)) # <class 'int'>
This makes it clear that input() gives you a str, and after int(), you have an int.
Storing and Comparing Types
Because type() returns a value, you can:
- Store it in a variable.
- Compare it to another type.
Storing a type
x = 42
x_type = type(x)
print(x_type) # <class 'int'>Comparing types directly
You can compare the result of type() with a type like int, float, etc.
Pattern:
$$\text{type}(\text{value}) == \text{SomeType}$$
Example:
x = 3.14
if type(x) == float:
print("x is a float")
else:
print("x is not a float")For beginners, this kind of check is often enough when you really need it.
A Common Use: Debugging Surprises
If something in your program behaves strangely, checking the type often reveals the problem.
Example: adding numbers vs. joining strings
a = "10"
b = "20"
print(a + b) # What do you expect?
print(type(a), type(b))Output:
a + bgives1020(string concatenation)type(a)andtype(b)are both<class 'str'>
After inspecting the type, you know you need to convert:
a = int(a)
b = int(b)
print(a + b) # 30
print(type(a), type(b)) # <class 'int'> <class 'int'>
Using type() like this is a simple but powerful debugging step: “What type is this really?”
Printing Types Clearly
You’ll often want to print both the value and its type in a readable way.
Example:
value = "hello"
print("Value:", value, "| Type:", type(value))Sample outputs for different values:
x = 100
print("x:", x, "| type:", type(x)) # x: 100 | type: <class 'int'>
x = 100.0
print("x:", x, "| type:", type(x)) # x: 100.0 | type: <class 'float'>
x = "100"
print("x:", x, "| type:", type(x)) # x: 100 | type: <class 'str'>Seeing these side by side helps you understand both the value and its data type.
Using `type()` Interactively
In interactive mode (REPL) or a notebook, you can quickly experiment:
>>> type(1)
<class 'int'>
>>> type(1.0)
<class 'float'>
>>> type("1")
<class 'str'>
>>> type(True)
<class 'bool'>Try:
type([])type(())type({})type(set())
to see the types of different collection objects as you learn them later.
When to Use `type()` (and When Not To)
Use type() when:
- You are learning and want to inspect values.
- You are debugging and something doesn’t act as you expect.
- You want to verify that a conversion or operation gave you the type you intended.
In many cases, you don’t need to check types in your final code all the time—Python is designed so you can usually just use the values. But while you’re learning, type() is like an “X‑ray” that lets you see what’s really going on.
Small Practice Ideas
You can practice with type() using simple mini‑tasks:
- Create one variable of each basic type you know (
int,float,str,bool) and print its type. - Use
input()to read something, print its type, convert it to a number, and print the new type. - Make a small list (e.g.,
[1, "two", 3.0, False]) and usetype()on each element inside a loop (once you learn loops).
In all these cases, the goal is the same: get comfortable seeing and understanding what type your data is in Python.
Views: 0
Where to Move
Move chapter:
- ☰ 1. Chapter 1: What Is Programming?
- ☰ 1.1. What is a computer program?
- ☰ 1.2. What is Python?
- ☰ 1.3. Where is Python used?
- ☰ 1.4. How programming helps solve problems
- ☰ 1.5. Your first Python program
-
- ☰ 2. Chapter 2: Setting Up Python
- ☰ 2.1. Installing Python (Windows / macOS / Linux)
- ☰ 2.2. Using Python in the terminal
- ☰ 2.3. Using an IDE (IDLE / VS Code)
- ☰ 2.4. Running Python scripts
- ☰ 2.5. Python interactive mode
-
- ☰ 3. Chapter 3: Variables and Data Types
- ☰ 3.1. What is a variable?
- ☰ 3.2. Creating variables
- ☰ 3.3. Numbers
- ☰ 3.4. Test
- ☰ 3.5. Boolean values
- ☰ 3.6. Type checking
-
- ☰ 4. Chapter 4: Basic Operations
- ☰ 4.1. Arithmetic operations
- ☰ 4.2. Working with numbers
- ☰ 4.3. String operations
- ☰ 4.4. Combining text and variables
- ☰ 4.5. Comments in Python
-
- ☰ 5. Chapter 5: User Input and Output
- ☰ 5.1. Getting input from the user
- ☰ 5.2. Converting input to numbers
- ☰ 5.3. Formatting output
- ☰ 5.4. Writing interactive programs
-
- ☰ 6. Chapter 6: Conditions
- ☰ 6.1. Comparison operators
- ☰ 6.2. Logical operators
- ☰ 6.3. if, elif, else
- ☰ 6.4. Nested conditions
- ☰ 6.5. Common beginner mistakes
-
- ☰ 7. Chapter 7: Loops
- ☰ 7.1. Why loops are useful
- ☰ 7.2. for loops
- ☰ 7.3. while loops
- ☰ 7.4. Loop counters
- ☰ 7.5. break and continue
- ☰ 7.6. Loop exercises
-
- ☰ 8. Chapter 8: Data Collections
- ☰ 8.1. Lists
- ☰ 8.2. Tuples
- ☰ 8.3. Dictionaries
- ☰ 8.4. Sets (basics)
- ☰ 8.5. Choosing the right data structure
-
- ☰ 9. Chapter 9: Functions
- ☰ 9.1. Why functions matter
- ☰ 9.2. Defining functions
- ☰ 9.3. Parameters and arguments
- ☰ 9.4. Return values
- ☰ 9.5. Scope (local vs global)
- ☰ 9.6. Reusing code
-
- ☰ 10. Chapter 10: Working with Files
- ☰ 10.1. What is a file?
- ☰ 10.2. Reading text files
- ☰ 10.3. Writing and appending files
- ☰ 10.4. File paths
- ☰ 10.5. Simple file-based programs
-
- ☰ 11. Chapter 11: Errors and Debugging
- ☰ 11.1. Types of errors
- ☰ 11.2. Reading error messages
- ☰ 11.3. Common beginner errors
- ☰ 11.4. try and except
- ☰ 11.5. Debugging strategies
-
- ☰ 12. Chapter 12: Using Python Libraries
- ☰ 12.1. What is a library?
- ☰ 12.2. Importing modules
- ☰ 12.3. Standard libraries
- ☰ 12.3.1. math
- ☰ 12.3.2. random
- ☰ 12.3.3. datetime
-
- ☰ 12.4. Installing external libraries with pip
-
- ☰ 13. Chapter 13: Object-Oriented Programming
- ☰ 13.1. What is object-oriented programming?
- ☰ 13.2. Classes and objects
- ☰ 13.3. The `__init__` method
- ☰ 13.4. Attributes and methods
- ☰ 13.5. Encapsulation
- ☰ 13.6. Inheritance (basics)
- ☰ 13.7. Simple OOP examples
-
- ☰ 14. Chapter 14: Automation with Python
- ☰ 14.1. What is automation?
- ☰ 14.2. Automating simple tasks
- ☰ 14.3. Working with files and folders
- ☰ 14.4. Automating text processing
- ☰ 14.5. Scheduling scripts
- ☰ 14.6. Practical automation examples
-
- ☰ 15. Chapter 15: Web Development with Python
- ☰ 15.1. How the web works (basic overview)
- ☰ 15.2. Introduction to Flask
- ☰ 15.3. Creating a simple web server
- ☰ 15.4. Routing and templates
- ☰ 15.5. Handling user input (forms)
- ☰ 15.6. Very basic APIs
-
- ☰ 16. Chapter 16: Data Science with Python
- ☰ 16.1. What is data science?
- ☰ 16.2. Working with data
- ☰ 16.3. Introduction to NumPy
- ☰ 16.4. Introduction to pandas
- ☰ 16.5. Data visualization with matplotlib
- ☰ 16.6. Simple data analysis examples
-
- ☰ 17. Chapter 17: Mini Projects
- ☰ 17.1. Number guessing game
- ☰ 17.2. Simple calculator
- ☰ 17.3. To-do list
- ☰ 17.4. Password generator
- ☰ 17.5. Quiz application
- ☰ 17.6. Small web or data project
-
- ☰ 18. Chapter 18: Next Steps
- ☰ 18.1. How to practice Python
- ☰ 18.2. Writing clean and readable code
- ☰ 18.3. Choosing a specialization
- ☰ 18.3.1. Web development
- ☰ 18.3.2. Data science
- ☰ 18.3.3. Automation
- ☰ 18.3.4. Software development
-
- ☰ 18.4. Learning resources and communities
-
- ☰ 19. Appendix
- ☰ 19.1. Python syntax cheat sheet
- ☰ 19.2. Common errors and solutions
- ☰ 19.3. Glossary of terms
-
Comments
Please login to add a comment.
Don't have an account? Register now!