Every value in Python has a type. When you write name = "Ada", Python sees a string. When you write score = 95, Python sees an integer. Understanding variable types is the starting point for writing code that behaves the way you expect it to, because the type of a value determines what operations you can perform on it.
If you have written even one line of Python, you have already used variable types. The number 42 is an int. The text "hello" is a str. The value True is a bool. Python assigns these types automatically when you create a variable, and the type travels with the value everywhere it goes. This tutorial walks through what that means, why it matters, and how to work with types in your own code.
What is a Variable Type?
A variable type is the classification Python gives to a piece of data. It tells Python what the data represents and what you can do with it. You can add two integers together, but you cannot add an integer to a string without converting one of them first. The type is what enforces those rules.
In Python, you never have to write a type declaration. There is no int x = 5; like you would see in Java or C. Instead, you just write x = 5, and Python figures out that 5 is an integer. This behavior is called dynamic typing -- the type is determined at runtime based on the value you assign, not by a declaration you write in advance.
age = 30 # int — a whole number
price = 19.99 # float — a decimal number
name = "Ada" # str — a text string
active = True # bool — True or False
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(name)) # <class 'str'>
print(type(active)) # <class 'bool'>
The type() function is your primary tool for inspecting types. It takes any value or variable as an argument and returns the class that value belongs to. Every single value in Python is an object, and every object has a type. There are no exceptions to this rule.
Because Python is dynamically typed, the same variable can hold different types over its lifetime. Writing x = 10 and then x = "ten" is perfectly valid. Python does not complain -- it simply updates the type. This flexibility is powerful, but it also means you need to keep track of what your variables hold.
A common misconception is that the variable itself has a type. In Python, the value has the type, not the variable name. The name x is just a label pointing to whatever object you last assigned to it. When you reassign x to a different value, the label moves to the new object, and the old object's type has not changed -- only the label moved.
Build the statement that checks the type of a variable called score and prints the result:
Python's Built-in Types at a Glance
Python ships with a set of built-in types that cover the data you will work with in nearly every program. You do not need to import anything to use them. They are available the moment Python starts running. Here is a tour of the ones you will encounter first as a beginner.
Numeric Types: int and float
Integers (int) represent whole numbers with no decimal point. Python integers have no fixed size limit -- they can grow as large as your system's memory allows. Floats (float) represent numbers with a decimal point and are stored as double-precision floating-point numbers, which means they are accurate to about 15 decimal places.
count = 42 # int
temperature = 98.6 # float
big_number = 10 ** 100 # int — Python handles this fine
# Mixing int and float produces a float
result = count + temperature
print(type(result)) # <class 'float'>
Text Type: str
Strings (str) hold sequences of characters. You can define them with single quotes, double quotes, or triple quotes for multi-line text. Strings are immutable, meaning you cannot change individual characters in place -- you create a new string instead.
greeting = "Hello, world"
language = 'Python'
bio = """This is a
multi-line string."""
print(type(greeting)) # <class 'str'>
print(len(greeting)) # 12
Boolean Type: bool
Booleans (bool) represent one of two values: True or False. They are the result of comparison operations and the backbone of conditional logic. In Python, bool is a subclass of int, where True equals 1 and False equals 0.
Collection Types: list, tuple, dict, and set
These types hold groups of values. A list is an ordered, mutable sequence. A tuple is an ordered, immutable sequence. A dict maps keys to values. A set holds unique, unordered elements. Each one is designed for a different use case, and choosing the right collection type for the job is a skill you will develop over time.
colors = ["red", "green", "blue"] # list
point = (10, 20) # tuple
user = {"name": "Ada", "age": 30} # dict
unique_ids = {101, 102, 103} # set
print(type(colors)) # <class 'list'>
print(type(point)) # <class 'tuple'>
print(type(user)) # <class 'dict'>
print(type(unique_ids)) # <class 'set'>
- Example
42,-7,0- Mutable?
- No (immutable)
- Use case
- Counting, indexing, whole-number math
- Example
3.14,-0.5,1.0- Mutable?
- No (immutable)
- Use case
- Measurements, scientific calculations, currency (with caution)
- Example
"hello",'Python'- Mutable?
- No (immutable)
- Use case
- Text, user input, file paths, messages
- Example
True,False- Mutable?
- No (immutable)
- Use case
- Conditional logic, flags, toggles
- Example
[1, 2, 3],["a", "b"]- Mutable?
- Yes (mutable)
- Use case
- Ordered collections that need to grow, shrink, or change
- Example
{"key": "value"}- Mutable?
- Yes (mutable)
- Use case
- Labeled data, lookups by key, structured records
This code tries to combine a user's name and age into a single message. One line has a type-related bug. Find it.
age to a string before concatenating. Change line 4 to message = name + " is " + str(age) + " years old". Python cannot concatenate a str and an int directly -- you must use str() to cast the integer to a string first, or use an f-string like f"{name} is {age} years old".
Checking and Converting Types
Knowing a variable's type is useful, but the real power comes from being able to check types in your code and convert between them when needed. Python gives you two main tools for checking types and a set of built-in functions for converting them.
type() vs isinstance()
You already know type() returns the exact class of a value. The related function isinstance() checks whether a value belongs to a given type and returns True or False. The key difference is that isinstance() also recognizes subclass relationships. Since bool is a subclass of int, isinstance(True, int) returns True, while type(True) == int returns False.
value = 3.14
# type() — returns the exact class
print(type(value)) # <class 'float'>
print(type(value) == float) # True
# isinstance() — checks class and subclasses
print(isinstance(value, float)) # True
print(isinstance(value, (int, float))) # True — checks multiple types
# Where they differ: bool is a subclass of int
print(type(True) == int) # False
print(isinstance(True, int)) # True
Use isinstance() when writing if checks in your code. It handles inheritance correctly and accepts a tuple of types, making it both safer and more flexible than comparing with type() directly.
Type Casting with Built-in Functions
When you need a value in a different type, Python's built-in casting functions handle the conversion. The four you will use often are int(), float(), str(), and bool(). Each one creates a new object of the target type from the input value.
# str to int
user_input = "42"
number = int(user_input)
print(number + 8) # 50
# int to float
whole = 7
decimal = float(whole)
print(decimal) # 7.0
# float to int (truncates, does not round)
pi = 3.99
truncated = int(pi)
print(truncated) # 3
# int to str
age = 25
label = "Age: " + str(age)
print(label) # Age: 25
# Truthiness — bool() on common values
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hello")) # True
print(bool([])) # False
print(bool([1, 2])) # True
Not every conversion is valid. Calling int("hello") raises a ValueError because Python cannot interpret the word "hello" as a number. Always validate input before casting, especially when working with data from users or external files.
How to Check and Identify Variable Types in Python
Follow these four steps to confidently check, identify, and convert variable types in any Python program.
-
Create a variable and assign a value
Start by creating a variable with a simple assignment. Write a name on the left side of the equals sign and a value on the right. Python will determine the type based on the value. For example,
score = 95creates anint, andname = "Ada"creates astr. -
Use type() to check the variable's type
Wrap the variable name inside the
type()function and print the result. Runningprint(type(score))will output<class 'int'>. This confirms the category of data the variable holds at that moment. -
Use isinstance() for conditional type checks
When you need a
TrueorFalseanswer about a variable's type, useisinstance(). For example,isinstance(score, int)returnsTrue. This function also supports checking against multiple types at once by passing a tuple, such asisinstance(score, (int, float)). -
Convert types with casting functions
When you need a value in a different type, use Python's built-in casting functions:
int(),float(),str(), orbool(). For example,str(95)converts the integer95into the string"95". Always ensure the value is compatible with the target type to avoid aValueError.
"In Python, everything is an object." — Python documentation, Data Model chapter
Python Learning Summary Points
- A variable type is the classification Python gives to a value -- it determines what operations are valid. Python assigns types automatically at runtime through dynamic typing, so you never write type declarations.
- The core built-in types you will use as a beginner are
int,float,str,bool,list,tuple,dict, andset. Usetype()to inspect them andisinstance()for conditional checks in your code. - Python provides casting functions like
int(),float(),str(), andbool()to convert between types. Not every conversion is valid, so always validate your data before casting to avoid runtime errors.
Variable types are one of the first things to understand when learning Python, and they stay relevant at every level of complexity. The patterns you build now -- checking types with type() and isinstance(), converting between types with casting functions, and understanding which operations each type supports -- will serve you in every Python project you write going forward.
Frequently Asked Questions
A variable type in Python is the category of data a variable holds at any given moment. Python determines the type automatically based on the value you assign. For example, assigning 42 creates an int, while assigning "hello" creates a str. You can check a variable's type at any time using the built-in type() function.
Use the built-in type() function. Pass the variable as an argument and it returns the class the value belongs to. For example, type(42) returns <class 'int'> and type("hello") returns <class 'str'>. For True/False checks against a specific type, use isinstance() instead.
Dynamic typing means Python determines a variable's type at runtime based on the value assigned to it, rather than requiring you to declare the type in advance. The same variable name can hold an integer in one line and a string in the next. Python will not raise an error for reassigning a variable to a different type.
Python's basic built-in types include int (whole numbers), float (decimal numbers), str (text strings), bool (True or False), list (ordered mutable sequences), tuple (ordered immutable sequences), dict (key-value mappings), set (unordered unique elements), and NoneType (the type of the None value).
Python provides built-in casting functions for type conversion. Use int() to convert to an integer, float() to convert to a decimal, str() to convert to a string, and bool() to convert to a boolean. Not every conversion is valid. For example, int("hello") will raise a ValueError because "hello" cannot be interpreted as a number.
type() returns the exact class of a value, while isinstance() checks whether a value belongs to a given type or any of its parent types. isinstance() is preferred in conditional logic because it supports inheritance and can check against multiple types at once using a tuple.
No. Unlike languages such as Java or C, Python does not require type declarations. You create a variable by assigning a value to it, and Python infers the type automatically. Python 3.5 and later support optional type hints for documentation and static analysis, but these do not enforce types at runtime.
Each data type is optimized for a specific kind of task. Integers handle counting and indexing. Floats handle measurements and calculations with decimals. Strings handle text. Lists handle ordered collections that change over time. Dictionaries handle labeled lookups. Using the right type makes your code clearer, faster, and less prone to bugs.
Yes. Because Python is dynamically typed, you can reassign a variable to a value of a different type at any time. For example, x = 10 makes x an int, and then x = "ten" makes x a str. This flexibility is powerful but can cause bugs if you lose track of what type a variable holds.
Python raises a TypeError when you try to perform an operation that is not supported for the given types. For example, adding a string and an integer with "hello" + 5 will raise TypeError: can only concatenate str (not "int") to str. You must convert one of the values to a compatible type first.