Letting the program listen
So far your programs have known everything in advance. input() is how you let a person hand something to the program while it's running.
name = input("What's your name? ")
print(f"Hi, {name}!")
If you type Maya and press Enter, the output is:
What's your name? Maya
Hi, Maya!
input() does three things, in order:
- Prints the prompt you gave it (the text inside the parentheses).
- Stops the program and waits until you type something and press Enter.
- Hands back whatever you typed, so you can put it in a box.
That middle step surprises people. Your program is genuinely frozen at that line. Nothing after it runs until Enter is pressed.
The trap that catches everybody
age = input("How old are you? ")
print(age + 1)
Type 18 and you get:
TypeError: can only concatenate str (not "int") to str
In plain words: you tried to add a number to a piece of text, and Python refused.
Here's the fact underneath it, and it's worth memorizing:
input()always hands back a string, no matter what the person typed.
You typed 18. Python gave you "18" — the character one followed by the character eight. Not the number 18. It looks identical on screen. It is not the same thing.
Why does that break? Because + means two different things depending on what's on either side:
print(3 + 4)
print("3" + "4")
Output:
7
34
For numbers, + adds. For strings, + glues them end to end. When Python sees text on the left and a number on the right, it has no way to know which one you wanted, so it stops instead of guessing. That's a kindness. A silently wrong answer is far worse than an error message.
Converting between types
int() turns text into a whole number:
age = int(input("How old are you? "))
print(f"Next year you turn {age + 1}")
Typing 18 gives:
How old are you? 18
Next year you turn 19
Read that first line from the inside out: input(...) runs first and produces "18", then int(...) converts it to 18, then that lands in age. Nested calls always work inside first.
The three conversions you'll use constantly:
print(int("18"))
print(float("1.5"))
print(str(18) + " years")
Output:
18
1.5
18 years
Two conversions that fail, so you recognize them later:
int("abc")raisesValueError. The type is right — you did hand it a string — but there's no number hiding in those letters.int("1.5")also raisesValueError.int()won't parse a decimal point out of text. Go through float first:int(float("1.5"))gives1, and note that it chops the decimal part off rather than rounding.
Two kinds of division
print(7 / 2)
print(7 // 2)
print(7 % 2)
Output:
3.5
3
1
/is ordinary division, and the result always has a decimal point, even when it divides evenly.6 / 3gives2.0, not2.//divides and throws away the remainder. Useful for "how many whole boxes fit."%is the remainder itself, pronounced "modulo." Useful far more often than you'd guess — a number is even whenn % 2 == 0, and it's the standard way to ask "does this divide evenly?"
Operator quick reference
print(2 + 3)
print(2 - 3)
print("ab" * 3)
print(2 ** 10)
Output:
5
-1
ababab
1024
* between a string and a number repeats the string, which is occasionally handy for drawing separator lines: print("-" * 40).
** is "to the power of." 2 ** 10 is 2 multiplied by itself 10 times.
A complete small program
weight = float(input("Weight in kilograms: "))
height = float(input("Height in meters: "))
bmi = weight / (height ** 2)
print(f"Your BMI is {bmi:.1f}")
With 70 and 1.75 typed in:
Weight in kilograms: 70
Height in meters: 1.75
Your BMI is 22.9
Two things to notice.
float instead of int, because heights have decimal points and int("1.75") would fail.
{bmi:.1f} inside the f-string is a format specifier: keep one digit after the decimal point. Without it you'd see 22.857142857142858, which is technically correct and useless to a human. :.2f gives two digits, and so on. You'll use this every time you print money.
Ten-second check
What does print("3" + "4") output?
The answer is 34. Both sides are text, so + glues rather than adds. To get 7, convert first: print(int("3") + int("4")).
Every time you read a number from a person, a file, or the internet, it arrives as text. Converting it is your job, not Python's.