Every program works on data. To work on data effectively, a programmer must understand the types of data that Python can store, how they can be converted from one form to another, and which functions are available to operate on them. Data handling in Python is built around the concept of data types, which define the kind of value a variable holds and the operations that can be performed on it.
Python treats everything as an object, and every object has a type. This chapter explores the built-in data types in detail, starting with the fundamental types: integers, floats, complex numbers, strings, and booleans. We will learn how to check the type of a value using the type() function, how to convert between types using functions like int(), float(), str(), and bool(), and how to write expressions using these values.
We will also look at operators in depth, including arithmetic, relational, logical, assignment, identity, and membership operators, along with the precedence rules that decide the order in which operators are evaluated. Understanding precedence and associativity is essential because it determines the result of complex expressions. By the end of this chapter, you will be able to write correct expressions, convert data safely between types, and predict the output of expression-based questions.
Python's built-in data types can be grouped as:
int, float, complex.str, list, tuple.dict.set, frozenset.bool.bytes, bytearray.Every value in Python belongs to one of these types, and the type determines its behaviour. We can verify the type with the built-in function type().
print(type(10))
print(type(2.5))
print(type("hello"))
print(type(True))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Integers are positive or negative whole numbers without a fractional part. There is no upper limit on the size of an integer in Python 3, which makes it very convenient for large calculations.
a = 100
b = -25
print(type(a))
Floats represent numbers with a decimal point and can also be written in scientific notation using e.
x = 3.14
y = 1.5e3
print(y)
Output: 1500.0. The value 1.5e3 means 1.5 multiplied by 10 to the power 3.
Complex numbers are written in the form a + bj, where a is the real part, b is the imaginary part, and j represents the square root of -1.
z = 3 + 4j
print(z.real)
print(z.imag)
Output:
3.0
4.0
A string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes. Triple quotes allow multi-line strings. Strings are immutable, which means their content cannot be changed after creation.
name = "Aarav"
greeting = 'Hello'
multi = """This is
a multi-line string"""
Important string properties:
- Indexing: Characters are accessed using indices starting from 0. name[0] gives 'A'.
- Slicing: A substring is extracted using [start:end]. name[1:4] gives 'ara'.
- Concatenation: Strings are joined with +.
- Repetition: * repeats a string. "ab" * 3 gives "ababab".
- len(): Returns the number of characters in the string.
word = "Python"
print(word[0])
print(word[1:4])
print(len(word))
print(word + " Rocks")
print(word * 2)
Output:
P
yth
6
Python Rocks
PythonPython
The boolean type bool has only two values: True and False. Booleans are the result of relational and logical operations and are used heavily in decision making.
print(10 > 5)
print(10 == 5)
print(bool(0))
print(bool("Hello"))
Output:
True
False
False
True
Notice that bool(0) is False but bool("Hello") is True, because an empty or zero value is considered falsy while non-empty values are truthy.
Sometimes we need to change the type of a value. Python provides built-in functions for this:
int(value): Converts to integer. Truncates floats and parses numeric strings.float(value): Converts to float.str(value): Converts to string.complex(real, imag): Creates a complex number.bool(value): Converts to boolean.print(int(3.99))
print(int("25"))
print(float("3.5"))
print(str(100))
print(bool(1))
Output:
3
25
3.5
100
True
A point to remember: int("3.5") will raise a ValueError because an integer cannot be created directly from a string containing a decimal point; you must convert to float first.
+, -, *, /, //, %, **. While / always returns a float, // floors the result and % returns the remainder.
<, >, <=, >=, ==, != compare two values and return True or False.
and, or, not combine boolean values. and returns the first falsy operand or the last operand if all are truthy.
=, +=, -=, *=, /=, %=, //=, **=. The expression x += 5 is equivalent to x = x + 5.
is and is not check whether two variables refer to the same object in memory, not whether their values are equal.
in and not in check whether a value exists inside a sequence such as a string, list, or tuple.
When an expression contains many operators, Python evaluates them according to precedence. From highest to lowest (simplified):
( )**+x, -x, not*, /, //, %+, -<, >, ==, etc.notandorprint(2 + 3 * 4)
print((2 + 3) * 4)
print(10 - 4 + 2)
Output:
14
20
8
Because * has higher precedence than +, 2 + 3 * 4 is evaluated as 2 + 12 = 14. Parentheses override precedence. Addition and subtraction have the same precedence and are evaluated left to right, so 10 - 4 + 2 gives 8.
| Data Type | Category | Example | Mutable |
|---|---|---|---|
| int | Number | 42 |
No |
| float | Number | 3.14 |
No |
| complex | Number | 2 + 3j |
No |
| str | Sequence | "Hello" |
No |
| bool | Boolean | True |
No |
| list | Sequence | [1, 2, 3] |
Yes |
| tuple | Sequence | (1, 2, 3) |
No |
| dict | Mapping | {"a": 1} |
Yes |
| set | Set | {1, 2, 3} |
Yes |
| Function | Purpose | Example | Result |
|---|---|---|---|
| int(x) | Convert to integer | int(3.9) |
3 |
| float(x) | Convert to float | float("2.5") |
2.5 |
| str(x) | Convert to string | str(100) |
"100" |
| bool(x) | Convert to boolean | bool(0) |
False |
| complex(r,i) | Create complex number | complex(2,3) |
(2+3j) |
int("3.5") directly, which raises a ValueError; the string must be converted to float first.= for comparison, causing a SyntaxError instead of checking equality.name[0] = 'X', which raises a TypeError./ returns an integer; it always returns a float in Python.+, e.g., "age: " + 17, which raises a TypeError.bool("False") is True because the non-empty string is truthy.2 + 3 * 4 wrong by evaluating left to right.is to compare values instead of identities, which can give unexpected results for some objects.type(), for example <class 'float'>.// and % are connected: a = (a // b) * b + (a % b).word[start:end] excludes the character at the end index.Data handling is the heart of programming in Python. By understanding data types, the programmer knows what operations are valid, and by understanding conversion functions, values can be reshaped safely. Operators and precedence rules determine how expressions are evaluated, and mastering them removes most of the ambiguity and errors in program output. These concepts form the vocabulary of every program, and the chapters on control structures, lists, and dictionaries build directly on this foundation to create truly useful programs.