Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 4 of 14Free to read

Types and input() · Python from Zero

Why does "3" + "4" give 34, and how do you read what the user types?

Course contents

Why you need it

Every value in Python has a type, and the type decides what operations mean. + adds numbers but joins strings. Programs that talk to people need input(), and input() always gives back text, so you must convert it before doing maths. Mixing up types is the most common beginner bug.

The intuition

A phone number and a quantity both look like digits, but you'd never add two phone numbers. Python keeps track of what kind of thing each value is:

TypeExampleMeaning
int42whole number
float3.5decimal number
str"42"text
boolTrueyes / no

Convert between them: int("42") → 42, str(42) → "42", float("3.5") → 3.5.

input() waits for the user to type a line and returns it as a str. To use it as a number: age = int(input()).

Definition

Data type

A data type is a category of values, such as int, float, str or bool, that determines which operations can be applied to them.

input()

input() reads one line typed by the user and returns it as a string.

On this site, type the input lines in the Input box before you press Run.

Examples

Asking for the type

Try it yourself

type() tells you the type of any value.

print(type(3))
print(type(3.5))
print(type("3"))
print(type(True))
Step by step
  1. L1A whole number is an int.<class 'int'>
  2. L2With a decimal point it's a float.<class 'float'>
  3. L3In quotes it's a str, even if it contains digits.<class 'str'>
  4. L4True and False are bool.<class 'bool'>

Reading and converting

Try it yourself

Change the lines in the Input box and run again.

name = input()
age = int(input())
print(name, "will be", age + 1, "next year")
Input (one line per input() call)
Ada
36
Step by step
  1. L1Reads the first input line as text: "Ada".name = 'Ada'
  2. L2Reads "36" (text), then int() turns it into the number 36.age = 36
  3. L3age + 1 is 37 because age is a number.Ada will be 37 next year

Common mistakes

Forgetting int() around input()

input() + 1 is a TypeError: you can't add a number to text. Convert first: int(input()) + 1.

int() only accepts whole numbers

int("3.5") and int("seven") are ValueErrors. Use float() for decimals.

Quick quiz

1. What does this print?

print("5" * 3)
  1. A
    15
  2. B
    555
  3. C
    5 5 5
Show the answer

Answer: B

A string times a number repeats the string.

2. What does this print?

print(int("7") + 3, "7" + "3")
  1. A
    10 73
  2. B
    73 10
  3. C
    10 10
Show the answer

Answer: A

int("7") is the number 7, so 7 + 3 = 10. "7" + "3" joins two strings.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictText or number?

    Type exactly what this program prints.

  2. ModifyReally add them

    This program reads two numbers but prints them stuck together (3 and 4 give 34). Fix it so it prints their sum.

  3. WriteMinutes to hours

    Read a number of minutes. Print the whole hours on the first line and the minutes left over on the second line. For 135 the output is 2 and 15.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Data type数据类型A category of values that determines which operations apply to them.
int整数类型The type of whole numbers, such as 42.
float浮点数类型The type of numbers with a decimal point, such as 3.5.
input()input() 输入函数A function that reads a line typed by the user and returns it as a string.
Type conversion类型转换Turning a value into another type, for example with int() or str().