Data Types in Python

In this lesson, we will dive deeper into the Data Types in Python, which are crucial for managing data in your programs. Understanding these data types will help you write more efficient and effective Python code.


Overview of Python Data Types

Python provides several built-in data types that can be categorized as follows:

  1. Numeric Types: int, float, complex
  2. Text Type: str
  3. Sequence Types: list, tuple, range
  4. Set Types: set, frozenset
  5. Mapping Type: dict
  6. Boolean Type: bool
  7. Binary Types: bytes, bytearray, memoryview
  8. None Type: None

Let’s explore the most commonly used data types in detail.


Numeric Types

Python supports three numeric types: int, float, and complex.

int: Integer Numbers

Integer numbers are whole numbers, positive or negative, without decimals.

x = 10  # int type
y = -5  # int type

float: Floating-Point Numbers

Floating-point numbers represent real numbers with a decimal point.

a = 3.14  # float type
b = -2.5  # float type

complex: Complex Numbers

Complex numbers have a real and an imaginary part, represented as a + bj.

z = 2 + 3j  # complex type
print(z.real)  # Output: 2.0
print(z.imag)  # Output: 3.0

Text Type

str: String

Strings are sequences of characters enclosed in either single or double quotes.

name = "Alice"  # string type
greeting = 'Hello, world!'  # string type

You can perform operations on strings, such as concatenation and slicing:

message = "Hello" + " " + "Alice"
print(message)  # Output: Hello Alice

# Slicing
substring = message[0:5]
print(substring)  # Output: Hello

Boolean Type

bool: Boolean

Booleans represent logical values, either True or False.

is_active = True  # boolean type
is_logged_in = False  # boolean type

Booleans are often used in conditions to control program flow.

if is_active:
    print("The user is active.")

Sequence Types

list: Lists

Lists are ordered, mutable collections of items that can be of different types.

fruits = ["apple", "banana", "cherry"]  # list type
numbers = [1, 2, 3, 4]  # list type

# Modifying a list
fruits[0] = "orange"
print(fruits)  # Output: ['orange', 'banana', 'cherry']

tuple: Tuples

Tuples are ordered, immutable collections of items.

coordinates = (10, 20)  # tuple type

Tuples are useful when you want a collection of items that should not change.

range: Ranges

Ranges represent a sequence of numbers, commonly used in loops.

for i in range(5):
    print(i)  # Output: 0, 1, 2, 3, 4

Set Types

set: Sets

Sets are unordered collections of unique elements.

unique_numbers = {1, 2, 3, 4}  # set type

# Automatically removes duplicates
numbers = {1, 2, 3, 3, 4}
print(numbers)  # Output: {1, 2, 3, 4}

frozenset: Immutable Sets

Frozensets are like sets but immutable.

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

Mapping Type

dict: Dictionaries

Dictionaries are collections of key-value pairs, enclosed in curly braces.

person = {"name": "Alice", "age": 25}  # dictionary type

# Accessing values
print(person["name"])  # Output: Alice

# Modifying a dictionary
person["age"] = 26
print(person)  # Output: {'name': 'Alice', 'age': 26}

Binary Types

bytes: Immutable Binary Data

byte_data = b"Hello"
print(byte_data)  # Output: b'Hello'

bytearray: Mutable Binary Data

mutable_byte_data = bytearray(b"Hello")
mutable_byte_data[0] = 72  # ASCII value of 'H'
print(mutable_byte_data)  # Output: bytearray(b'Hello')

memoryview: Memory Views

Allows manipulation of binary data without copying it.

mv = memoryview(bytearray(b"Hello"))
print(mv[0])  # Output: 72

None Types

NoneType: Absence of Value

None is a special data type in Python used to represent the absence of a value or a null value.

x = None  # NoneType
print(x)  # Output: None

None is often used to indicate that a variable has no value assigned or as a placeholder for optional arguments in functions.


Type Conversion in Python

You can convert between different data types using built-in functions such as int(), float(), str(), etc.

Example 1: Convert a string to an integer

num_str = "123"
num_int = int(num_str)
print(num_int)  # Output: 123

Example 2: Convert an integer to a string

num = 456
num_str = str(num)
print(num_str)  # Output: "456"

Example 3: Convert a float to an integer

float_num = 3.14
int_num = int(float_num)
print(int_num)  # Output: 3

Copyright © 2025 Devship. All rights reserved.

Made by imParth