Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jcomte23/Python_vanilla/llms.txt
Use this file to discover all available pages before exploring further.
Introduction to Operators
Operators are special symbols in Python that perform operations on values and variables. Python supports various types of operators including arithmetic, comparison, and assignment operators.Arithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values.Basic Arithmetic Operations
Division (
/) always returns a float, even when dividing evenly. Use // for integer division.Advanced Arithmetic Operations
Arithmetic Operators Summary
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 4 + 2 | 6 |
- | Subtraction | 4 - 2 | 2 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 4 / 2 | 2.0 |
% | Modulo | 13 % 5 | 3 |
** | Exponentiation | 2 ** 8 | 256 |
// | Floor Division | 7 // 2 | 3 |
Assignment Operators
Assignment operators are used to assign and modify variable values.Increment Operations
Decrement Operations
Assignment Operators Summary
| Operator | Example | Equivalent To |
|---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
**= | x **= 3 | x = x ** 3 |
Comparison Operators
Comparison operators compare two values and return a boolean result (True or False).
Equality Comparison
The equality operator (
==) checks both value and type. 2 == "2" is False because one is an integer and the other is a string.Inequality Comparison
Greater Than and Less Than
Greater/Less Than or Equal To
Comparison Operators Summary
| Operator | Name | Example | Result |
|---|---|---|---|
== | Equal to | 10 == 10 | True |
!= | Not equal to | 10 != 14 | True |
> | Greater than | 20 > 30 | False |
< | Less than | 20 < 30 | True |
>= | Greater than or equal | 20 >= 20 | True |
<= | Less than or equal | 20 <= 30 | True |
Practical Examples
Example 1: Calculator Operations
Example 2: Password Validation
Example 3: Counter Updates
Type Checking with isinstance()
You can check variable types using comparison withisinstance():
isinstance() is the preferred way to check types in Python. It’s more flexible than type() as it considers inheritance.Key Takeaways
- Arithmetic operators perform mathematical calculations
- Assignment operators modify variable values in place
- Comparison operators return boolean values (
TrueorFalse) - Python is type-sensitive in comparisons:
2 == "2"isFalse - Use
+=and-=for incrementing/decrementing (Python has no++or--) - Division (
/) always returns a float, even for whole number results