ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

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.

2. Numbers: Integers and Floats

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 , and type(12.5) shows . When we perform arithmetic on integers, the result is usually an integer, but division with / always gives a float.

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.

3. Strings

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.

4. Booleans and Comparison

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.

5. Lists and Tuples

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.

Quick Revision Tables

Table 1: Python Built-in Data Types

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)

Table 2: Comparison Operators

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

Mind Map

flowchart TD A["Python Data Types"] --> B["Numbers"] A --> C["Strings"] A --> D["Booleans"] A --> E["Lists"] A --> F["Tuples"] B --> B1["int - whole numbers"] B --> B2["float - decimal numbers"] C --> C1["len() and concatenation"] C --> C2["Indexing and slicing"] D --> D1["True or False"] D --> D2["Comparison operators"] E --> E1["Changeable [1, 2, 3]"] E --> E2["append(), remove()"] F --> F1["Unchangeable (1, 2, 3)"]

Important Diagrams (SVG)

Diagram 1: Python Data Types Overview

Python Data Types DATA TYPES int Whole numbers - 12 float Decimals - 12.5 str Text - "Hello" bool True or False list [1, 2, 3] tuple (1, 2, 3) Golden Rule: Every value in Python has a type - int, float, str, bool, list or tuple. Use type() to check the type of a value in your program.

Diagram 2: String Indexing

String Indexing in Python P y t h o n 0 1 2 3 4 5 Indexing starts from 0 word[0] gives 'P', word[3] gives 'h' word[0:3] gives 'Pyt' (slicing) Golden Rule: String indexing starts at 0, and len() gives the number of characters. Use concatenation with + and repetition with * to build new strings.

6. Detailed Concept Explanation

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.

Common Mistakes

  1. Using a single equals sign to compare values. The comparison operator is ==, while = is for assignment.
  2. Forgetting that input() returns a string. Numbers entered by the user must be converted with int() or float().
  3. Confusing lists with tuples. Lists are changeable, while tuples are unchangeable.
  4. Using the wrong brackets. Lists use square brackets [] and tuples use parentheses ().
  5. Believing that int(7.9) rounds to 8. It drops the decimal part and gives 7.
  6. Trying to concatenate a string and a number directly. We must convert the number to a string with str().
  7. Starting to access an index at 1. String and list indexing begins at 0.
  8. Confusing the len() function with counting words. len() counts characters in a string.
  9. Writing True or False in lowercase. Boolean values must be capitalised: True and False.
  10. Forgetting the type() function. type() is very useful to check the type of a value.

Exam Tips

  1. Learn the six main data types: int, float, str, bool, list and tuple, with examples.
  2. Remember that indexing starts at 0 for strings, lists and tuples.
  3. Know the functions len(), int(), float(), str() and type() and what they do.
  4. Memorise the comparison operators and their meaning.
  5. Understand the difference between = (assignment) and == (comparison).
  6. Be able to explain why lists are changeable and tuples are not.
  7. Practise writing expressions that mix strings, numbers and booleans.

Conclusion

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.