Nearby lessons

23 of 159

Python - Data Types Summary

📌 What You Will Learn
  • Identify the built-in data types available in Python
  • Explain the purpose of each built-in data type
  • Distinguish mutable data types from immutable data types
  • Run examples that create values of every data type
  • Use the type() function to check the data type of a value

Introduction

Python provides several built-in data types to store different kinds of data.

Each data type has its own purpose and characteristics.

Some data types are immutable, while others are mutable.

Summary of Python Data Types

Data Type Description Mutable / Immutable
int Stores whole numbers. Immutable
float Stores decimal numbers. Immutable
complex Stores complex numbers. Immutable
bool Stores True or False. Immutable
str Stores text (sequence of characters). Immutable
bytes Stores byte values (0 to 255). Immutable
bytearray Stores byte values (0 to 255). Mutable
range Represents a sequence of numbers. Immutable
list Stores an ordered collection of objects. Mutable
tuple Stores an ordered collection of objects. Immutable
set Stores an unordered collection of unique values. Mutable
frozenset Immutable version of a set. Immutable
dict Stores data as key-value pairs. Mutable
None Represents no value or nothing. Immutable

Examples of All Data Types

The following examples show how each data type is created in Python.

Example - int

🐍Code Cell
1a = 10
2 
3print(type(a))
Output
No output captured.

Example - float

🐍Code Cell
1b = 10.5
2 
3print(type(b))
Output
No output captured.

Example - complex

🐍Code Cell
1c = 10 + 5j
2 
3print(type(c))
4print(c.real)
5print(c.imag)
Output
10.0
5.0

Example - bool

🐍Code Cell
1flag = True
2 
3print(type(flag))
4 
5flag = False
6 
7print(type(flag))
Output
No output captured.

Example - str

🐍Code Cell
1s = 'durga'
2 
3print(type(s))
4 
5s = "durga"
6 
7print(type(s))
8 
9s = '''Durga Software Solutions
10Ameerpet'''
11 
12print(type(s))
Output
No output captured.

Example - bytes

🐍Code Cell
1data = [1, 2, 3, 4]
2 
3b = bytes(data)
4 
5print(type(b))
Output
No output captured.

Example - bytearray

🐍Code Cell
1data = [10, 20, 30]
2 
3ba = bytearray(data)
4 
5print(type(ba))
Output
No output captured.

Example - range

🐍Code Cell
1r = range(10)
2 
3r1 = range(0, 10)
4 
5r2 = range(0, 10, 2)
6 
7print(type(r))
Output
No output captured.

Example - list

🐍Code Cell
1l = [10, 11, 12, 13, 14, 15]
2 
3print(type(l))
Output
No output captured.

Example - tuple

🐍Code Cell
1t = (1, 2, 3, 4, 5)
2 
3print(type(t))
Output
No output captured.

Example - set

🐍Code Cell
1s = {1, 2, 3, 4, 5, 6}
2 
3print(type(s))
Output
No output captured.

Example - frozenset

🐍Code Cell
1fs = frozenset({10, 20, 30})
2 
3print(type(fs))
Output
No output captured.

Example - dict

🐍Code Cell
1d = {
2 101: "durga",
3 102: "ravi"
4}
5 
6print(type(d))
Output
No output captured.

Example - None

🐍Code Cell
1x = None
2 
3print(type(x))
Output
No output captured.

Mutable and Immutable Data Types

Immutable Data Types Mutable Data Types
int bytearray
float list
complex set
bool dict
str -
bytes -
range -
tuple -
frozenset -
None -

Important Notes

  • Python provides several built-in data types for different kinds of data.
  • Choose the correct data type based on your requirement.
  • Immutable objects cannot be modified after creation.
  • Mutable objects can be modified after creation.
  • Understanding data types is the foundation of Python programming.

Quick Summary

Data Type Purpose Mutable?
intWhole numbersNo
floatDecimal numbersNo
complexComplex numbersNo
boolTrue / FalseNo
strTextNo
bytesByte valuesNo
bytearrayMutable byte valuesYes
rangeNumber sequenceNo
listOrdered collectionYes
tupleRead-only collectionNo
setUnique valuesYes
frozensetRead-only setNo
dictKey-value pairsYes
NoneNo valueNo
📝 Key Takeaways
  • Python provides several built-in data types for storing different kinds of data
  • int, float, complex, bool, str, bytes, range, tuple, frozenset, and None are immutable
  • list, bytearray, set, and dict are mutable data types
  • The type() function returns the class of a value
  • Each data type has its own purpose and characteristics

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8