1. First Steps & Data Basics

Math Operators, User Input & Type Conversion

Learn everyday math operators in Python, ask users for input with input(), understand why input() always gives text, and convert text to numbers safely.

4 min readUpdated 2026-11-29
In This Lesson
  • Perform math with +, -, *, /, // (floor division), %, and **
  • Ask users for input with input() and understand why it always returns text
  • Convert text safely using int() and float() to calculate real numbers

In Lesson 1, we typed values like coffee_price = 4.50 directly into our code. That means the program produces the exact same answer every time you run it!

Real programs ask the user for information—like "How many tickets do you want?" or "What was your total dinner bill?"—and then calculate the answer live.

Doing Math in Python

Python works like a smart calculator. Most math symbols match what you already know, plus three helpful extras:

  • + and -: Add and subtract (10 + 4 → 14)
  • * and /: Multiply and divide (10 * 4 → 40, 10 / 2 → 5.0) (Note: normal division / always gives a decimal number like 5.0)
  • // (Whole-Number Division): Divides and throws away the decimal remainder. Example: If you have 17 cookies and 5 friends, 17 // 5 gives 3 (each friend gets 3 whole cookies).
  • % (Remainder / Modulo): Gives you only what is left over after dividing. Example: 17 % 5 gives 2 (2 cookies are left in the box!).
  • ** (Power): 2 ** 3 means $2 \times 2 \times 2 = 8$.

Why Is % (Remainder) So Useful?

A classic trick in programming: how do you test if a number is even or odd? Divide by 2 and check the remainder (number % 2)!

  • 12 % 2 is 0 → even!
  • 13 % 2 is 1 → odd!

Asking the User with input()

To pause your program and let the user type an answer, use input():

python
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")

Here is what happens step by step:

  1. Python prints What is your name? and waits.
  2. You type Sam and press Enter.
  3. input(...) hands "Sam" back, and Python saves it inside name.
  4. Line 2 prints Nice to meet you, Sam!.

The Big Surprise with input()

Try to guess what goes wrong in this short program if the user types 5:

python
apples = input("How many apples? ")
double = apples * 2
print(double)

If you guessed 10, look at what Python actually prints: 55!

Why? Because input() ALWAYS returns text (str), even when you type digits! So apples holds the text "5", not the number 5—and multiplying text by 2 just repeats the text twice ("5" + "5" = "55").

How to Fix It: Convert Text to a Number

Before doing math with input(), convert it:

  • Use int(...) if you expect a whole number (like 3 friends).
  • Use float(...) if you expect a decimal number (like $48.50).
python
apples = int(input("How many apples? "))
print(apples * 2)   # Now if you type 5, it prints 10!

Let's read int(input("How many apples? ")) from the inside out:

  1. First, input(...) asks the question and gets "5" (text).
  2. Next, int("5") converts "5" into the whole number 5.
  3. Finally, apples stores 5!
Two Common Casting Errors
  1. int("12.5") crashes with ValueError! int() only accepts whole numbers without a decimal point. Whenever a user might enter decimals (like money or weight), use float(...).
  2. Formatting money cleanly: Sometimes math produces $15.5 instead of $15.50. Adding :.2f inside {total:.2f} tells Python: "Show 2 digits after the decimal point."

Worked Example — Dinner Bill Splitter

Let's build a clean script that splits a $60 dinner bill + tip among friends. (Notice we start with variables below so you can test instantly without popups—and you can swap them for float(input(...)) anytime!)

Python 3Try It Live: Dinner Bill & Tip Splitter
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Suppose the restaurant gives a $9.00 discount coupon (coupon = 9.00) taken off total_bill. Update the code to subtract coupon before dividing by friends, and see how much each friend pays!

Quick Checkpoints

Check Your Understanding

What does print(19 // 5, 19 % 5) output?

Check Your Understanding

A user types 7 when asked x = input('Number: '). What is type(x)?