Python BasicsCore· 30 min read

Operators & Input

Do maths, compare values, and ask the user for input.

What you will learn

  • Use arithmetic and comparison operators
  • Get input with input()
  • Convert text to numbers

Maths

An operator is a symbol that does something to values — most are the maths symbols you already know, plus a few handy extras Python adds.

Arithmetic operators
print(10 + 3)   # 13
print(10 / 3)   # 3.333...
print(10 // 3)  # 3   (whole-number division)
print(10 % 3)   # 1   (remainder)
print(2 ** 3)   # 8   (power)

Reading each line: + adds (10 plus 3 is 13). A single / is normal division and always gives a decimal (10 ÷ 3 is 3.333…). The double // is floor division — it divides and throws away anything after the dot, giving the whole number 3. The % (called modulo) gives the remainder left over after dividing — 10 split into groups of 3 leaves 1. And ** means power, so 2 ** 3 is 2 × 2 × 2 = 8.

Note: Output: 13 3.3333333333333335 3 1 8

Tip: The % remainder operator is surprisingly useful: n % 2 == 0 is the classic way to test if a number n is even (an even number leaves no remainder when divided by 2). Do not worry about the == yet — we explain it right below.

Comparing values

Besides doing maths, operators can also compare two values and answer a simple yes/no question. The answer always comes back as a bool — either True or False. These are the comparisons you will use in nearly every if statement, so it is worth learning their plain-English meaning:

OperatorMeansExampleResult
==is equal to5 == 5True
!=is NOT equal to5 != 3True
>is greater than5 > 3True
<is less than5 < 3False
>=is greater than or equal to5 >= 5True
<=is less than or equal to3 <= 5True

Watch out: A common beginner trap: a single = stores a value (age = 20), while a double == asks a question (age == 20, “is age equal to 20?”). They are not the same — mixing them up is one of the most common early mistakes.

Let us see a few comparisons in action. Each one is a yes/no question, so each prints True or False:

Comparison operators return True or False
age = 20
print(age == 20)   # is age equal to 20?
print(age != 18)   # is age NOT equal to 18?
print(age >= 18)   # is age 18 or more?
print(age < 13)    # is age less than 13?

Going line by line, with age holding 20: age == 20 asks “is age equal to 20?” — yes, so it prints True. age != 18 asks “is age NOT equal to 18?” — 20 is indeed not 18, so True. age >= 18 asks “is age 18 or more?” — 20 is more, so True. age < 13 asks “is age less than 13?” — no, 20 is not under 13, so this one prints False. Notice == (a question) is different from = (which stores a value).

Note: Output: True True True False

Tip: These comparisons are the questions you put inside if statements (the very next lesson). For example, if age >= 18: runs its block only when that comparison is True.

Getting input

So far our programs only talk to the user. The input() function lets the user talk back — it pauses the program, shows a prompt, and waits for the person to type something and press Enter. Whatever they type comes back as text, so you must convert it with int() or float() before doing maths.

input() reads what the user types
name = input("Your name: ")
age = int(input("Your age: "))   # convert text to a number
print("Next year you will be", age + 1)

Here is the flow of this little program, step by step:

  1. input("Your name: ") prints the prompt Your name: and waits. The user types their name and presses Enter; that text is stored in name.
  2. input("Your age: ") does the same for the age — but the user’s answer is text (e.g. the characters "22", not the number 22).
  3. int(...) wraps that text and converts it into a real number, which is stored in age. Now maths will work on it.
  4. The last line adds 1 to age and prints the sentence, so a 22-year-old sees Next year you will be 23.

Note: Output: Your name: Asha Your age: 22 Next year you will be 23 (The first two lines include what the user typed.)

Watch out: Remember: input() gives text. int(input(...)) + 5 adds numbers; without int(), "20" + 5 would crash. Always convert input you’ll do maths on.

Q. What type does input() always return?

Answer: input() returns a string. Wrap it in int() or float() to use it as a number.

✍️ Practice

  1. Ask for two numbers and print their sum.
  2. Show the remainder of one number divided by another.

🏠 Homework

  1. Build a tiny calculator that asks for two numbers and prints +, −, × and ÷.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →