Python Data Types in Detail

Learn Python @ Freshers.in

Python’s versatility is attributed to its diverse set of data types. In this comprehensive guide, we will explore Python’s data types in detail, covering text, numbers, sequences, mappings, sets, boolean values, binary types, and the special NoneType. Through real-world examples and outputs, you’ll gain a solid understanding of Python’s fundamental data types.

1. Text Type: str

Strings represent text in Python and can be enclosed in single, double, or triple quotes.

Example:

name = "Alice"
message = 'Hello, Python!'

2. Numeric Types: int, float, complex

Python supports integers, floating-point numbers, and complex numbers.

Example:

integer = 42
floating_point = 3.14
complex_number = 2 + 3j

3. Sequence Types: list, tuple, range

Sequences are ordered collections. Lists are mutable, tuples are immutable, and range represents a sequence of numbers.

Example:

my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
my_range = range(1, 5)

4. Mapping Type: dict

Dictionaries are unordered collections of key-value pairs.

Example:

person = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

5. Set Types: set, frozenset

Sets are unordered and contain unique elements, while frozensets are immutable sets.

Example:

my_set = {1, 2, 3}
my_frozenset = frozenset({4, 5, 6})

6. Boolean Type: bool

Boolean values represent truth values, either True or False.

Example:

is_python_fun = True
is_raining = False

7. Binary Types: bytes, bytearray, memoryview

Binary data is represented by bytes, bytearray, and memoryview objects.

Example:

binary_data = bytes([65, 66, 67])
byte_array = bytearray([68, 69, 70])
memory_view = memoryview(byte_array)

8. None Type: NoneType

NoneType represents the absence of a value or a null value in Python.

Example:

empty_variable = None
Author: user