Python Data Types: The Ultimate Guide for Beginners
Python Data Types
A comprehensive guide to the built-in data types in Python, categorized by their behavior and characteristics.
🔢
Numeric Types
Used to represent numbers. Python automatically handles memory allocation for large numbers.
- int: Whole numbers, positive or negative, without decimals.
x = 42 - float: Numbers containing one or more decimals, or exponential notation.
y = 3.14 - complex: Written with a "j" as the imaginary part.
z = 3 + 5j
🧵
Sequence Types
Ordered collections of items where elements can be accessed via indexing.
- str: Textual data enclosed in single or double quotes. Immutable.
name = "Alice" - list: Ordered, mutable collections that allow duplicate members.
items = [1, "apple", 3.4] - tuple: Ordered, immutable collections that allow duplicate members.
point = (10, 20)
🗺️
Mapping Type
Unordered collections of key-value pairs. Keys must be unique and immutable.
- dict: Stores data as associated pairs for fast lookups. Mutable.
user = {"id": 1, "role": "admin"}
🧺
Set Types
Collections of unique, unordered items. Ideal for membership testing and eliminating duplicates.
- set: Unordered, mutable collection of unique items.
unique_ids = {1, 2, 3} - frozenset: Immutable version of a set; cannot be modified after creation.
fs = frozenset([1, 2])
⚖️
Boolean Type
Represents logical values used primarily in conditional statements and expressions.
- bool: Can only hold one of two values:
TrueorFalse. Evaluates logical conditions.
Comments
Post a Comment