A variable is a name pointing to a value: score = 78. Python works out the type from the value — int (whole numbers), float (decimals), str (text), bool (True/False) and None (no value). Use type(x) to check.
Strings have many useful methods: .upper(), .strip(), .replace(), .split(). f-strings (f"...") insert values into text and can format them, e.g. {average:.1f} for one decimal place.
input() always returns a string. Convert it with int() or float() before doing arithmetic.
school = "Azania Secondary" # str
students = 420 # int
fee = 150_000.50 # float (underscores make big numbers readable)
is_open = True # bool
nickname = None # no value yet
students += 15
print(type(students), students)
name = " amina juma "
clean = name.strip().title()
print(clean) # Amina Juma
print(clean.split()) # ['Amina', 'Juma']
print(len(clean), clean.lower())
scores = "78,64,91"
total = sum(int(s) for s in scores.split(","))
average = total / 3
print(f"{school}: total={total}, average={average:.1f}")
age_text = "17" # imagine this came from input()
age = int(age_text)
print(f"Next year you will be {age + 1}")Key points
- Python infers types;
type()shows them. - f-strings are the clearest way to build text:
f"{value:.2f}". input()returnsstr— convert before calculating.
Exercise
Write a program that asks for a student's name and three scores using input(), then prints the name in title case and the average to one decimal place.
Show solution
Try the exercise yourself first — then compare your approach with this one.
input() always returns text, so convert each score with float() before adding. .strip().title() tidies the name, and :.1f in the f-string rounds the average to one decimal place.
name = input("Student name: ")
score1 = float(input("Score 1: "))
score2 = float(input("Score 2: "))
score3 = float(input("Score 3: "))
average = (score1 + score2 + score3) / 3
print(f"{name.strip().title()}: average {average:.1f}")Student name: amina hassan
Score 1: 78
Score 2: 64
Score 3: 91
Amina Hassan: average 77.7