Operators & Expressions
Doing Things With Values
Storing values in variables is useful. But a program that only stores things and never acts on them isn't much of a program.
An operator is a symbol that performs an action on one or more values. You already know the basic ones from mathematics. Python uses most of them the same way — with a few important additions and one small surprise.
Arithmetic Operators
Run this and look carefully at the last line. 10 / 3 gives 3.3333... — a float. In Python, division always produces a float, even when the numbers divide evenly:
The answer is 5.0, not 5. The division operator / insists on returning a decimal result. If you want a whole number, Python gives you a different operator for that.
Floor Division and Remainder
Two operators that beginners often haven't seen before:
10 // 3 is 3 — how many whole times does 3 fit into 10?
10 % 3 is 1 — what's left over after those 3 fits?
These two operators appear everywhere in real programs. % in particular is one of the most useful tools you'll reach for regularly.
Exponentiation
Raising a number to a power uses **:
Two asterisks, not one. * is multiplication. ** is power.
Order of Operations
Python follows the same precedence rules you learned in school: multiplication and division before addition and subtraction. When in doubt, use parentheses — they always take priority, and they also make your code easier to read.
If you are unsure about precedence, add parentheses. There is no penalty for being explicit, and future-you will be grateful.
String Operators
Operators aren't only for numbers. Two of them work on strings too.
Joining strings with +
+ with strings means join them together. This is called concatenation. Notice the space " " in the middle — strings are joined exactly as given, no spaces added automatically.
Repeating strings with *
* with a string and a number repeats the string that many times. Simple, but surprisingly handy.
Comparison Operators
Sometimes you don't want to calculate a result — you want to ask a question. Comparison operators do this. They always return a boolean: True or False.
The equality check is == — two equals signs. This is one of the most common mistakes for new programmers: writing = when you mean ==. Remember: = stores a value. == asks a question.
Combining Conditions
You can join multiple comparisons together using logical operators.
and— the whole expression isTrueonly if both sides areTrueor— the whole expression isTrueif at least one side isTruenot— flips the boolean:TruebecomesFalse,FalsebecomesTrue
These three words — and, or, not — are the building blocks of every decision a program makes. You'll use them constantly.
Expressions vs Statements
A quick distinction worth knowing now:
An expression is any piece of code that produces a value. 3 + 4 is an expression — it produces 7. age > 18 is an expression — it produces True or False. "Hi" * 3 is an expression — it produces "HiHiHi".
A statement is a complete instruction. print("hello") is a statement. name = "Priya" is a statement.
You'll often find expressions inside statements:
result = (price * quantity) + shipping
The right side is one big expression. Python evaluates it completely, then the assignment statement stores the result. This layering — expressions inside statements — is how all real programs are built.
Putting It Together
Read through this slowly. Every line uses something from this lesson or the previous one. There's no new concept here — just the existing ones working together. This is how programming always grows: small things combining into bigger things.
What You Have Learned
Operators let you act on values — calculate, combine, and compare.
The key ideas:
- Arithmetic:
+,-,*,/,//,%,**— each has a precise meaning /always returns a float;//returns a whole number+and*work on strings too — joining and repeating- Comparison operators (
==,!=,>,<,>=,<=) returnTrueorFalse ==asks a question;=stores a value — they are completely differentand,or,notcombine boolean values logically- An expression produces a value; a statement is a complete instruction
{item.op}
{item.desc}
))}