Every value in Python has a data type. Understanding the type of data you are working with is crucial because it dictates what operations can be performed on it.
1. Numeric Types
These types handle numbers and calculations.
| Type | Description | Example |
int | Integers: Whole numbers (positive, negative, or zero) without a fractional part. | 10, -5, 4000000 |
float | Floating Point Numbers: Numbers that contain a decimal point. | 3.14, -0.5, 10.0 |
A. Basic Arithmetic Operators
| Operator | Operation | Example |
+ | Addition | 5 + 2 results in 7 |
- | Subtraction | 5 - 2 results in 3 |
* | Multiplication | 5 * 2 results in 10 |
/ | Division (always returns a float) | 5 / 2 results in 2.5 |
// | Floor Division (discards the remainder) | 5 // 2 results in 2 |
% | Modulo (returns the remainder) | 5 % 2 results in 1 |
** | Exponentiation | 5 ** 2 results in 25 |
2. The bool Type (Booleans)
The Boolean type represents the two truth values: True and False. These are fundamental to control flow and decision-making.
- Note:
TrueandFalsemust start with a capital letter.
is_adult = True
has_permission = False
3. String Type (str)
A String is a sequence of characters (text) enclosed in single quotes ('...'), double quotes ("..."), or triple quotes ("""...""").
- Triple Quotes: Used for multi-line strings and docstrings (P1.2).
A. String Indexing and Slicing
Strings, like lists, are ordered sequences, meaning each character has a specific position called an index.
| Concept | Description | Example |
| Indexing | Accessing a single character by its position. Starts at $\mathbf{0}$. | my_str[0] returns 'P' |
| Negative Indexing | Accessing characters from the end. $-1$ is the last character. | my_str[-1] returns 'n' |
| Slicing | Extracting a substring using start and end indices. (End index is exclusive). | my_str[0:4] returns 'Pyth' |
my_str = "Python"
print(my_str[0]) # Output: P
print(my_str[-1]) # Output: n
print(my_str[1:3]) # Output: yt
B. Formatted String Literals (f-strings)
f-strings are the modern, readable way to embed expressions or variables directly inside a string.
name = "Alice"
score = 95.5
message = f"User {name} scored {score} points."
print(message)
# Output: User Alice scored 95.5 points.
4. Finding the Type of an Object
You can use the built-in type() function to determine the data type of any value or variable.
a = 10
b = 3.14
c = "hello"
print(type(a)) # Output: <class 'int'>
print(type(b)) # Output: <class 'float'>
print(type(c)) # Output: <class 'str'>
5. Type Conversion (Casting)
Since Python is strongly typed, you often need to convert data from one type to another (e.g., converting a string input into a number for calculations).
You use the name of the type as a function to perform the conversion:
| Function | Purpose | Example |
int(value) | Converts to integer. Truncates floats. | int("5") $\rightarrow$ 5 |
float(value) | Converts to float. | float(5) $\rightarrow$ 5.0 |
str(value) | Converts to string. | str(3.14) $\rightarrow$ "3.14" |
Example: Converting User Input
Recall that input() returns a string. To do math, you must convert it.
# 1. User input is ALWAYS a string
str_num_a = input("Enter first number: ") # e.g., "10"
str_num_b = input("Enter second number: ") # e.g., "5"
# print(str_num_a + str_num_b)
# Output: "105" (Strings concatenate)
# 2. Conversion
num_a = int(str_num_a)
num_b = int(str_num_b)
# 3. Calculation
print(num_a + num_b)
# Output: 15 (Integers add)
