Data Types in Python | Python Course | Dataplexa

Data Types in Python

Every piece of information in a Python program has a type. A number is different from a word. A true/false value is different from a list of items. Python uses data types to keep track of what kind of information each variable holds and what you are allowed to do with it.

Understanding data types is one of the most important foundations in Python. Once you know the types available and when to use each one, writing programs becomes much more natural and predictable.

How to Check a Data Type

Python has a built-in function called type() that tells you what kind of data a variable is holding. You can use this at any point in your code to check or confirm the type.

a = 50
b = 12.9
c = "Hello"

print(type(a))
print(type(b))
print(type(c))
<class 'int'> <class 'float'> <class 'str'>
  • int means it is a whole number.
  • float means it is a decimal number.
  • str means it is text (string).

Numbers in Python

Python has three numeric types. The two you will use most often are int for whole numbers and float for numbers with decimal points. The third type, complex, is used in advanced mathematics and science.

x = 10          # int — whole number
y = 12.75       # float — decimal number
z = 2 + 5j      # complex — advanced numeric type

print(x)
print(y)
print(z)
print(type(x))
print(type(y))
print(type(z))
10 12.75 (2+5j) <class 'int'> <class 'float'> <class 'complex'>
  • Use int for things you count — ages, quantities, scores, years.
  • Use float for things you measure — prices, distances, percentages.
  • complex numbers appear in scientific and engineering programs. You will not need them often as a beginner.

Text — the String Type

A string is simply text. Any time you store a word, a sentence, a name, or any collection of characters, you are working with a string. Strings must be written inside quotation marks — either single or double.

platform = "Dataplexa"
topic = "Python"

print(platform)
print(topic)
print(platform.upper())   # convert to uppercase
print(len(topic))         # count the characters
Dataplexa Python DATAPLEXA 6
  • upper() converts every letter to uppercase.
  • len() counts how many characters are in the string.
  • Strings are used for names, messages, addresses, labels, file paths — almost everything that involves text.

True or False — the Boolean Type

A boolean is one of the simplest data types. It can only hold one of two values: True or False. Booleans are used whenever a program needs to make a decision — is the user logged in? Is an item in stock? Is the password correct?

is_logged_in = True
has_access = False

print(is_logged_in)
print(has_access)
print(5 > 3)    # comparison returns True
print(10 < 2)   # comparison returns False
True False True False
  • Comparisons using >, <, == always produce a boolean result.
  • Booleans are the foundation of all decision-making in Python programs.
  • The values True and False must start with a capital letter in Python.

Lists — Storing Multiple Items

A list lets you store several values inside a single variable. The items are kept in order and you can add, remove, or change them at any time. Lists are one of the most commonly used data structures in Python.

fruits = ["apple", "banana", "mango"]

print(fruits)
print(fruits[1])         # second item — counting starts from 0

fruits.append("orange")  # add a new item to the end
print(fruits)
['apple', 'banana', 'mango'] banana ['apple', 'banana', 'mango', 'orange']
  • List items are numbered starting from 0, so fruits[0] is "apple" and fruits[1] is "banana".
  • append() adds a new item to the end of the list.
  • Lists keep their order. The first item you put in stays first.

Tuples — Fixed Collections

A tuple is similar to a list, but once you create it, you cannot change its contents. Tuples are used when the data should stay the same throughout the program — like GPS coordinates, RGB colour values, or a fixed set of settings.

coordinates = (10, 20)

print(coordinates)
print(coordinates[0])    # first item
(10, 20) 10
  • Tuples use round brackets () instead of square brackets.
  • You can read values from a tuple but cannot modify, add, or remove items.
  • Because tuples cannot change, they are slightly faster and safer than lists for fixed data.

Dictionaries — Key and Value Pairs

A dictionary stores information in pairs. Each piece of data has a key and a value. You use the key to find the value instantly, just like looking up a word in a real dictionary to find its meaning.

student = {
    "name": "Alex",
    "age": 21,
    "course": "Python"
}

print(student["name"])
print(student["course"])
Alex Python
  • Keys like "name" and "course" must be unique within the dictionary.
  • You access a value by writing the key inside square brackets.
  • Dictionaries are used everywhere — user profiles, API responses, configuration settings, and more.

Sets — Unique Values Only

A set stores a collection of values where every item must be unique. If you try to add the same value twice, Python quietly ignores the duplicate. Sets are useful when you need to remove duplicates from data or check whether a value exists in a collection.

nums = {10, 20, 30, 20, 10}

print(nums)
print(20 in nums)
print(99 in nums)
{10, 20, 30} True False
  • The duplicates 20 and 10 were automatically removed — only one copy of each is kept.
  • The in keyword checks whether a value exists in the set.
  • Sets are unordered, so the position of items can vary each time you print them.

Real World Example — Order Summary

Real programs use several data types at the same time. This example shows how a shopping order might be stored using different types working together.

item = "Bluetooth Speaker"   # str
price = 1499.99              # float
quantity = 2                 # int
in_stock = True              # bool

total = price * quantity

print("Item:", item)
print("Quantity:", quantity)
print("In Stock:", in_stock)
print("Total Price:", total)
Item: Bluetooth Speaker Quantity: 2 In Stock: True Total Price: 2999.98

Why Each Type Was Chosen

  • item is a string because product names are text.
  • price is a float because it has decimal places.
  • quantity is an int because you cannot buy half an item.
  • in_stock is a bool because it is either available or not.
  • total is calculated from numeric values and becomes a float automatically.

Data Types at a Glance

TypeExampleUsed For
intcount = 10Whole numbers
floatprice = 49.99Decimal numbers
complexz = 2 + 3jScientific computing
strname = "Dataplexa"Text values
boolis_valid = TrueTrue or False logic
listitems = [1, 2, 3]Ordered, changeable collection
tuplepoint = (10, 20)Ordered, fixed collection
dictuser = {"name":"Alex"}Key-value pairs
setnums = {1, 2, 3}Unique values only

Practice

Which built-in function tells you the data type of a variable?



Which data type can only hold True or False?



Which data type is ordered and can be changed after creation?



Which data type is ordered but cannot be changed after creation?



Which data type automatically removes duplicate values?



Quick Quiz

Which data type should you use for a price like 49.99?





Which type is best for storing a user profile with a name and age?





Which type is best when you need only unique values?





NEXT UP
Input and Output in Python
Learn how to display information using print() and how to receive data from a user with input() — the two most essential tools for building interactive programs.