Nearby lessons

14 of 159

Python - Data Types

📌 What You Will Learn
  • Understand what data types are and why they matter
  • Learn all built-in Python data types
  • Know how to check the type of any variable
  • Understand dynamic typing in Python

Introduction

When you write Python code, you store values in variables. But here is an important question — how does the computer know if a value is a number, text, or something else?

The answer is: Data Types.

A data type tells Python what kind of value you are storing — is it a whole number, a decimal, text, or something else?

Key Point: In Python, you do not need to tell Python the data type. Python figures it out automatically! This is why Python is called a Dynamically Typed Language.

Example of Dynamic Typing

🐍Code Cell
1a = 10
2b = 10.5
3c = "Durga"
4d = True
5 
6print(type(a))
7print(type(b))
8print(type(c))
9print(type(d))
Output
No output captured.

Built-in Data Types in Python

Data Type Description Example
intInteger numbers10
floatDecimal numbers10.5
complexComplex numbers3+5j
boolBoolean valuesTrue
strText/String"Hello"
listOrdered mutable collection[1,2,3]
tupleOrdered immutable collection(1,2,3)
setUnordered unique values{1,2,3}
dictKey-value pairs{"a":1}
bytesImmutable binary databytes([1,2])
bytearrayMutable binary databytearray([1,2])
rangeSequence of numbersrange(5)
frozensetImmutable setfrozenset({1,2})
NoneNo valueNone

Important Built-in Functions

Python provides several built-in functions related to data types.

1. type() Function

The type() function returns the data type of a variable.

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

2. id() Function

The id() function returns the identity (memory reference) of an object.

Note: The returned value may be different on different systems.

🐍Code Cell
1a = 10
2 
3print(id(a))
Output
140723456789

Everything is an Object

In Python, everything is an object — even numbers and strings.

This means every value in Python has:

  • An identity (memory address)
  • A data type
  • A value
Key Point: You can use the id() function to see the memory address of any value, and type() to see its data type.

Classification of Python Data Types

Python data types can be grouped into the following categories.

1. Numeric Data Types

Numeric data types store numbers.

  • int
  • float
  • complex
🐍Code Cell
1a = 10
2b = 10.5
3c = 3 + 5j
Output
Numeric values created successfully.

2. Boolean Data Type

The bool data type stores True or False.

🐍Code Cell
1is_active = True
2 
3print(is_active)
Output
True

3. String Data Type

The str data type stores text.

🐍Code Cell
1name = "Durga"
2 
3print(name)
Output
Durga

4. Sequence Data Types

Sequence data types store multiple values in order.

  • list
  • tuple
  • range
🐍Code Cell
1numbers = [10, 20, 30]
2 
3print(numbers)
Output
[10, 20, 30]

5. Set Data Types

Set data types store unique values.

  • set
  • frozenset
🐍Code Cell
1s = {10, 20, 30}
2 
3print(s)
Output
{10, 20, 30}

6. Mapping Data Type

The dict data type stores data as key-value pairs.

🐍Code Cell
1student = {
2 "name": "Rahul",
3 "marks": 90
4}
5 
6print(student)
Output
{'name': 'Rahul', 'marks': 90}

7. Binary Data Types

Binary data types store binary values.

  • bytes
  • bytearray
🐍Code Cell
1x = [10, 20, 30]
2 
3b = bytes(x)
4 
5print(b)
Output
b'\n\x14\x1e'

8. None Data Type

None represents the absence of a value.

🐍Code Cell
1x = None
2 
3print(x)
Output
None
📝 Key Takeaways
  • Python has built-in data types: int, float, str, bool, list, tuple, dict, set
  • You do not need to declare the data type — Python figures it out automatically
  • Use the type() function to check what type of data a variable holds
  • Python is called a Dynamically Typed Language
  • Each data type has its own rules for how values are stored and used

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8