Learning objectives:
- Know the definition, purpose and use of integer, float, string and Boolean (2.1.1)
The four standard data types
A data type tells the program what kind of value is stored, which operations make sense and how much space it needs.
| Data type | Definition | Python example | Use it for |
|---|---|---|---|
Integer (int) | A whole number, positive, negative or zero | age = 17 | Counts, quantities, scores |
Float (float) | A number with a decimal part | price = 4.99 | Money, measurements, averages |
String (str) | A sequence of characters in quotes | name = "Amina" | Names, text, codes such as postcodes or phone numbers |
Boolean (bool) | Either True or False | logged_in = True | Yes/no states, flags, conditions |
Choosing the right type
- Use an integer when the value can only be whole: you cannot have 2.5 students.
- Use a float when precision matters: a temperature of 21.4.
- Use a string for text, and for numbers you never calculate with, such as phone numbers (a leading zero would be lost as an integer).
- Use a Boolean for anything with only two states.
quantity = 3 # integer
unit_price = 2.50 # float
product = "Pen" # string
in_stock = True # Boolean
total = quantity * unit_price
print(product, total) # Pen 7.5
input() always returns a string. To do maths with it you must convert it, which is covered in stop 2.2.