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.
- 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 like5.0)//(Whole-Number Division): Divides and throws away the decimal remainder. Example: If you have17cookies and5friends,17 // 5gives3(each friend gets 3 whole cookies).%(Remainder / Modulo): Gives you only what is left over after dividing. Example:17 % 5gives2(2 cookies are left in the box!).**(Power):2 ** 3means $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 % 2is0→ even!13 % 2is1→ odd!
Asking the User with input()
To pause your program and let the user type an answer, use input():
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")Here is what happens step by step:
- Python prints
What is your name?and waits. - You type
Samand press Enter. input(...)hands"Sam"back, and Python saves it insidename.- 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:
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 (like3friends). - Use
float(...)if you expect a decimal number (like$48.50).
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:
- First,
input(...)asks the question and gets"5"(text). - Next,
int("5")converts"5"into the whole number5. - Finally,
applesstores5!
int("12.5")crashes withValueError!int()only accepts whole numbers without a decimal point. Whenever a user might enter decimals (like money or weight), usefloat(...).- Formatting money cleanly: Sometimes math produces
$15.5instead of$15.50. Adding:.2finside{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!)
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
What does print(19 // 5, 19 % 5) output?
A user types 7 when asked x = input('Number: '). What is type(x)?