Skip to main content

Python Data Types

In Python, every value has a data type. Data types define the nature of a value, and Python provides a wide variety of built-in data types to handle different kinds of data. Understanding these is crucial for effective programming.


Data Types in Python​

CategoryData Type
Text Typestr
Numeric Typesint, float, complex
Sequence Typeslist, tuple, range
Mapping Typedict
Set Typesset, frozenset
Boolean Typebool
Binary Typesbytes, bytearray, memoryview
None TypeNoneType

Text Type: str​

A sequence of Unicode characters.

name = "Dhruba"

You can perform operations like:

  • Slicing
  • Concatenation
  • Length check with len()

Numeric Types​

int​

Whole numbers:

age = 25

float​

Decimal numbers:

pi = 3.14

complex​

Numbers with real and imaginary parts:

z = 2 + 3j

Sequence Types​

list​

Mutable, ordered sequence:

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

tuple​

Immutable, ordered sequence:

dimensions = (1024, 768)

range​

Represents a sequence of numbers:

nums = range(5)

Mapping Type: dict​

Unordered collection of key-value pairs:

person = {
"name": "Alice",
"age": 30
}

Set Types​

set​

Unordered, mutable, no duplicates:

unique_ids = {1, 2, 3}

frozenset​

Immutable version of a set:

readonly_ids = frozenset([1, 2, 3])

Boolean Type: bool​

Only True or False:

is_active = True

Binary Types​

bytes​

Immutable byte sequence:

b = b"Hello"

bytearray​

Mutable version:

ba = bytearray([65, 66, 67])

memoryview​

Provides memory-efficient access:

mv = memoryview(bytes([1, 2, 3]))

None Type​

Represents no value:

response = None

Type Checking and Conversion​

Check type​

type(3.14)  # Output: <class 'float'>

Type Conversion​

int("5")     # Output: 5
str(10) # Output: "10"
list("abc") # Output: ['a', 'b', 'c']

Conclusion​

Python provides a variety of built-in data types to handle data in efficient and expressive ways. Knowing when and how to use each data type is essential for writing clean and effective Python code.