🐍 Paper 2 · Topic 6: Programming
6.1a Variables, Constants & Data Types in Python
Edexcel 1CP2 · GCSE Computer Science · ~12 min read · ✅ Free
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

Variables in Python

A variable is a named location in memory used to store data that can change during program execution. In Python, variables are created by assignment:

# Creating variables age = 16 name = "Alice" temperature = 37.5 is_student = True

Python uses dynamic typing — you don't declare the type; Python infers it automatically.

Variable Naming Rules

  • Must start with a letter or underscore (not a number)
  • Can contain letters, numbers, and underscores
  • Case-sensitive: age and Age are different variables
  • Cannot use reserved keywords (if, while, for, etc.)
  • Convention: use snake_case for variable names in Python

Constants in Python

A constant is a value that does not change during program execution. Python has no built-in constant mechanism, but the convention is to use UPPER_CASE names:

# Constants (by convention — Python does not enforce this) MAX_SCORE = 100 PI = 3.14159 SCHOOL_NAME = "CSZone Academy"
Exam tip: For Edexcel, you must know the difference between variables (can change) and constants (do not change). Mention that constants improve readability and make programs easier to maintain — if you change the constant value, you only change it in one place.

Data Types

Every value in Python has a data type that determines what kind of data it holds and what operations can be performed on it:

Data TypeDescriptionPython ExampleEdexcel Pseudocode
Integer (int)Whole numbers, positive or negativeage = 16INTEGER
Float (float)Decimal / real numbersprice = 9.99REAL
String (str)Text — sequence of characters in quotesname = "Alice"STRING
Boolean (bool)True or False onlylogged_in = TrueBOOLEAN
Char (character)Single character (Python uses str)grade = "A"CHAR

Checking Data Types

# type() function returns the data type print(type(42)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("hello")) # <class 'str'> print(type(True)) # <class 'bool'>

Type Casting

Type casting (type conversion) converts a value from one data type to another. This is essential when accepting user input (which is always a string):

# Converting types age_str = input("Enter your age: ") # input() returns a string age = int(age_str) # Convert to integer price = float("9.99") # String to float: 9.99 number_str = str(42) # Integer to string: "42" is_valid = bool(1) # Integer to bool: True

Common type casting functions: int(), float(), str(), bool()

Input and Output

# Taking input and displaying output name = input("What is your name? ") age = int(input("How old are you? ")) print("Hello, " + name) print("In 10 years you will be", age + 10)
Exam tip: The input() function ALWAYS returns a string. A very common exam question asks you to identify the bug when a student forgets to cast the result of input() to an integer before doing arithmetic.
⚠️ Common Mistakes
  • Forgetting that input() returns a string — must cast to int/float for arithmetic
  • Confusing True/False (Python booleans with capital T/F) with strings
  • Using spaces or hyphens in variable names (Python requires underscores)
  • In Edexcel pseudocode: INTEGER not int, REAL not float, STRING not str
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — 6.1a Variables, Constants & Data Types

8 Edexcel-style questions · instantly marked

Q1State the difference between a variable and a constant in programming.[2]
✅ Mark scheme
A variable is a named storage location whose value can change during program execution [1]; a constant is a named value that is fixed and does not change during execution [1].
Q2Write Python code to ask the user for their age and store it as an integer.[2]
✅ Mark scheme
age = int(input("Enter your age: ")) [2] — award 1 mark if input() used without int() (type casting missing) or if variable assigned but not cast.
Q3What data type would be most appropriate for each of the following? Justify one of your choices. (a) A student's score out of 100. (b) Whether a user is logged in. (c) A person's height in metres.[4]
✅ Mark scheme
(a) Integer — scores are whole numbers [1]; (b) Boolean — can only be True or False [1]; (c) Float/Real — height involves decimal values [1]; Justification for one: e.g. Boolean chosen because logged-in status has only two possible states — either True (logged in) or False (not logged in) [1].
Q4A student writes: score = input("Enter score: ") / 10. Identify the error and explain how to fix it.[2]
✅ Mark scheme
Error: input() returns a string; you cannot divide a string by a number [1]; Fix: cast the input to a number first: score = float(input("Enter score: ")) / 10 or int(input(...)) [1].
Q5Give two advantages of using named constants rather than 'magic numbers' (literal values) in a program.[2]
✅ Mark scheme
Any two: improves readability — the name makes the purpose clear [1]; easier to maintain — change the value in one place and it updates everywhere in the program [1]; reduces errors — avoids accidentally using different values in different places [1].
Q6What does the function type() do in Python? Give an example.[2]
✅ Mark scheme
type() returns the data type of a value/variable [1]; e.g. type(42) returns <class 'int'>, type("hello") returns <class 'str'> [1].
Q7Identify the data type of each value: (a) 3.14 (b) "False" (c) True (d) 99 (e) "99"[5]
✅ Mark scheme
(a) float [1]; (b) string — it is in quotes [1]; (c) boolean [1]; (d) integer [1]; (e) string — it is in quotes, not an integer [1].
Q8Write a short Python program that: asks for a user's name and age, calculates what year they were born (assume current year is 2025), then prints a message with their name and birth year.[4]
✅ Mark scheme
name = input("Enter your name: ") [1]; age = int(input("Enter your age: ")) [1]; birth_year = 2025 - age [1]; print(name + " was born in " + str(birth_year)) or equivalent [1]. (Accept f-string, concatenation, or print with commas. Must cast input to int and birth_year to str for concatenation.)
Topic Quiz
Q 1 of 15
You scored
out of 15
Click to reveal definition
🎉
Session complete!
TermDefinition
🎯

Mini Test — Variables & Data Types

Timed exam-style test — 10 minutes.

← 5.3b Environmental ImpactTopic 6 · PythonNext: 6.1b Selection & Iteration →