Washington | 16°C (light rain)
Understanding Python Data Types

A friendly guide to Python’s built‑in data types

Python treats everything as an object, and each object belongs to a specific data type. This article walks through numbers, sequences, sets, dictionaries, and booleans with clear examples.

When you write x = 10 in Python, you’re not just storing a value—you’re also telling the interpreter, “Hey, this is an int.” Every value you work with has a type, and that type decides what you can do with it.

Let’s start with the basics: numeric types. Python knows three kinds of numbers. Integers (int) are whole numbers, positive or negative, without a decimal point. Floats (float) are real numbers that include a fractional part, like 3.14. Finally, complex numbers (complex) store a real and an imaginary part, written as 2+3j. A quick demo:

a = 5
b = 5.0
c = 2 + 4j
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>

Moving on to sequences, which are ordered collections you can index into. The first and most common sequence is the string (str). You can create a string with single, double, or triple quotes, and then pull out characters by position:

s = "Welcome to Python"
print(s) # Welcome to Python
print(s[1]) # e
print(s[-1]) # n

Lists (list) come next. They’re mutable, meaning you can change, add, or remove items after creation. Elements may be of mixed types:

nums = [1, 2, 3]
mixed = ["Geeks", 42, True]
print(mixed[3-1]) # 42

Tuples (tuple) look a lot like lists, but they’re immutable—you can’t alter them once they exist. A tiny quirk: a single‑item tuple needs a trailing comma, otherwise Python treats it as just the item itself.

t1 = (1,) # this is a tuple
t2 = ("a", "b", "c")
print(t2[0]) # a

The boolean type (bool) is simple: it’s either True or False. Booleans pop up everywhere—especially in if statements. Python also has the notion of “truthy” and “falsy” values: non‑zero numbers, non‑empty containers, etc., evaluate to True, while 0, None, or empty collections act like False.

if 1:
print("1 is truthy")
if not 0:
print("0 is falsy")

Sets (set) store unordered collections of unique items. Because they’re unordered, you can’t rely on an index to fetch an element; you typically loop over a set.

animals = {"cat", "dog", "cat"}
print(animals) # {'cat', 'dog'}
for a in animals:
print(a)

Dictionaries (dict) are perhaps the most useful built‑in type. They map unique keys to values, letting you retrieve data quickly with square brackets or the get() method. Keys are case‑sensitive, so "Key" and "key" are different.

person = {"name": "Alice", "age": 30}
print(person["name"]) # Alice
print(person.get("age")) # 30

All these types work together harmoniously. Knowing which type you’re dealing with helps the interpreter store data efficiently and prevents you from accidentally applying an unsupported operation.

Comments 0
Please login to post a comment. Login
No approved comments yet.

Editorial note: Nishadil may use AI assistance for news drafting and formatting. Readers can report issues from this page, and material corrections are reviewed under our editorial standards.