Chapter 15: Operators

Part 1: What Are Operators?

An operator is a symbol that represents a specific action or operation to be performed on one or more values, which we call operands.

  • Operand: The values or variables that the operator acts upon. (e.g., in 5 + 35 and 3 are operands).

  • Operator: The symbol that defines the action (e.g., + is the operator).

The combination of operands and operators forms an expression, which evaluates to a single value.

python

Part 2: The Main Families of Operators

We can group operators into several key families based on what they do. Let’s explore each one with examples in Python.

Family 1: Arithmetic Operators – The Mathematicians

These are the operators you’re most familiar with from math class. They perform basic mathematical calculations on numeric operands.

Operator Name Description Example Result
+ Addition Adds two operands 5 + 3 8
- Subtraction Subtracts the right operand from the left 10 - 4 6
* Multiplication Multiplies two operands 6 * 7 42
/ Division Divides the left operand by the right (always returns a float) 15 / 4 3.75
// Floor Division Divides and returns the largest whole number (integer) less than or equal to the result 15 // 4 3
% Modulo (Remainder) Divides and returns the remainder 15 % 4 3
** Exponentiation Raises the left operand to the power of the right 2 ** 3 8
python

Instructor’s Note: Pay special attention to // and %. They are incredibly useful. Modulo (%) is perfect for checking if a number is even (number % 2 == 0), cycling through a range, or many other tasks.

Family 2: Assignment Operators – The Store Keepers

These operators are used to assign values to variables. The most basic is the = sign, but it’s often combined with arithmetic operators to create shortcuts.

Operator Name Description Example Equivalent To
= Assignment Assigns the value on the right to the variable on the left x = 5 x = 5
+= Add and Assign Adds the right operand to the left and assigns the result to the left x += 3 x = x + 3
-= Subtract and Assign Subtracts the right operand from the left and assigns the result x -= 4 x = x - 4
*= Multiply and Assign Multiplies the left operand by the right and assigns the result x *= 2 x = x * 2
/= Divide and Assign Divides the left operand by the right and assigns the result x /= 3 x = x / 3
//= Floor Divide and Assign Performs floor division and assigns the result x //= 2 x = x // 2
%= Modulo and Assign Takes the modulo and assigns the result x %= 3 x = x % 3
**= Exponentiate and Assign Raises the left operand to the power of the right and assigns the result x **= 2 x = x ** 2
python

Family 3: Comparison Operators – The Judges

These operators compare two values and always return a boolean result: True or False. They are the foundation of decision-making in programs.

Operator Name Description Example Result
== Equal to True if operands are equal 5 == 5 True
!= Not equal to True if operands are not equal 5 != 3 True
> Greater than True if left is greater than right 7 > 5 True
< Less than True if left is less than right 7 < 5 False
>= Greater than or equal to True if left is greater than or equal to right 5 >= 5 True
<= Less than or equal to True if left is less than or equal to right 3 <= 5 True
python

Family 4: Logical Operators – The Decision Makers

These operators combine or invert boolean values. They allow you to build complex conditions from simpler ones.

Operator Name Description Example Result
and Logical AND True if both operands are True (5 > 3) and (10 < 20) True
or Logical OR True if at least one operand is True (5 > 3) or (10 > 20) True
not Logical NOT Inverts the boolean value: True becomes False and vice versa not (5 > 3) False
python

Family 5: Identity Operators – The Twin Finders

These operators are special to Python. They don’t compare if two variables have the same value (use == for that). They compare if two variables refer to the exact same object in memory.

Operator Name Description Example
is Identity True if both operands refer to the same object x is y
is not Negated Identity True if they refer to different objects x is not y

This is subtle but very important. Two lists can have the exact same contents (== would be True), but if they were created separately, they are different objects in memory (is would be False).

python

When to use is: Primarily for comparing with the single value None.

python

Family 6: Membership Operators – The Search Party

These operators test whether a value exists within a sequence, like a string, list, or tuple.

Operator Name Description Example Result
in Membership True if the value is found in the sequence 'a' in 'apple' True
not in Negated Membership True if the value is not found 'z' not in 'apple' True
python

Part 3: Operator Precedence (The Order of Operations)

Just like in mathematics, operators have a defined precedence that determines the order in which they are evaluated in a complex expression. You probably remember “Please Excuse My Dear Aunt Sally” (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). Programming is similar, but with more operators.

The general rule of thumb: When in doubt, use parentheses () to make your intention clear to both the computer and anyone reading your code.

python

Part 4: Putting It All Together – A Practical Example

Let’s build a small program that checks if a user is eligible for a special discount. This combines assignment, arithmetic, comparison, and logical operators.

python

This example shows how operators are not just isolated symbols but the glue that holds the logic of our programs together.

Summary: The Operator Philosophy

  • Operators are the action symbols of programming. They perform operations on data.

  • Arithmetic Operators (+-*///%**) do math.

  • Assignment Operators (=+=-=, etc.) store values.

  • Comparison Operators (==!=><, etc.) create logic by producing True/False.

  • Logical Operators (andornot) combine these boolean values for complex decisions.

  • Identity Operators (isis not) check if two things are the exact same object.

  • Membership Operators (innot in) check for existence in a sequence.

  • Operator Precedence determines the order of evaluation. Use parentheses () to be clear and safe.

Mastering operators is like learning the verbs of a language. Without them, you can have all the nouns (data) you want, but you can’t make a sentence (a program). Practice using them in different combinations, and they will soon become second nature.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *