Operators and Operands
Explore how programming reshapes the way we approach problems and think about solutions through interactive Python examples.
Operators and Operands
What are operators in Python?
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
Symbol | Task Performed | Meaning | Example |
---|---|---|---|
+ | Addition | Add two operands or unary plus | x + y or +x |
- | Subtraction | Subtract right operand from the left or unary minus | x - y or -x |
* | Multiplication | Multiply two operands | x * y |
/ | Division | Divide left operand by right one (results in float) | x / y |
% | Modulus (remainder) | Remainder of division of left operand by the right | x % y |
// | Integer/Floor division | Division that results in whole number (floor) | x // y |
** | Exponentiation (power) | Left operand raised to the power of right | x ** y |
Comparison/Relational Operators
Comparison operators are used to compare values. They return True or False based on the condition.
Symbol | Task Performed | Meaning | Example |
---|---|---|---|
> | Greater than | True if left operand is greater than the right | a > b |
< | Less than | True if left operand is less than the right | a < b |
== | Equal to | True if both operands are equal | a == b |
!= | Not equal to | True if both operands are not equal | a != b |
>= | Greater than or equal to | True if left operand is greater than or equal | a >= b |
<= | Less than or equal to | True if left operand is less than or equal | a <= b |
Note:
The operator == checks for equality, whereas = is used for assignment.
Logical/Boolean Operators
Logical operators are the and, or, and not operators.
Symbol | Meaning | Example |
---|---|---|
and | True if both operands are true | x and y |
or | True if either operand is true | x or y |
not | True if operand is false (logical NOT) | not x |
Try Your Own Code
Use the cell below to experiment with Python code. Try creating your own variables and operations!