Python variables, data types and control
Use Python 3.10+ variables, constants, types, input/output, casting, selection, iteration, counting and totalling.
Section Component 2 · Algorithms and Programming
Mapped to the Cambridge IGCSE Computer Science 0265 syllabus for exams in 2029. These original explanations and self-checks are independent study support, not Cambridge papers, official marks or grades. Practical programming is taught with worked code and trace tasks; an executable Python workspace and timed digital-exam simulation are not yet available.
Before you begin
- Use the 2029 syllabus for this course. Cambridge specifies Python 3.10 or higher for programming and digital exams for both components. The syllabus currently identifies this route for 2029 only; confirm later-year availability with Cambridge and your school.
Work on paper. Try each question before revealing help, and explain why your method works.
Quick readiness check
Before python variables, data types and control, which statement is the safest starting point?
What you will learn
- Initialise variables and constants and use the four basic data types
- Write sequence, selection and iteration constructs
- Use input, output, casting, counting and totals
Values and input need correct types
A variable stores a value that can change; a constant is intended to remain fixed and is conventionally named in uppercase. The specified data types are int, float, str and bool. Input from input() is text, so use int() or float() when numeric work is required.
Output uses print(). Casting converts a value between types, but conversion must make sense: int('12') works, while int('twelve') raises an error. Use meaningful names, valid indentation and consistent data types so a program can be traced and maintained.
Control constructs express an algorithm
Sequence executes statements in order. Selection uses if, elif and else; nested selection places a decision inside another branch, and match-case selects among patterns. Iteration uses a count-controlled for loop, a pre-condition while loop, a post-condition style while/break pattern, or nested loops.
A counter increases to record how many items meet a condition. A total adds values. Initialise them before the loop, update them at the right point and check whether a loop must run zero times or at least once. Python uses indentation to show which statements belong to each block.
Pause and explain
Which habit best checks your understanding of python variables, data types and control?
Worked example
Read three integer scores and print their total.
Show the worked solution
- Start total at 0.
- Repeat three times, convert each input to int and add it to total.
- Print total after the loop.
Answer total = 0 for _ in range(3): total += int(input("Score: ")) print(total)
Try a topic test
Choose Easy, Normal or Difficult. Save your answers and open worked review after finishing.
Test this topic →From guided practice to a new situation
Write your answer and reasoning first. Use a hint only if you are stuck. The model solution is for self-checking; your written work is not automatically marked.
Write Python statements to input an age, convert it to an integer and print the next age.
Give me a hint
Cast the input before using arithmetic.
Compare my reasoning
- Use input() to read the value as text.
- Convert it with int().
- Add 1 and print the result.
age = int(input("Age: ")) print(age + 1)
Look for these in your work
- Input read
- Type converted
- Output calculated
Write a loop that reads four temperatures, counts values below 0 and prints the count.
Give me a hint
Initialise a counter and test each converted reading.
Compare my reasoning
- Set below_zero = 0 before the loop.
- Repeat four times, convert each input to float and increment if it is below 0.
- Print the counter after all readings.
below_zero = 0 for _ in range(4): temp = float(input("Temperature: ")) if temp < 0: below_zero += 1 print(below_zero)
Look for these in your work
- Counter initialised
- Input converted and tested
- Count printed after loop
A café gives a 10% discount if a bill is at least 50 units; otherwise it gives no discount. Write pseudocode or Python that selects the correct final bill and explain the boundary.
Give me a hint
Use a comparison that includes exactly 50.
Compare my reasoning
- Read or set bill.
- If bill >= 50, calculate bill × 0.9; otherwise keep bill unchanged.
- Test bill = 50 to confirm it receives the discount.
if bill >= 50: final_bill = bill * 0.9 else: final_bill = bill
Look for these in your work
- Condition correct
- Both branches present
- Boundary checked
Common mistakes
- Adding input strings instead of numeric values.
- Resetting a total inside the loop.
- Using inconsistent indentation or a loop with the wrong boundary.
What can you explain now?
What type does input() return before casting?
Compare with the explanation
str
Keyboard input is read as text, even when the learner types digits.
After trying it yourself, choose your next review. This is your self-assessment.
Your review choice appears on Today. Sign in to sync it across devices.
Make it stick
How do types, casting, selection, iteration, counters and totals work together in a Python algorithm?
If you needed the solution, close it and solve the problem again from a blank page. A correct answer is stronger when you can explain the reason for each step.