ProceduralOCR J277
DashboardCoursePracticeReviewProgressGlossary
Saved on this deviceContinue learning

Procedural · OCR J277 programming practice

Progress stays in this browser unless you export it.

Course/Data types & casting/Converting input into numbers
J277 · 2.2.2

Unit 4 · Lesson 2

Converting input into numbers

Lesson progress0 / 7

Build the idea

Start with the concept and its key vocabulary.

Key idea

Use int() or float() to cast input into a suitable numeric type.

Python input is always a string. Cast it before a numeric calculation: int(input(...)) for a whole number or float(input(...)) for a decimal. OCR ERL also publishes real() as an alternative decimal cast. str() creates text, while bool() converts a value to a Boolean: for example, bool(1) is true.

Casting

Converting one data type into another.

int()

A cast that converts a suitable value into an integer.

float() / real()

Casts that convert a suitable value into a decimal number.

str()

A cast that converts a value into a string.

bool()

A cast that converts a suitable value into a Boolean.

Keep these in mind

  • Use int() for whole-number input.
  • Use float() or ERL real() for decimal input.
  • Use str() when a number must be concatenated into text.
  • Use bool() to convert a suitable value to true or false.

Learning objective

Use int() or float() to cast input into a suitable numeric type.

Exam tip

Define casting generally as converting one data type into another. 'Changing to a string' is only one example, not the full definition.

Common mistake

Without casting, input stays as text. A program may join digits instead of calculating or cause a runtime error.

PreviousNext