Python Data Types Explained: Full Beginner Guide with Examples

Data Types in Python

🐍 Data Types in Python

int · float · str · bool · list · tuple · set · dict · None · range · complex

Introduction
What are Data Types?

Data types define the kind of data a variable can store in a program. Python has several built-in data types that are used to store different kinds of values such as numbers, text, collections, and more. Python automatically detects the data type — you do not need to declare it manually.

Data TypeCategoryExample
intNumericx = 10
floatNumericx = 3.14
complexNumericx = 2+3j
strTextx = "Hello"
boolBooleanx = True
listCollectionx = [1, 2, 3]
tupleCollectionx = (1, 2, 3)
setCollectionx = {1, 2, 3}
dictMappingx = {"a": 1}
rangeSequencex = range(5)
NoneNone Typex = None
πŸ’‘ Use type(x) to check the data type of any variable — e.g. print(type(x))
Data Type 01
Integer (int)

Integer is a whole number — positive, negative, or zero — without any decimal point. In Python, there is no size limit for integers. They are one of the most commonly used data types in programming.

Stores whole numbers only
Can be positive, negative, or zero
No decimal point allowed
Example
python
a = 10

b = -25

c = 0

print(a, b, c)       # 10 -25 0

print(type(a))       # <class 'int'>
πŸ“Œ You can do all math operations on integers: + - * / // % **
Data Type 02
Float (float)

Float is a number that contains a decimal point. It is used when more precision is needed, such as in scientific calculations or prices. Float values are accurate up to about 15 decimal places.

Contains a decimal point
Used for precise calculations
Can be positive or negative
Example
python
x = 3.14

y = -9.81

z = 2.0

print(x, y, z)       # 3.14 -9.81 2.0

print(type(x))       # <class 'float'>
πŸ’‘ When you divide two integers in Python 3, the result is always a float — e.g. 5 / 2 = 2.5
Data Type 03
Complex (complex)

Complex numbers have two parts — a real part and an imaginary part. The imaginary part is written with the letter j in Python. Complex numbers are mainly used in engineering and scientific computing.

Written as: real + imaginaryj
Used in mathematics and engineering
Example
python
c = 2 + 3j

print(c)             # (2+3j)

print(c.real)        # 2.0

print(c.imag)        # 3.0

print(type(c))       # <class 'complex'>
πŸ“Œ Use c.real to get the real part and c.imag to get the imaginary part.
Data Type 04
String (str)

A string is a sequence of characters enclosed inside single quotes, double quotes, or triple quotes. Strings are used to store text data. Each character in a string has an index starting from 0.

Stores text / characters
Index starts from 0
Strings are immutable — cannot be changed
Example
python
name = "Ali"

city = 'Karachi'

print(name[0])       # A

print(len(name))     # 3

print(name.upper())  # ALI

print(name + " " + city) # Ali Karachi
String Methods
python
s = "hello world"

print(s.upper())      # HELLO WORLD

print(s.capitalize()) # Hello world

print(s.replace("hello", "hi")) # hi world

print(s.split(" "))    # ['hello', 'world']
πŸ’‘ Use f-strings to insert variables into strings: f"My name is {name}"
Data Type 05
Boolean (bool)

Boolean data type has only two possible values — True or False. Booleans are mainly used in conditions and comparisons. In Python, True equals 1 and False equals 0 in numeric operations.

Only two values: True or False
Used in if conditions and loops
True = 1 and False = 0 in math
Example
python
x = True

y = False

print(type(x))       # <class 'bool'>

print(5 > 3)          # True

print(5 == 3)         # False

print(True + True)   # 2 (1+1)
πŸ“Œ Empty values like 0, "", [], None are treated as False in Python.
Data Type 06
List (list)

A list is an ordered collection used to store multiple items in a single variable. Lists are created using square brackets [ ]. They are mutable — meaning items can be added, removed, or changed after creation.

Ordered — items stay in the same position
Mutable — can be changed after creation
Allows duplicate values
Index starts from 0
Example
python
fruits = ["apple", "banana", "mango"]

print(fruits[0])           # apple

fruits.append("grape")   # Add

fruits.remove("banana")  # Remove

fruits[0] = "orange"      # Update

print(len(fruits))        # Length
πŸ’‘ Lists can store mixed data types: x = [1, "hello", 3.14, True]
Data Type 07
Tuple (tuple)

A tuple is similar to a list but it is immutable — its values cannot be changed after creation. Tuples are created using round brackets ( ). They are faster than lists and used when data should not be modified.

Ordered — items stay in position
Immutable — cannot be changed
Allows duplicate values
Faster than lists
Example
python
colors = ("red", "green", "blue")

print(colors[0])     # red

print(len(colors))   # 3

print(type(colors))  # <class 'tuple'>

# This will cause an ERROR:

# colors[0] = "yellow"  # TypeError!
πŸ“Œ Use tuples when data should stay constant and should not be changed — like days of the week.
Data Type 08
Set (set)

A set is an unordered collection with no duplicate values. Sets are created using curly braces { }. Since sets are unordered, you cannot access items by index. Sets are useful for removing duplicates and performing math set operations.

Unordered — no fixed position
No duplicate values allowed
Cannot access by index
Example
python
nums = {1, 2, 3, 2, 1}

print(nums)            # {1, 2, 3} — no duplicates

a = {1, 2, 3}

b = {3, 4, 5}

print(a | b)           # {1,2,3,4,5} Union

print(a & b)           # {3}       Intersection
πŸ’‘ To create an empty set, use set() — NOT { } because {} creates an empty dictionary.
Data Type 09
Dictionary (dict)

A dictionary stores data as key-value pairs. Each key must be unique and is used to access its value. Dictionaries are created using curly braces { } with a colon : between key and value. They are mutable and unordered.

Key-value pair structure
Keys must be unique
Mutable — values can be changed
Example
python
student = {

    "name":  "Ali",

    "age":   18,

    "grade": "A"

}

print(student["name"])        # Ali

student["age"] = 20          # Update

print(student.keys())        # all keys

print(student.values())      # all values
πŸ’‘ Use dict.get("key") to safely access values — it returns None instead of an error if the key is missing.
Data Type 10
Range (range)

The range type represents a sequence of numbers. It is mostly used in for loops to repeat a block of code a specific number of times. Range is memory-efficient because it does not store all values at once.

Generates a sequence of numbers
Used mostly in for loops
End value is NOT included
Example
python
# range(stop)

for i in range(5):

    print(i)           # 0 1 2 3 4

# range(start, stop, step)

for i in range(1, 10, 2):

    print(i)           # 1 3 5 7 9
πŸ“Œ range(5) gives 0,1,2,3,4 — the stop number (5) is never included.
Data Type 11
None (NoneType)

None represents the absence of a value — it means "nothing" or "empty". It is Python's equivalent of NULL in other languages. None is often used as a default value for variables or to indicate that a function returned nothing.

Represents no value / empty
Used as a placeholder or default
None is not the same as 0 or False
Example
python
x = None

print(x)              # None

print(type(x))        # <class 'NoneType'>

if x is None:

    print("No value assigned")
πŸ’‘ Always use x is None to check for None — not x == None.
Section 12
Type Conversion

Type conversion means changing one data type into another. Python provides built-in functions for this. There are two kinds — implicit (automatic) and explicit (manual using functions).

FunctionConverts ToExample
int()Integerint("5") → 5
float()Floatfloat(3) → 3.0
str()Stringstr(10) → "10"
bool()Booleanbool(0) → False
list()Listlist((1,2)) → [1,2]
tuple()Tupletuple([1,2]) → (1,2)
set()Setset([1,1,2]) → {1,2}
Example
python
x = "10"             # str

y = int(x)           # convert to int

print(y + 5)          # 15

a = 3.99

print(int(a))          # 3 (decimal removed)
⚠️ int("hello") will raise an error — you can only convert numbers stored as strings.
Final Summary
All Data Types at a Glance
TypeMutable?Ordered?Duplicates?
intNo
floatNo
strNoYesYes
boolNo
listYesYesYes
tupleNoYesYes
setYesNoNo
dictYesYes*No (keys)
🌟 Key Rule: Use list for ordered changeable data, tuple for fixed data, set for unique items, and dict for key-value data.
Python Data Types Beginner Guide
Python Data Types — The Foundation of Every Program.

Comments