Core Data Types
It’s essential to recognise the common data types you’ll work with in Python:
- String – text like
"Hello!","Toy Story 4","Boeing 747". - Integer – whole numbers like
1470,0,-34. - Float – decimal numbers like
-32.12,3.14. - Boolean –
TrueorFalse. - Character – in Python this is simply a string of length 1 (e.g.
"a","6","?").
Casting Between Types
Convert values from one type to another using these functions:
str(x)→ stringint(x)→ integer (truncates decimals)float(x)→ float
Example: converting a number to text so it can be joined inside a sentence:
total = 45
print("You owe £", total, "in total.") # comma-join (adds spaces automatically)
print("You owe £" + str(total) + " in total.") # using str() to concatenate
Division Returns a Float
In Python 3, the division operator / returns a float, even when the result is mathematically a whole number. You can convert it to an integer with int() if needed (this drops the decimal part):
total = 100 / 10
print("The answer is", total) # 10.0 (float)
print("The answer is", int(total)) # 10 (integer)
Data Types Task 1 – Time
Ask the user for the current hour and minute as integers, then output the time as text using str():
hour = int(input("What is the hour? "))
minute = int(input("What is the minute? "))
print("The time is " + str(hour) + ":" + str(minute))
Data Types Task 2 – Decimal
Read an integer from the user, then show it as a decimal using float():
number = int(input("Enter any number: "))
print(float(number))