In this article9
In lesson 1 you met Python's data types. Now you'll put them to work: show values with print(), keep them in variables and change them, build sentences with f-strings, and ask the user a question with input(). By the end you'll write a small program that talks to its user. Every example runs right here: press Run, then press Edit and change it.
Your first line: print()
print() shows whatever you put in its brackets. Text goes in quotes:
print("Hello, world!")
print("I'm learning Python.")
print(7 + 3)Three things to notice:
- Text in quotes is a string. You can use
"double"or'single'quotes, as long as both ends match. - Numbers have no quotes.
7 + 3is maths, so Python prints10. - Each
print()starts a new line.
You can give print() several values, separated by commas. It puts a space between them:
print("Apples:", 4, "Pears:", 2)Variables: a name for a value
A variable keeps a value under a name, so you can use it later. You make one with =:
name = "Ada"
age = 36
print(name)
print(age)
print(name, "is", age, "years old")Unlike JavaScript, there's no let, const or var. You just write the name, =, and the value. You can give a variable a new value whenever you like:
score = 0
print(score)
score = score + 10
print(score)
score += 5 # the short way to write: score = score + 5
print(score)Everything after # is a comment: a note for people, which Python skips.
A quick reminder: strings and numbers
From lesson 1: "7" in quotes is a str, 7 without quotes is an int. That matters as soon as you use +:
print(7 + 7) # 14: adding numbers
print("7" + "7") # 77: joining two strings
print("ha" * 3) # hahaha: a string repeatedKeep this in mind: it's the reason for the most common input() mistake further down.
f-strings: values inside a sentence
Joining text with + gets messy fast. Python has a better way, the f-string: put f before the quotes, and write variables in {curly brackets}:
name = "Ada"
age = 36
print(f"My name is {name} and I am {age} years old.")
print(f"Next year I'll be {age + 1}.")Anything inside {} is real Python, so you can even do sums there, like {age + 1}. This is the way most Python code builds text today.
input(): ask the user something
input() stops, waits for the user to type an answer, and gives it back to you. The text in the brackets is the question.
Here the answers come from the Input box under the code: one line for each input(). Type your name there, then press Run:
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")input() always gives you a string
This is the most common surprise for beginners. Even if the user types 12, input() gives you the text "12", not the number. To do maths with it, turn it into a number with int() (whole numbers) or float() (decimals).
Put two lines in the Input box, for example Ada and 36, and run it:
name = input("Name: ")
age = int(input("Age: "))
print(f"{name}, in ten years you'll be {age + 10}.")int(input("Age: ")) reads from the inside out: first input() gets the text, then int() turns it into a number.
Common mistakes
Exercise
Show the answer
product = input("Product: ")
price = float(input("Price: "))
amount = int(input("How many: "))
total = price * amount
print(f"{amount} x {product} = {total} euros")The price uses float() because it can have decimals, the amount uses int() because you can't buy half a coffee. Storing the sum in total keeps the f-string short and easy to read.
Want exactly two decimals, like 7.50? Write {total:.2f} inside the f-string.
Quick check
Quick check
What does this print?
a = "5"
b = "5"
print(a + b)Why
a and b are strings, because of the quotes. + joins two strings, so you get "55". To get 10, write the numbers without quotes, or turn them into numbers with int().
Quick check
The user types 20. What is the type of age here: age = input("Age: ")?
Why
input() always gives a string, whatever the user types. Use int(input(...)) to get a number.