SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
Edexcel 1CP2 · Topic 6 · 6.1a

Variables, Constants
& Data Types in Python

int · float · str · bool · Casting · Constants · Type Errors

CSZoneEdexcel GCSE Computer Science 1CP2
Variables in Python

Storing Data

name = "Alice" # string (str)
age = 16 # integer (int)
height = 1.72 # float
passed = True # boolean (bool)

print(name, age) # output: Alice 16
Python is dynamically typed — you don't declare the type, Python infers it from the value
Variable names: use snake_case (e.g. first_name), must not start with a number
type(variable) returns the data type of a variable
Data Types & Casting

Converting Between Types

score = input("Enter score: ") # input() returns str
score = int(score) # cast to int

price = 9.99
print(str(price)) # "9.99" (str)
print(int(price)) # 9 (truncated, not rounded)
int(): converts to integer; float(): to decimal; str(): to string; bool(): to boolean
Edexcel: input() always returns a string — always cast before arithmetic
TypeError: adding a string to an integer without casting causes a runtime error
Constants

Values That Should Not Change

# Python has no built-in constant keyword
# Convention: use UPPER_CASE names
PI = 3.14159
MAX_SCORE = 100
VAT_RATE = 0.2

print("Max score:", MAX_SCORE)
A constant is a named value that should not change during program execution — e.g. tax rates, mathematical constants
Using named constants makes code more readable and easier to maintain than using "magic numbers"
Exam Practice

Have a go at this question

Edexcel-style question
The following Python code contains an error. Identify the error and explain how to fix it.

age = input("Enter age: ")
next_year = age + 1
print(next_year)
2 marks
input() returns a string, so adding 1 causes a TypeError [1]. Fix: cast to int: age = int(input("Enter age: ")) [1].
Key Takeaways

What to Remember

4 main data types: int, float, str, bool — Python infers type automatically
input() always returns str — always cast with int() or float() before arithmetic
Constants: UPPER_CASE by convention; no Python keyword, but treated as unchangeable
type() checks a variable's type; casting converts between types