Concepts / Arithmetic Operations with Numbers

Arithmetic Operations with Numbers

To use that input as an integer in arithmetic, pass the input() call to int().

  • Programming

From Fixed Values to User Input

A program that always adds the same hard-coded values is limited. A more useful calculator can ask the user for a number, receive the response, and then use that response in an arithmetic calculation. Python's input() function provides the pause-and-receive step needed for this interaction.

Key point

input() pauses the program, waits for keyboard input, and returns what the user typed as a string.

The Waiting Point

Program executionPrompt displayedTell the user what to enterinput()Waiting for keyboard inputEnterUser finishes typingProgram resumesReturned text is availablereaches input()then waitsuser typespresses Enter
What happens to program execution before, during, and after the user presses Enter?

Execution does not continue immediately after Python reaches input(). First, the prompt is displayed. Then the program waits while the user types. After the user presses Enter, input() returns the typed text, and the program continues with the next operation.

Capturing a Response

KeyboardUser types a responseinput()Returns typed textStringThe user's responsenameStored stringtyped responsereturnsassigned to
How does data move from the keyboard through input() and into a variable?

Store the result of input() by assigning it to a variable. A prompt string should be supplied so the user knows what to enter. In the source example, the prompt asks, "What is your name?" When the user types Alice and presses Enter, the returned string is stored in the variable name.

python
Output
If the user types Alice and presses Enter, the value stored in name is the string "Alice".
Best practice

Give input() a clear prompt instead of calling it without an argument. The prompt appears before the program pauses and guides the user about what to enter.

Turning Text into a Number

"15"String returned by input()int()Converts the returnedstring15Integer usable inarithmeticwrap withproduces
What changes when the text returned by input() is converted with int()?

input() always returns a string, even when the user types digits. Therefore, input such as 15 is returned as the string "15". To use that input as an integer in arithmetic, pass the input() call to int(). The conversion changes the returned value from text to the integer 15.

python
Worked example

Read a Number and Add One

Capture a user's number and make it available for an arithmetic calculation.

  1. 1
    Display a prompt

    input() displays the prompt "Enter a number: " before waiting for the user's response.

  2. 2
    Receive the response

    After the user types a value and presses Enter, input() returns the typed value as a string.

  3. 3
    Convert immediately

    int() wraps the input() call, so the returned string is converted to an integer as the value is assigned to number.

  4. 4
    Use the integer

    The resulting integer can be used in arithmetic, such as adding 1 to the value.

Result

The variable number contains an integer rather than the original string returned by input().

Common Input Mistakes

  • Common mistake

    Treating input() as if it returned an integer automatically

    What it looks like
    value = input("Enter a number: ")
    Why it fails

    input() always returns a string, even when the user types numbers.

    Do this instead

    Use value = int(input("Enter a number: ")) when the response must be an integer for arithmetic.

  • Common mistake

    Calling input() without explaining what to enter

    What it looks like
    value = input()
    Why it fails

    The user is not given a prompt describing the expected input.

    Do this instead

    Pass a prompt string, such as input("Enter a number: ").

  • Common mistake

    Expecting the program to continue before the user presses Enter

    What it looks like

    The line after input() does not run while input() is still waiting.

    Why it fails

    input() pauses program execution until the user types and presses Enter.

    Do this instead

    Type the response and press Enter so input() can return the string and execution can resume.

Try the Interaction

What do you think happens?

A program reaches number = int(input("Enter a number: ")). What happens before the next line can use number?

  • The program assigns the prompt text to number immediately

  • The program waits for the user to type a response and press Enter

  • The program automatically chooses an integer

  • The program skips input() and continues

Reveal answer
Answer:

The program waits for the user to type a response and press Enter.

input() pauses execution and returns the typed response only after the user presses Enter. The surrounding int() call then converts that returned string to an integer.

EASY

Describe the stages of number = int(input("Enter a number: ")) in order. Include the prompt, the pause, the returned string, and the conversion to an integer.

Hints
  • Start with what the user sees.
  • Identify which function waits for keyboard input.
  • Remember that input() returns a string before int() converts it.

Key Takeaways

  1. 1
    input() displays a prompt when one is provided, pauses execution, and waits for the user to press Enter.
  2. 2
    The value returned by input() is always a string.
  3. 3
    Assigning input() to a variable stores the user's response for later use.
  4. 4
    Use int(input("prompt")) to convert the user's string response into an integer immediately.
  5. 5
    After conversion, the integer can be used in an arithmetic calculation.

At a glance

  • input() pauses a program until the user types a response and presses Enter.
  • A prompt string tells the user what to enter before the program waits.
  • input() returns text, including when the user types digits.
  • int() converts that returned text into an integer for arithmetic.
  • The concise pattern int(input("prompt")) captures and converts numeric input in one line.

Related concepts

More concepts starting with A