Nearby lessons

14 of 108

Python - Data Types

Introduction

A Data Type represents the type of value stored in a variable.

In Python, you do not need to specify the data type manually.

Python automatically determines the data type based on the assigned value.

Because of this feature, Python is called a Dynamically Typed Language.

Example of Dynamic Typing

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

How Python Assigns Data Types

Variable Value Data Type
a10int
b10.5float
c"Durga"str
dTruebool

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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
140723456789

3. print() Function

The print() function displays output on the screen.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Python

Everything is an Object

In Python, everything is treated as an object.

Examples include:

  • Variables
  • Functions
  • Strings
  • Lists
  • Numbers

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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Numeric values created successfully.

2. Boolean Data Type

The bool data type stores True or False.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

3. String Data Type

The str data type stores text.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Durga

4. Sequence Data Types

Sequence data types store multiple values in order.

  • list
  • tuple
  • range
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
[10, 20, 30]

5. Set Data Types

Set data types store unique values.

  • set
  • frozenset
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{10, 20, 30}

6. Mapping Data Type

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
{'name': 'Rahul', 'marks': 90}

7. Binary Data Types

Binary data types store binary values.

  • bytes
  • bytearray
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
b'\n\x14\x1e'

8. None Data Type

None represents the absence of a value.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
None

Dynamic Typing

Python automatically changes the data type based on the assigned value.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
No output captured.

Advantages of Dynamic Typing

  • Easy to write programs.
  • Less code is required.
  • Provides flexibility.
  • Speeds up development.

Key Points

  • Python automatically assigns data types.
  • Python is a dynamically typed language.
  • Everything in Python is an object.
  • Python provides 14 built-in data types.
  • The type() function checks the data type.
  • The id() function returns the object's identity.
  • The print() function displays output.

Quick Summary

Category Data Types
Numericint, float, complex
Booleanbool
Stringstr
Sequencelist, tuple, range
Setset, frozenset
Mappingdict
Binarybytes, bytearray
SpecialNone

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - int Data Type