3.2.1, 3.2.7 - Data types, input and output (Python)

3.2.1, 3.2.7 - Data types, input and output (Python)

A program needs data in a suitable form, a way to receive it and a way to communicate its results. This lesson introduces Python 3 values, keyboard input and display output for the written Python route.

Choosing a data type

A value's type affects the operations that make sense on it: adding two numbers calculates a sum, while adding two strings joins their text.

Data type

A category of data that determines the values it can represent and the operations that can be performed on them.

General exam namePython formSuitable example
Integerint4: a whole-number ticket count
Realfloat2.75: a distance with a fractional part
BooleanboolTrue: a member status, either True or False
Charactera one-character str"B": one grade symbol
Stringstr"AB12 3CD": a postcode containing several characters

Python has no separate character type; it uses a string of length one. The specification's general term real is represented here by float. Quotes distinguish text from a number: "42" is a string and 42 is an integer. A telephone number is usually text because it is an identifier, can begin with zero and is not a quantity to add. Meaningful names such as distance also make the intended interpretation clear.

Reading keyboard input

Keyboard input lets the user supply a value while the program is running.

Python's input() displays its prompt, waits for the user to enter a line and returns that line as a string. Assignment stores the returned value under a name.

Python
visitor_name = input("Name: ")
ticket_count = int(input("Tickets: "))

If the user enters Mina and 3, visitor_name holds the string "Mina" and ticket_count holds the integer 3. The int() conversion is necessary before ordinary numeric processing; float() can convert a numeric string representing a real value. For examination input tasks, assume the user supplies the stated numeric type unless the task asks for other checks. Converting an input is different from proving it lies in an allowed range.

The following sandbox connects an integer input to a calculated result. Predict the total for input 3, run it, then change the ticket price to 7 and run with 3 again. Explain why the conversion belongs on the input line.

Ticket calculation

{"code":"ticket_count = int(input("Tickets: "))\nticket_price = 5\ntotal = ticket_count * ticket_price\nprint("Total:", total)","instructions":"Predict and run with 3 tickets. Change ticket_price from 5 to 7, then run with 3 again."}

Displaying results

Display output turns a stored or calculated result into information a person can read.

print() displays its values and ends with a new line by default. Commas separate items and insert spaces, so a label can explain a result without converting the numeric value yourself.

Python
visitor_name = "Mina"
ticket_count = 3
print("Visitor:", visitor_name)
print("Tickets:", ticket_count)
Output
Visitor: Mina
Tickets: 3

Quoted "ticket_count" would display that literal name; unquoted ticket_count displays its stored value. A helpful output includes the relevant label or unit, for example print("Mass in kg:", mass). Input receives data; output communicates data or information after any required processing.