Fundamentals 7 min read

Master Python’s Core Data Types: Numbers, Strings, Lists, and More

This guide introduces Python’s fundamental data types—including integers, floats, complex numbers, strings, booleans, lists, tuples, dictionaries, sets, NoneType, enums, functions, modules, and classes—explaining their characteristics and providing concise code examples that demonstrate creation, manipulation, and common operations for each type.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Master Python’s Core Data Types: Numbers, Strings, Lists, and More

This article provides a concise reference for Python’s built‑in data types, describing their purpose, typical usage, and offering ready‑to‑run code snippets for each type.

Number Types

Python supports three numeric types: int for whole numbers, float for decimal numbers, and complex for complex numbers with real and imaginary parts.

int_num = 10
float_num = 3.14
complex_num = 3 + 4j
print(int_num, type(int_num))  # 10
print(float_num, type(float_num))  # 3.14
print(complex_num, type(complex_num))  # (3+4j)

String Type

Strings ( str) are immutable sequences of Unicode characters used to represent textual data.

str_text = "Hello world!"
print(str_text[0], type(str_text[0]))  # H
sub_str = str_text[7:12]
print(sub_str, type(sub_str))  # world!

Boolean Type

Booleans ( bool) have only two possible values: True and False. They are a special subclass of integers.

bool_val1 = False
bool_val2 = not bool_val1
print(bool_val1, type(bool_val1))  # False
print(bool_val2, type(bool_val2))  # True

List Type

Lists ( list) are mutable, ordered collections that can hold items of any type.

list_items = [1, "two", 3.14, False]
list_items.insert(2, 'new item')
print(list_items[2], type(list_items[2]))  # new item
list_items[0] = 2
print(list_items)  # [2, "two", "new item", False]
list_items.pop(0)
print(list_items)  # ["two", "new item", False]

Tuple Type

Tuples ( tuple) are immutable ordered collections.

tuple_items = (1, "two", 3.14, False)
# tuple_items[0] = 2  # raises TypeError
print(tuple_items[1], type(tuple_items[1]))  # two
part_tuple = tuple_items[1:]
print(part_tuple, type(part_tuple))  # ("two", 3.14, False)

Dictionary Type

Dictionaries ( dict) store key‑value pairs; keys must be unique and hashable.

dict_data = {"name": "John", "age": 30, "job": "developer"}
# add element
dict_data["city"] = "New York"
print(dict_data["name"], type(dict_data["name"]))  # John
# update element
dict_data["age"] = 31
print(dict_data)  # {'name': 'John', 'age': 31, 'job': 'developer', 'city': 'New York'}
# remove element
del dict_data["job"]
print(dict_data)  # {'name': 'John', 'age': 31, 'city': 'New York'}

Set Type

Sets ( set) are unordered collections of unique elements and support mathematical operations such as union and intersection.

NoneType

The None singleton represents the absence of a value.

my_none = None
if my_none is None:
    print("The variable is None")
else:
    print("The variable is not None")
try:
    print(my_none.name)
except TypeError as e:
    print(e)  # NoneType object has no attribute 'name'

Enum

Enums provide a set of symbolic names bound to unique, constant values.

from enum import Enum
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
my_color = Color.RED
print(my_color.name)  # RED
print(my_color.value)  # 1

Function

Functions are defined with def and can return values, including None.

def greet(name):
    return f"Hello, {name}"
greeting = greet("World")
print(greeting)  # Hello, World

def null_function():
    return None
result = null_function()
print(result)  # None

Module

Modules group related code; they can be imported with import.

import math
pi = math.pi
print(pi)  # 3.141592653589793
e = math.e
print(e)  # 2.718281828459045

Class

Classes define custom data structures and behavior.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def say_hello(self):
        return f"Hello, my name is {self.name} and I am {self.age} years old."
john = Person("John", 30)
print(john.name)  # John
print(john.age)   # 30
hello_message = john.say_hello()
print(hello_message)  # Hello, my name is John and I am 30 years old.

Stack (Implemented with a List)

A stack follows a last‑in‑first‑out (LIFO) order; Python lists can serve as stacks.

stack = []
stack.append(1)
stack.append('apple')
top_element = stack.pop()
print(top_element)  # 'apple'
top_element = stack.pop()
print(top_element)  # 1
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 TypesTutorialprogramming basics
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.