|
| Home |
|---|
Python data types define the kind of value that a variable contains. A data type tells Python what a value represents and what operations can be performed with that value.
For example, an integer can be used for counting and mathematical calculations, a string can store text, a Boolean can represent a true-or-false condition, and a list can store multiple values. Understanding Python data types is one of the most important foundations for learning Python programming.
Python is a dynamically typed language. This means that you do not normally have to declare the data type of a variable before using it. Python determines the type automatically from the value assigned to the variable.
For example, Python automatically recognizes 25 as an
integer, 3.14 as a floating-point number and
"Python" as a string.
age = 25
price = 19.99
name = "Alice"
is_student = True
print(type(age))
print(type(price))
print(type(name))
print(type(is_student))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Python provides several built-in data types that are used to represent numbers, text, Boolean values, collections and other kinds of data.
A data type specifies the kind of data represented by a value. Data types are important because different types support different operations.
For example, Python treats these two values differently:
10
"10"
The first value is an integer, while the second value is a string. Although they look similar, Python does not treat them as the same type.
number = 10
text = "10"
print(type(number))
print(type(text))
<class 'int'>
<class 'str'>
Python has several important built-in data types. They can be grouped into categories according to the kind of data they represent.
| Category | Data Type | Example |
|---|---|---|
| Numeric | int |
25 |
| Numeric | float |
3.14 |
| Numeric | complex |
2 + 3j |
| Text | str |
"Python" |
| Boolean | bool |
True |
| Sequence | list |
[1, 2, 3] |
| Sequence | tuple |
(1, 2, 3) |
| Sequence | range |
range(5) |
| Mapping | dict |
{"name": "Alice"} |
| Set | set |
{1, 2, 3} |
| Set | frozenset |
frozenset({1, 2, 3}) |
| Binary | bytes |
b"Python" |
| Binary | bytearray |
bytearray(5) |
| Binary | memoryview |
memoryview(data) |
| Special | NoneType |
None |
For beginners, the most important types to learn first are
int, float, str, bool,
list, tuple, set and
dict. These types appear frequently in Python programs.
Python provides the built-in type() function for checking
the data type of a value or variable.
x = 10
print(type(x))
<class 'int'>
The output shows that x contains an integer.
You can also check the type of an expression:
result = 10 / 2
print(result)
print(type(result))
5.0
<class 'float'>
Notice that normal division using / produces a
floating-point result, even when both operands are integers.
The integer data type, written as int,
represents whole numbers. Integers can be positive, negative or zero.
Examples include:
10100-250Python integers are commonly used for counting, indexing, ages, quantities, scores and mathematical calculations.
age = 25
year = 2026
temperature = -5
students = 30
print(age)
print(year)
print(temperature)
print(students)
25
2026
-5
30
a = 10
b = 5
total = a + b
print(total)
15
Integers can be used with arithmetic operators such as
+, -, *, //,
% and **.
The float data type represents floating-point numbers, which commonly contain decimal values.
Examples include:
3.1419.995.75-2.5Floats are commonly used for measurements, percentages, scientific calculations, averages and other values that may contain decimal fractions.
price = 19.99
height = 5.8
temperature = -2.5
print(price)
print(height)
print(temperature)
19.99
5.8
-2.5
a = 5.5
b = 2.5
result = a + b
print(result)
8.0
Python also supports complex numbers. A complex number contains a real part and an imaginary part.
The imaginary part uses the letter j in Python.
number = 2 + 3j
print(number)
print(type(number))
(2+3j)
<class 'complex'>
Complex numbers are mainly used in scientific computing, engineering, mathematics and specialized numerical applications. Most beginners do not need complex numbers immediately, but it is useful to know that Python supports them as a built-in numeric type.
The string data type, written as str,
represents text. Strings can contain letters, numbers, spaces and
special characters.
Python strings can be written using single quotes, double quotes or triple quotes for longer strings.
name = "Alice"
course = "Python Programming"
city = "London"
print(name)
print(course)
print(city)
Alice
Python Programming
London
A number written inside quotation marks is a string rather than a numeric value.
number = "100"
print(number)
print(type(number))
100
<class 'str'>
This distinction is important when working with user input because
input() returns a string by default.
Strings can be joined together using the + operator. This
process is called string concatenation.
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)
Alice Smith
The built-in len() function can be used to find the number
of characters in a string.
message = "Python"
print(len(message))
6
The Boolean data type, written as bool,
represents one of two logical values: True or
False.
Boolean values are particularly important for conditions, comparisons, decision-making and program control.
is_student = True
is_logged_in = False
print(is_student)
print(is_logged_in)
True
False
Comparison expressions produce Boolean results.
x = 10
y = 5
print(x > y)
print(x == y)
print(x < y)
True
False
False
The expression x > y is True because 10 is
greater than 5.
Boolean expressions are frequently used with if
statements.
age = 20
if age >= 18:
print("Adult")
Adult
A list is a collection used to store multiple values in a single variable. Lists are ordered and mutable, which means their contents can be changed after the list is created.
Lists can contain numbers, strings, Boolean values or even other collections.
fruits = ["apple", "banana", "cherry"]
print(fruits)
['apple', 'banana', 'cherry']
List items can be accessed using an index. Python uses zero-based
indexing, so the first item has index 0.
fruits = ["apple", "banana", "cherry"]
print(fruits[0])
print(fruits[1])
apple
banana
Because lists are mutable, an existing item can be changed.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits)
['apple', 'orange', 'cherry']
Lists are widely used when a program needs to store and process a collection of values.
A tuple is an ordered collection of values. Unlike a list, a tuple is immutable, which means its items cannot normally be changed after the tuple has been created.
coordinates = (10, 20)
print(coordinates)
print(type(coordinates))
(10, 20)
<class 'tuple'>
Tuples are useful when you want to group related values together and do not want those values to be modified.
colors = ("red", "green", "blue")
print(colors[0])
print(colors[2])
red
blue
A set is a collection of unique values. Sets are particularly useful when duplicate values need to be removed or when you need to perform mathematical set operations.
numbers = {1, 2, 3, 3, 4}
print(numbers)
{1, 2, 3, 4}
The duplicate value 3 appears only once because a set
stores unique elements.
numbers = {10, 20, 30}
print(20 in numbers)
print(50 in numbers)
True
False
Sets are useful when membership testing and uniqueness are important.
A dictionary, written as dict, stores data
as key-value pairs. Dictionaries are useful when information needs to
be associated with meaningful keys.
student = {
"name": "Alice",
"age": 20,
"score": 85
}
print(student)
{'name': 'Alice', 'age': 20, 'score': 85}
Dictionary values can be accessed using their keys.
student = {
"name": "Alice",
"age": 20
}
print(student["name"])
print(student["age"])
Alice
20
Dictionaries are commonly used for structured information such as student records, customer information, product details and configuration settings.
The range type represents a sequence of numbers and is
commonly used with for loops.
numbers = range(5)
for number in numbers:
print(number)
0
1
2
3
4
The expression range(5) produces numbers starting at
zero and stopping before five.
Python has a special value called None. It represents the
absence of a value or the lack of a meaningful result.
result = None
print(result)
print(type(result))
None
<class 'NoneType'>
None is commonly used when a variable has no value yet,
when a function does not return a meaningful result, or when a program
needs to represent a missing value.
The bytes data type represents a sequence of bytes. It is
commonly used when working with binary data, files, network
communication and certain encoding operations.
data = b"Python"
print(data)
print(type(data))
b'Python'
<class 'bytes'>
Most beginners will work with strings much more frequently than bytes, but the bytes type becomes important when working with binary data and network applications.
Another important concept when learning Python data types is the difference between mutable and immutable objects.
A mutable object can be changed after it is created. An immutable object cannot be changed after creation.
| Data Type | Generally Mutable? |
|---|---|
list |
Yes |
dict |
Yes |
set |
Yes |
tuple |
No |
str |
No |
int |
No |
float |
No |
bool |
No |
numbers = [1, 2, 3]
numbers[0] = 100
print(numbers)
[100, 2, 3]
The list can be changed after it is created, so lists are mutable.
Strings cannot be changed in place. Operations on strings create new string values.
name = "Alice"
name = name + " Smith"
print(name)
Alice Smith
The original string is not modified in place. A new string value is
assigned to name.
Type conversion, also called type casting, means converting a value from one data type to another.
Python provides several built-in functions for converting values,
including int(), float(), str()
and bool().
number = "25"
number = int(number)
print(number)
print(type(number))
25
<class 'int'>
price = "19.99"
price = float(price)
print(price)
print(type(price))
19.99
<class 'float'>
age = 25
text = str(age)
print(text)
print(type(text))
25
<class 'str'>
The input() function is especially important when learning
about Python data types because it always returns the user's input as a
string.
age = input("Enter your age: ")
print(type(age))
Enter your age: 20
<class 'str'>
If the value is needed as an integer, it must be converted.
age = int(input("Enter your age: "))
print(type(age))
Enter your age: 20
<class 'int'>
Python data types determine what operations can be performed on values. Using the correct data type is therefore important when designing a program.
For example, mathematical calculations normally require numeric types, while names and messages are represented using strings.
price = 100
quantity = 3
total = price * quantity
print(total)
300
Here, price and quantity are integers, allowing
Python to perform numerical multiplication.
Using incompatible data types can produce errors. A common beginner mistake is trying to add a number to a string.
age = 20
message = "Age: "
print(message + age)
This produces a TypeError because Python cannot directly
concatenate a string and an integer using the + operator.
One solution is to convert the number into a string:
age = 20
message = "Age: "
print(message + str(age))
Age: 20
Choosing an appropriate data type makes Python programs easier to design, understand and maintain.
| If you need to store... | Use |
|---|---|
| A whole number | int |
| A decimal number | float |
| Text | str |
| True or false | bool |
| An ordered collection that can change | list |
| An ordered collection that should not change | tuple |
| Unique values | set |
| Key-value information | dict |
| A missing or absent value | None |
A real Python program will often use several data types at the same time. For example, a student record can contain a name, age, score and enrollment status.
name = "Alice"
age = 20
score = 87.5
is_student = True
print("Name:", name)
print("Age:", age)
print("Score:", score)
print("Student:", is_student)
Name: Alice
Age: 20
Score: 87.5
Student: True
This example uses four different Python data types:
str, int, float and
bool.
10 with the string
"10".
input() returns a string.
int() to convert text that does not represent a
valid integer.
Python variables do not have to be permanently associated with one data type. A variable can refer to an object of one type and later be assigned an object of another type.
value = 10
print(type(value))
value = "Python"
print(type(value))
<class 'int'>
<class 'str'>
This behavior is one of the characteristics of Python's dynamic typing.
Python data types define the kind of value represented by an object.
Common Python data types include int, float,
str, bool, list,
tuple, set and dict.
For beginners, four fundamental Python data types are integers
(int), floating-point numbers (float),
strings (str) and Boolean values (bool).
Python also provides many other built-in data types.
Use the built-in type() function.
x = 10
print(type(x))
An int represents a whole number such as
10, while a float represents a
floating-point number such as 10.5.
A string represents text and is written inside quotation marks, while an integer represents a whole number.
number = 10
text = "10"
The first value is an int and the second is a
str.
The input() function returns a string. If you need a
number, convert the result using int() or
float().
None is a special Python value used to represent the
absence of a value or a missing result. Its data type is
NoneType.
Both lists and tuples are ordered collections. A list is mutable, so its contents can be changed. A tuple is immutable, so its contents cannot normally be changed after it has been created.
A dictionary stores information as key-value pairs. Dictionaries are useful when values need to be associated with meaningful keys, such as a student's name, age and score.
A set is a collection of unique values. It is useful when duplicate values need to be removed or when membership and set operations are required.
Python data types determine the kind of values used by a Python program. Python automatically determines the type of an object when a value is assigned, making Python a dynamically typed language.
| Data Type | Purpose | Example |
|---|---|---|
int |
Whole numbers | 25 |
float |
Decimal numbers | 3.14 |
complex |
Complex numbers | 2 + 3j |
str |
Text | "Python" |
bool |
True or false values | True |
list |
Ordered mutable collection | [1, 2, 3] |
tuple |
Ordered immutable collection | (1, 2, 3) |
set |
Collection of unique values | {1, 2, 3} |
dict |
Key-value collection | {"name": "Alice"} |
range |
Sequence of numbers | range(5) |
NoneType |
Represents no value | None |
bytes |
Binary data | b"Python" |
Learning how Python data types work is essential before moving on to more advanced topics such as lists, dictionaries, functions, loops, object-oriented programming and data processing.
Continue learning Python with these related tutorials:
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net