Bytebook Compiler

Python Basics

Operators & Input

Arithmetic including // and **, comparison chains, logical and identity operators, input() with type conversion, and formatted output.

3 min read7 runnable examples4-question quiz

Arithmetic operators

Operator Meaning Example Result
+ - * add, subtract, multiply 17 + 5, 17 - 5, 17 * 5 22, 12, 85
/ true division (always float) 17 / 5 3.4
// floor division 17 // 5 3
% remainder 17 % 5 2
** power 17 ** 5 1419857
Python
print(17 / 5, 17 // 5, 17 % 5, 17 ** 5)
print(-17 // 5, -17 % 5)          # floor division rounds DOWN, so the remainder is non-negative
print(divmod(17, 5))              # quotient and remainder together
print(0.1 + 0.2, 0.1 + 0.2 == 0.3)
print(abs(-7.5), pow(2, 10), round(3.14159, 2))
Output
3.4 3 2 1419857
-4 3
(3, 2)
0.30000000000000004 False
7.5 1024 3.14

Comparison and logical operators

Comparisons return True or False, and Python lets you chain them like maths:

Python
x = 15
print(10 < x <= 20)                 # same as 10 < x and x <= 20
print(x == 15 and x % 2 == 1)
print(not x > 100 or x < 0)
print("apple" < "banana", "Zebra" < "apple")   # dictionary order; uppercase sorts first
Output
True
True
True
True True

Python uses the words and, or, not instead of &&, ||, !. They short-circuit, and they return one of their operands rather than strictly True/False:

Python
name = ""
print(name or "Guest")        # empty string is falsy, so "Guest"
print(0 and 10, 5 and 10)
Output
Guest
0 10

Identity and membership

Operator Checks
is, is not are both names the same object?
in, not in is a value inside a collection or string?
Python
vowels = "aeiou"
print("e" in vowels, "z" not in vowels)
print(3 in [1, 2, 3])
x = None
print(x is None)          # the right way to test for None
Output
True True
True
True

Augmented assignment

x += 1, x -= 2, x *= 3, x //= 2, x **= 2 … Python has no ++ or -- operators.

Reading input

input() always returns a string. Convert it before doing arithmetic:

Python
name = input("Your name: ")
age = int(input("Your age: "))
print(f"Hi {name}, next year you will be {age + 1}.")
Output
Your name: Ravi
Your age: 19
Hi Ravi, next year you will be 20.
Sample input is includedTry it Yourself

Read several numbers from one line with split() and map():

Python
a, b, c = map(int, input("Three numbers: ").split())
print("sum =", a + b + c, "max =", max(a, b, c))

numbers = list(map(int, "4 8 15 16 23 42".split()))
print(numbers, "average", sum(numbers) / len(numbers))
Output
Three numbers: 12 7 30
sum = 49 max = 30
[4, 8, 15, 16, 23, 42] average 18.0
Sample input is includedTry it Yourself

Formatted output

Python
item, qty, price = "Notebook", 3, 45.5
print(f"{item:<12}{qty:>4}{price * qty:>10.2f}")     # widths and alignment
print(f"{'Total':<12}{'':>4}{price * qty:>10.2f}")
print(f"{1234567.891:,.2f}")                          # thousands separator
print(f"{0.4567:.1%}")                                # percentage
print(f"{255:b} {255:o} {255:x}")                     # binary, octal, hex
print("{} scored {}".format("Asha", 91))             # older str.format style
Output
Notebook       3    136.50
Total               136.50
1,234,567.89
45.7%
11111111 377 ff
Asha scored 91

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    What is 7 // 2 + 7 % 2?

  2. 2

    What does -7 // 2 give in Python?

  3. 3

    What type does input() return?

  4. 4

    What does print(1 < 3 < 2) print?