All lessons

Numbers & Math

6 min · Python

Use Python as a calculator — and do math with your variables.

Python · Numbers & Math0/5 cleared

Enemy 1 of 5

At its core, Python is a calculator that also happens to be able to do everything else. If you print a math expression, Python evaluates it first — works out the numeric answer — and only then hands the result to print. The expression itself never appears in the output; only its value does.

Run this — notice it prints 5, not the text 2 + 3, because there are no quotes around it. That's the key difference between code and text: 2 + 3 without quotes is an instruction to compute, while "2 + 3" with quotes is just four characters to display as-is. Mixing these up — forgetting quotes around text, or accidentally adding quotes around a number you meant to calculate with — is one of the most common early mistakes.

You
Syntax Skeleton

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: 5