Fundamentals 8 min read

Python Built‑in Types: Numbers, Sequences, Mappings, Sets, Booleans, None and Other Types

This article introduces Python's built‑in data types—including numeric, sequence, mapping, set, boolean, None and other auxiliary types—explaining their characteristics, typical use cases, and providing concrete code examples to help readers write clear and efficient Python programs.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Python Built‑in Types: Numbers, Sequences, Mappings, Sets, Booleans, None and Other Types

Introduction In Python, built‑in types are predefined data structures that provide fundamental operations and functionality. Understanding these types is essential for writing efficient and reliable Python code.

Numeric Types

1.1 int Integers are whole numbers without a fractional part.

x = 42
print(type(x))  # output: <class 'int'>
print(x + 1)    # output: 43
print(x * 2)    # output: 84

1.2 float Floats represent numbers with a decimal part.

y = 3.14
print(type(y))  # output: <class 'float'>
print(y + 1)    # output: 4.14
print(y * 2)    # output: 6.28

1.3 complex Complex numbers consist of a real and an imaginary part.

z = 3 + 4j
print(type(z))   # output: <class 'complex'>
print(z.real)      # output: 3.0
print(z.imag)      # output: 4.0

Sequence Types

2.1 str Strings are ordered sequences of characters.

s = "Hello, world!"
print(type(s))   # output: <class 'str'>
print(s[0])      # output: H
print(s[-1])     # output: !
print(s[0:5])    # output: Hello
print(s.upper()) # output: HELLO, WORLD!

2.2 list Lists are ordered, mutable sequences that can contain elements of any type.

l = [1, 2, 3, "hello", 3.14]
print(type(l))   # output: <class 'list'>
print(l[0])      # output: 1
print(l[-1])     # output: 3.14
print(l[0:3])    # output: [1, 2, 3]
l.append(4)
print(l)         # output: [1, 2, 3, 'hello', 3.14, 4]

2.3 tuple Tuples are ordered, immutable sequences.

t = (1, 2, 3, "hello", 3.14)
print(type(t))   # output: <class 'tuple'>
print(t[0])      # output: 1
print(t[-1])     # output: 3.14
print(t[0:3])    # output: (1, 2, 3)

Mapping Types

3.1 dict Dictionaries are unordered collections of key‑value pairs.

d = {"name": "Alice", "age": 25, "city": "New York"}
print(type(d))               # output: <class 'dict'>
print(d["name"])           # output: Alice
print(d.keys())              # output: dict_keys(['name', 'age', 'city'])
print(d.values())            # output: dict_values(['Alice', 25, 'New York'])

Set Types

4.1 set Sets are unordered collections of unique elements.

s = {1, 2, 3, 4, 4}
print(type(s))   # output: <class 'set'>
print(s)         # output: {1, 2, 3, 4}
print(2 in s)    # output: True

4.2 frozenset Frozensets are immutable sets.

fs = frozenset({1, 2, 3})
print(type(fs))  # output: <class 'frozenset'>
print(2 in fs)   # output: True

Boolean Type

5.1 bool Booleans represent truth values: True or False.

a = True
b = False
print(type(a))          # output: <class 'bool'>
print(a and b)           # output: False
print(a or b)            # output: True
print(not a)             # output: False

None Type

6.1 NoneType None denotes the absence of a value.

x = None
print(type(x))          # output: <class 'NoneType'>
print(x is None)        # output: True

Other Built‑in Types

7.1 range Range creates an immutable sequence of integers.

r = range(1, 5)
print(type(r))          # output: <class 'range'>
print(list(r))          # output: [1, 2, 3, 4]

7.2 bytes Bytes represent an immutable sequence of bytes.

b = bytes([1, 2, 3])
print(type(b))          # output: <class 'bytes'>
print(b)                # output: b'\x01\x02\x03'

7.3 bytearray Bytearray is a mutable sequence of bytes.

ba = bytearray([1, 2, 3])
print(type(ba))         # output: <class 'bytearray'>
print(ba)               # output: bytearray(b'\x01\x02\x03')
ba.append(4)
print(ba)               # output: bytearray(b'\x01\x02\x03\x04')

Conclusion The article has covered Python's main built‑in types: numeric (int, float, complex), sequence (str, list, tuple), mapping (dict), set (set, frozenset), boolean (bool), None, and other types (range, bytes, bytearray). Mastering these types is crucial for writing clear, efficient, and reliable Python code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData Typesprogramming fundamentalsBuilt-in Types
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.