03 Operators, Keywords & Variables
03 Operators, Keywords & Variables
Operators in
Python
A COMPREHENSIVE GUIDE TO OPERATORS AND OPERANDS WITH
INTERACTIVE EXERCISES
Repo(3)
Understanding Operators and
Operands in Python
When you're working with Python, you'll frequently encounter the concepts of operators and
operands. These are fundamental to performing calculations and logical operations in your
code.
What is an Operator?
An operator is a symbol or keyword that instructs Python to perform a specific operation,
such as addition, subtraction, or comparison. Here are a few examples of operators:
+: Addition
-: Subtraction
***: Multiplication
==: Comparison
not: Logical NOT
What is an Operand?
An operand is the value or variable upon which an operator acts. In the expression a + b, a and
b are operands, and + is the operator. Here's a simple example:
a=5
b=3
c=a+b
In this example, 5 and 3 are operands, and + is the operator, resulting in c being 8.
1. Unary Operators
Negation: -a
Logical NOT: not True results in False
Bitwise NOT: ~5 results in -6
2. Binary Operators
Arithmetic: +, -, *, /, //, %, **
Comparison: ==, !=, >, <, >=, <=
Logical: and, or
Assignment: =, +=, -=, etc.
3. Logical Operators
4. Assignment Operators
=: Assign value
+=: x += 3 is shorthand for x = x + 3
-=, *=, /=, //=: Similar shorthand for other operations
5. Identity Operators
Identity operators check if two variables refer to the same object in memory:
6. Membership Operators
Summary Table
Here's a quick reference of operator types and examples:
Type Examples
Arithmetic +, -, *, /, **
Assignment =, +=, -=
MCQ 1
MCQ 2
A. +
B. -
C. *
D. //
✔️ Answer: B. -
Explanation: Unary operators operate on a single operand. -x negates the value of x.
MCQ 3
x=5
y = ~x
print(y)
A. 5
B. -5
C. -6
D. 6
✔️ Answer: C. -6
Explanation: ~x is a bitwise NOT operator. It inverts all bits of x. ~5 gives -6 due to two’s
complement representation.
MCQ 4
A. binary()
B. int()
C. str()
D. bin()
✔️ Answer: D. bin()
Explanation: The bin() function returns the binary representation of a number as a string
prefixed with '0b'.
MCQ 5