Data is the information that a program works with, and every piece of data in Python has a type. A data type tells Python what kind of value something is and what operations can be performed on it. For example, we can add two numbers, but we cannot add two words in the same way. Understanding data types is essential because it helps us choose the correct operations and avoid errors in our programs.
Python has several built-in data types. The most important ones for beginners are numbers, which include integers and floats, strings for text, booleans for true or false values, and lists and tuples for storing collections of values. Python is a dynamically typed language, so we do not have to declare the type of a variable; Python infers it from the value we assign. This chapter explains each data type, how to use them, and the common operations and functions related to them.
Numbers in Python are of two main types. An integer is a whole number without a decimal point, such as 12, -5 or 100. A float is a number with a decimal point, such as 12.5, -3.14 or 0.0. The type() function tells us the type of a value. For example, type(12) shows
Floats and integers can be used together in arithmetic. When an integer and a float are mixed, the result is a float. For example, 5 + 2.5 equals 7.5. We can convert one type to another using functions: int() converts a value to an integer, and float() converts a value to a float. For example, int(7.9) gives 7, dropping the decimal part, and float(7) gives 7.0. Understanding these conversions helps us control the type of our data.
A string is a sequence of characters, which can be letters, digits, spaces or symbols. Strings are written in single quotes such as 'Hello' or double quotes such as "Hello". The length of a string can be found with the len() function. For example, len('Hello') returns 5. Strings can be joined together with the plus operator, which is called concatenation. For example, 'Good' + 'Morning' gives 'GoodMorning'. Strings can also be repeated with the asterisk operator.
Individual characters of a string can be accessed using indexing, where the first character has the index 0. For example, in the string word = 'Python', word[0] gives 'P' and word[1] gives 'y'. We can also extract a part of a string, called a slice, using two indices, such as word[0:3], which gives 'Pyt'. Strings have many built-in methods, such as upper() to convert to uppercase, lower() to lowercase, and title() to capitalise each word. These string operations make handling text easy.
A boolean is a data type that has only two possible values: True or False. Booleans are usually produced by comparison operators. For example, the expression 5 > 3 compares 5 and 3 and returns True, while 5 < 3 returns False. The comparison operators are == for equality, != for inequality, > for greater than, < for less than, >= for greater than or equal to, and <= for less than or equal to. A single equals sign assigns a value, while a double equals sign compares two values.
Booleans can also be combined with logical operators. The and operator returns True only if both values are true, the or operator returns True if at least one value is true, and the not operator reverses the value. For example, (5 > 3) and (2 > 1) is True, while (5 < 3) or (2 < 1) is False. Booleans and comparisons are the foundation of decision-making in programs, which we will study in the chapter on control structures.
Sometimes a single variable needs to store many values. A list is an ordered collection of values written inside square brackets, such as marks = [12, 45, 67] or fruits = ['apple', 'mango', 'banana']. Lists can contain mixed types, they can be changed, and their items are accessed by index starting from 0. For example, fruits[0] gives 'apple'. Lists have methods such as append(), which adds an item at the end, and remove(), which deletes an item.
A tuple is another collection type, but it is written in parentheses and cannot be changed after creation, so it is called immutable. For example, point = (10, 20) is a tuple. Tuples are useful for data that should never change. The len() function works on both lists and tuples to give the number of items. Choosing between a list and a tuple depends on whether we need to modify the data. These collection types allow us to store and manage groups of related values in one variable.
| Data Type | Meaning | Example |
|---|---|---|
| int | Whole number | 12, -5 |
| float | Decimal number | 12.5, 3.14 |
| str | Text | "Hello" |
| bool | True or False | True |
| list | Changeable collection | [1, 2, 3] |
| tuple | Unchangeable collection | (1, 2, 3) |
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | 5 == 5 is True |
| != | Not equal to | 5 != 3 is True |
| > | Greater than | 7 > 2 is True |
| < | Less than | 3 < 9 is True |
| >= | Greater than or equal | 5 >= 5 is True |
| <= | Less than or equal | 4 <= 4 is True |
The distinction between an integer and a float may seem small, but it has real consequences in a program. When we divide with the single slash operator, Python always returns a float, even when the division is exact; for example, 10 divided by 2 gives 5.0 and not 5. This matters when we compare values or use them in further arithmetic, because the type of a result affects how it is stored and displayed. The type() function is the programmer's tool for checking these details, and a quick call to type() can often explain why a program behaves differently from what we expected. Learning to predict the type of every expression is a habit that saves a great deal of debugging time.
Strings also repay careful study, because text operations are so common. The plus sign performs concatenation only when both values are strings; if we try to add a string and a number directly, Python raises a type error. This is why we convert numbers to strings with str() before joining them with words. Indexing is another source of confusion, since the first character is at position 0, not position 1. A helpful way to think about slices is to remember that a slice s[start:end] includes the start position but stops before the end position, which is the same convention used by the range function in loops. Because Python also allows negative indexing, where -1 refers to the last character, a string can be examined from either end without calculating its length first.
Lists and tuples both store collections, but their difference is fundamental to good program design. Because a list can be changed, we use it when the data will grow or be modified, such as a list of student marks to which new marks may be added. Because a tuple cannot be changed, we use it when the data must stay fixed, such as the coordinates of a point or the days of the week. Choosing the correct collection type is not just a matter of syntax; it communicates to anyone reading the program whether the data is meant to be permanent or changeable, and it protects the program from accidental modification. In the same way, booleans are more than simple answers to comparison questions, because they power the if statements and while loops that make programs decide and repeat. Every true or false value produced by a comparison can be stored in a variable, combined with and, or, and not, and used to control the flow of an entire program.
Data types define the kind of values a program can work with. Python provides integers and floats for numbers, strings for text, booleans for true or false, and lists and tuples for collections of values. We can check types with type(), convert between types with int(), float() and str(), and manipulate strings using indexing, slicing and concatenation. Mastering data types is essential before we learn control structures, where these values will be used to make decisions and repeat actions in our programs.