Ch 5 · Operators & Expressions · Python
Topic 5 of 13 in Python — Complete Tutorial (Beginner) — 5 lessons.
What is an Operator?
An operator is a special symbol that tells Python to perform an action on one or more values. You already know the most common ones from maths — + adds, - subtracts. In Python, operators go far beyond arithmetic: there are operators for comparing values, for combining logical conditions, and for updating variables in shorthand.
Arithmetic Operators
Arithmetic operators perform mathematical calculations on numbers. Most match the symbols you use in maths class. Python adds two you may not have seen: floor division //, which divides and throws away the remainder to give a whole number, and the remainder operator % (modulus), which gives you only the remainder. Both are surprisingly useful — // and % together can split a total number of seconds into minutes and seconds, for example.
Comparison Operators
Comparison operators compare two values and always produce a boolean — True or False. Python uses == to check if two values are equal (note the double equals — a single = means assignment) and != to check if they are not equal. The other comparisons — greater than, less than, and their "or equal" versions — work exactly as in maths. These operators are how your program asks questions about its data.
Logical Operators
Logical operators combine multiple boolean conditions into a single decision. Python uses real English words instead of symbols, which makes conditions wonderfully readable. The and operator requires every condition to be true. The or operator requires at least one to be true. The not operator flips a boolean: True becomes False and vice versa. You will use these constantly when your program makes decisions based on several factors at once.
Assignment Operators
Assignment operators are shorthand ways to update a variable using its current value. Writing score += 10 means exactly the same as score = score + 10 — take whatever score holds, add 10, and store the result back. These compact operators appear constantly whenever a value changes step by step: counting up a score, reducing a countdown, doubling an amount. They save typing and make the intent clearer.
All topics in Python Beginner
- Introduction to Programming & Python
- Your First Python Program
- Variables — Your Data Containers
- Data Types — Kinds of Values
- Operators & Expressions
- Conditional Statements
- Loops & Iterations
- Functions
- Lists — Collections of Data
- Dictionaries & Classes
- Strings — Working with Text
- Error Handling
- Mini-Projects & Practice