Operators
Introduction to Python
Getting Started
Comments
Variables
Data Types
Type Casting
Input and Output
Operators
Strings
Booleans
Conditional Statements
While Loop
For Loop
Loop Control Statements
Lists
Tuples
Sets
Dictionaries
Functions
Function Arguments and Return Values
Variable Scope
Recursion
Lambda Functions
Arrays and Python Lists
Modules
Dates and Math
String Formatting
File Handling
Try and Except
Classes and Objects
Inheritance
Iterators
JSON
RegEx
Operators
Definition: Operators are special symbols in Python that are used to perform calculations, compare values, and manipulate data.
Why: Operators are the building blocks of logic in any program. They allow you to perform math, make comparisons, and combine logical conditions to control how your code behaves.
Main Operator Groups
- Arithmetic Operators: Used for mathematical calculations:
+(Add),-(Subtract),*(Multiply),/(Divide),%(Modulus),**(Exponent),//(Floor Division). - Comparison Operators: Used to compare two values:
==(Equal),!=(Not Equal),>(Greater Than),<(Less Than),>=,<=. - Logical Operators: Used to combine conditional statements:
and,or,not. - Assignment Operators: Used to assign values to variables:
=,+=,-=,*=,/=.
Example
x = 10
y = 3
print(x + y) # Arithmetic: 13
print(x / y) # Arithmetic: 3.3333...
print(x % y) # Arithmetic (Remainder): 1
print(x > y) # Comparison: True
print(x == y) # Comparison: False
Explanation
x + yadds the two numbers together.x / yperforms a standard division, resulting in a float.x % y(Modulus) returns the remainder left over after division.x > ychecks if 10 is greater than 3, which results in a Boolean value (True).
Key Notes
- The
=operator is for assignment (setting a value), while==is for comparison (checking if values are equal). - Floor division (
//) rounds the result down to the nearest whole number. - Logical
andonly returnsTrueif both conditions being checked are true.
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute