Python Data Types Explained: int, str, list, dict, tuple
Python determines a variable's type from its value, not a declaration. This guide covers int, float, str, list, tuple, set, and dict with placement-round examples.
Python determines a variable’s type from its assigned value, not from a declaration. The type controls which operations are valid on that value.
Pick the wrong type and you get a TypeError at runtime, not a compile-time warning. For placement rounds and practical coding in Python 3.13, knowing the seven core built-in types is a baseline, not a bonus.
What Python’s Type System Actually Controls
A data type is not just a label. It specifies which operations Python will allow and which it will refuse.
Consider two variables:
a = 10
b = "5"
a + b raises TypeError: unsupported operand type(s) for +: 'int' and 'str'. Python will not silently coerce the integer to a string or the string to a number. You must do that conversion explicitly.
The Python 3 Built-in Types documentation organises all built-in types by category. For most practical work, and for the vast majority of placement-round questions, seven types cover the full range: int, float, complex, str, list, tuple, set, and dict.
To inspect a value’s type at runtime, use the built-in type() function:
x = 42
print(type(x)) # <class 'int'>
y = "placement"
print(type(y)) # <class 'str'>
z = [1, 2, 3]
print(type(z)) # <class 'list'>
The Python 3 Built-in Functions documentation covers type(), along with the conversion functions int(), float(), and str() discussed later in this article.
Number Types: int, float, and complex
Python 3 supports three numeric types.
int stores whole numbers of arbitrary size. Python 3 merged the older long type (from Python 2) into int, so there is no upper bound on the integer value beyond available memory. Division between two integers returns a float by default; use // (floor division) to get an integer result.
a = 10
b = 3
print(a / b) # 3.3333... (float)
print(a // b) # 3 (int)
print(a % b) # 1 (int, remainder)
float stores double-precision decimal values (64-bit IEEE 754). Floating-point arithmetic is not exact:
print(0.1 + 0.2) # 0.30000000000000004
This surprises many students in placement rounds. When exact decimal arithmetic matters, Python’s decimal module provides fixed-precision alternatives.
complex stores complex numbers with a real part and an imaginary part:
c = 3 + 4j
print(c.real) # 3.0
print(c.imag) # 4.0
Complex numbers appear in signal-processing and electrical engineering contexts but rarely in standard placement aptitude rounds.
Sequence Types: str, list, and tuple
All three are ordered and indexable by integer position (starting at 0). The key difference is mutability.
str
A str is an immutable sequence of Unicode characters. Once created, no individual character can be changed. Python returns a new string for every string operation:
s = "placement"
print(s[0]) # 'p'
print(s[-1]) # 't'
print(s[0:5]) # 'place'
print(s.upper()) # 'PLACEMENT' (new string; s is unchanged)
String immutability is a common placement question: “Can you modify a string in Python?” The answer is no. Assigning s[0] = 'P' raises TypeError. To build a modified version, create a new string.
For a practical example of string slicing in an interview context, the palindrome check shows how immutability and reverse-slicing interact in a classic problem.
list
A list is a mutable, ordered sequence. Elements can be any type and can be mixed:
items = [1, "python", 3.14, True]
items[0] = 99 # allowed — list is mutable
items.append("new") # adds to end
items.pop() # removes last element
print(type(items)) # <class 'list'>
Lists are Python’s general-purpose sequence. They map closely to arrays in other languages, though without fixed element types. For a practical list-based problem, the smallest and largest element in an array article walks through traversal logic that works identically with Python lists.
tuple
A tuple is an immutable ordered sequence, written with parentheses:
coords = (10, 20)
print(coords[0]) # 10
# coords[0] = 99 # TypeError: tuple is immutable
Tuples are faster than lists for iteration and use less memory. They are also hashable (when their elements are hashable), which means they can serve as dictionary keys or set members. Lists cannot.
A concise mutability comparison:
| Type | Mutable | Ordered | Indexable | Duplicates allowed |
|---|---|---|---|---|
| str | No | Yes | Yes | Yes |
| list | Yes | Yes | Yes | Yes |
| tuple | No | Yes | Yes | Yes |
Unordered Collection Types: set and dict
set
A set holds unique elements. Adding a duplicate does nothing:
numbers = {1, 2, 3, 2, 4}
print(numbers) # {1, 2, 3, 4} (order may vary)
print(type(numbers)) # <class 'set'>
Sets support the standard mathematical operations (union, intersection, and difference), which makes them useful for problems involving overlapping sequences. Because sets are unordered, they have no index; you cannot write numbers[0].
dict
A dict maps unique keys to values. Since Python 3.7, dicts maintain insertion order as a language guarantee:
student = {"name": "Ananya", "cgpa": 8.4, "branch": "CSE"}
print(student["name"]) # 'Ananya'
student["year"] = 4 # adds a new key
print(type(student)) # <class 'dict'>
Dictionary keys must be hashable. Strings, integers, and tuples qualify. Lists do not. Attempting {[1, 2]: "value"} raises TypeError: unhashable type: 'list'.
For a broader view of when to use each structure, the data structures interview questions article covers the trade-offs between arrays (lists), hash maps (dicts), and other structures that appear in technical rounds.
Type Conversion in Python
Python provides built-in functions to convert between types explicitly:
int(x)convertsxto an integer.int(3.9)returns3(truncates, does not round).int("42")returns42.int("3.9")raisesValueError.float(x)converts to floating-point.float("3.14")returns3.14.float("hello")raisesValueError.str(x)converts any value to its string representation.str(100)returns"100".str([1, 2])returns"[1, 2]".list(x)converts an iterable to a list.list("abc")returns['a', 'b', 'c'].list((1, 2))returns[1, 2].tuple(x)converts an iterable to a tuple.set(x)converts an iterable to a set, removing duplicates.
A common exam trap:
x = "10"
y = 5
print(x + y) # TypeError: cannot add str and int
print(int(x) + y) # 15 (correct)
Data Types in Placement-Round Questions
Technical rounds at service-tier companies test data type awareness through output-prediction questions. Three patterns repeat frequently:
- Type of operation result: what does
type(10 / 2)return? Answer:<class 'float'>, because/always returns float.type(10 // 2)returns<class 'int'>. - Mutability trap: a function receives a list and modifies it in place. The caller’s list also changes, because lists are passed by reference to the same object. Tuples prevent this.
- Dict key errors: accessing a key that does not exist raises
KeyError. Usedict.get(key, default)to avoid it.
Python 3.13 (the current stable release as of 2026) also added isinstance() improvements and better error messages that name the expected type directly in the exception, which is useful context if you read CPython release notes that some interview panels reference.
Knowing the mutable-versus-immutable distinction for each type answers most entry-level technical questions. The deeper questions, covering hash collisions in dicts, memory layout of lists, and CPython’s interning rules for small integers, appear at product-company or senior-level screens.
The mutable-versus-immutable line (mutable: list, dict, set; immutable: int, float, str, tuple) also matters when writing Python for production systems. In LLM API responses, JSON fields often arrive as strings that need conversion to float or int before further processing. Using type() to inspect a field before operating on it, and int() or float() to convert it explicitly, is the same pattern you practise in these placement-round output-prediction questions. TinkerLLM is a good environment to see this in practice, at ₹299 for access to a Python-first LLM playground where the JSON parsing exercises reinforce exactly this type-handling discipline.
Primary sources
Frequently asked questions
What is the difference between a list and a tuple in Python?
Lists are mutable: you can add, remove, or change elements after creation. Tuples are immutable: once defined, no element can be added, changed, or removed. Use tuples for fixed collections (coordinates, RGB values) and lists when the collection needs to change.
Can you change a string after you create it in Python?
No. Strings in Python are immutable. Operations like upper() or replace() return a new string object; they do not modify the original. To accumulate characters efficiently, build a list and join at the end: ''.join(char_list).
What does the type() function return in Python?
It returns the class of the value passed to it. type(42) returns the class int, type('hello') returns the class str, and type([1, 2]) returns the class list. The output format is
Is Python dynamically typed?
Yes. Python infers the type from the assigned value at runtime rather than requiring an explicit declaration. A variable that holds an integer can later hold a string. This flexibility is useful but means type errors surface at runtime, not at compile time.
How do you convert an int to a string in Python?
Use the str() built-in: str(42) returns the string '42'. The reverse, int('42'), converts a numeric string to an integer. int('hello') raises ValueError because 'hello' cannot be parsed as an integer.
Why can't a list be used as a dictionary key?
Dictionary keys must be hashable. Hashability requires that the value's hash does not change during its lifetime. Lists are mutable, so their contents can change, which would invalidate any hash computed earlier. Tuples of hashable elements are hashable and can serve as keys.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)