Sharing notes from my ongoing learning journey — what I build, break and understand along the way.
Python Basics Part 2: Operators in Python Explained for Beginners
Python Basics – Part 2: Operators in Python
As part of my 5-week Python training course, these notes cover the most important Python operators — from arithmetic and string operations to logical and comparison operators.
We’ll also look at assignment shortcuts, the versatile in operator, and chained comparisons.
In upcoming posts, I’ll share extra exercise examples to practice these concepts.
Arithmetic Operators
Python supports standard mathematical operations: addition (+), subtraction (-), multiplication (*), and division (/).
print(3 + 4)
print(10 - 7)
print(12 * 2)
Output:
7
3
24
You can also store results in a variable:
result = 3 + 4
print(result)
Output:
7
Division (/)
In Python 3, division with / always returns a float.
print(9 / 3) # 3.0
print(10 / 3) # 3.3333333333333335
print(round(10/3,2)) # 3.33
Floor Division (//)
Performs division and rounds down to the nearest integer.
print(9 // 3) # 3
print(10 // 3) # 3
Modulo (%)
Returns the remainder of a division.
print(10 % 3) # 1
print(7 % 3) # 1
print(4 % 3) # 1
Common use: checking if a number is even or odd.
num = 7
print(num % 2) # 1 → odd
num2 = 8
print(num2 % 2) # 0 → even
Special cases:
print(7 % 7) # 0 (equal numbers)
print(4 % 7) # 4 (first smaller than second)
Exponentiation (**)
Raises a number to a power.
print(2 ** 3) # 8
print(2 ** 8) # 256 (8 bits = 1 byte)
print(2 ** 24) # 16777216
print(2 ** 32) # 4294967296 (IPv4 address count)
String Operators
first_name = 'Peter'
last_name = 'Wellert'
Concatenation (+)
Joins strings together. Both operands must be strings.
name = first_name + last_name
print(name)
name2 = first_name + ' ' + last_name
print(name2)
Output:
PeterWellert
Peter Wellert
If you try to concatenate a string and an integer, Python will throw an error.
To fix this, convert the number using str():
# print('Age: ' + 5) # ❌ Error
print('Age: ' + str(5)) # ✅ "Age: 5"
String Multiplication (*)
Repeats a string a given number of times.
print('Hello' * 7)
Output:
HelloHelloHelloHelloHelloHelloHello
You can use it for simple separators too:
print('-' * 30)
Comparison Operators
Comparison (or relational) operators always evaluate to a Boolean (True or False).
print(7 == 8) # False
a = 7
b = 8
answer = a == b
print(answer)
Output:
False
Common Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 7 == 7 | True |
!= | Not equal to | 7 != 8 | True |
> | Greater than | 10 > 5 | True |
< | Less than | 3 < 1 | False |
>= | Greater or equal | 5 >= 5 | True |
<= | Less or equal | 4 <= 6 | True |
Remember: = is assignment, == is comparison.
🧾 Assignment Operators
Assign or update variable values.
number = 42
print(number)
Output:
42
Short Forms
Without shorthand:
x = 23
x = x + 1
print(x)
With shorthand:
x += 6
print(x)
Output:
24
30
Other assignment operators:
x -= 1
x *= 2
x /= 4
x %= 5
x **= 2
print(x)
Multiple Assignment
a = b = c = 0
print(a, b, c)
Output:
0 0 0
Logical Operators
Also called Boolean operators, named after mathematician George Boole.
and
Returns True only if both operands are True.
print(True and True) # True
print(True and False) # False
print(False and True) # False
print(False and False) # False
or
Returns True if at least one operand is True.
print(True or True) # True
print(True or False) # True
print(False or True) # True
print(False or False) # False
not
Negates the Boolean value.
print(not False) # True
print(not True) # False
num = 7
print(not (num == 8)) # True
print(num != 8) # True
Membership Operators (in, not in)
Check whether a value exists within a sequence (like a list, string, or tuple).
numbers = [1, 4, 7, 9]
print(4 in numbers) # True
print(5 in numbers) # False
print(4 not in numbers) # False
print(5 not in numbers) # True
Equivalent:
print(not (4 in numbers)) # False
Strings:
print('e' in 'Hello') # True
print('Hell' in 'Hello') # True
Dictionary keys:
my_dict = {"a": 1, "b": 2}
print("a" in my_dict) # True
print(1 in my_dict) # False
Using in as a cleaner alternative to multiple or checks:
num = 8
print(num == 7 or num == 8 or num == 9) # True
print(num in [7, 8, 9]) # True
Chained Comparison
Python allows chaining comparisons in a single statement.
num = 7
if 3 < num < 9:
print("num is between 3 and 9")
Output:
num is between 3 and 9
This is equivalent to:
if num > 3 and num < 9:
but shorter and cleaner.
Summary
In this lesson, we learned:
- Arithmetic operators (
+,-,*,/,//,%,**) - String operators (
+,*) - Comparison and assignment operators
- Logical operators (
and,or,not) - Membership operator (
in,not in) - Chained comparisons
Next up: Conditional Statements and Loops in Python — the foundation for building logic and control flow.
