0% found this document useful (0 votes)
3 views

03 Operators, Keywords & Variables

The document provides a comprehensive guide to operators and operands in Python, detailing their definitions and types, including unary, binary, logical, assignment, identity, and membership operators. It includes examples and a summary table for quick reference, as well as multiple-choice questions to test understanding of the concepts. The content is designed to enhance the reader's knowledge of performing calculations and logical operations in Python.

Uploaded by

Rehan Sabir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

03 Operators, Keywords & Variables

The document provides a comprehensive guide to operators and operands in Python, detailing their definitions and types, including unary, binary, logical, assignment, identity, and membership operators. It includes examples and a summary table for quick reference, as well as multiple-choice questions to test understanding of the concepts. The content is designed to enhance the reader's knowledge of performing calculations and logical operations in Python.

Uploaded by

Rehan Sabir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Understanding

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.

Types of Operators in Python


Python supports several types of operators, each with its specific use cases:

1. Unary Operators

Unary operators work with a single operand. Examples include:

Negation: -a
Logical NOT: not True results in False
Bitwise NOT: ~5 results in -6

2. Binary Operators

Binary operators require two operands. They include:

Arithmetic: +, -, *, /, //, %, **
Comparison: ==, !=, >, <, >=, <=
Logical: and, or
Assignment: =, +=, -=, etc.

3. Logical Operators

Logical operators are used to combine boolean conditions:

and: True if both operands are True


or: True if at least one operand is True
not: Reverses the boolean value

4. Assignment Operators

These operators assign values and can update variables:

=: 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:

is: True if both refer to the same memory location


is not: True if they refer to different memory locations

6. Membership Operators

These operators check for membership in sequences like lists or strings:

in: True if a value exists in the sequence


not in: True if a value does not exist in the sequence

Summary Table
Here's a quick reference of operator types and examples:

Type Examples

Unary -x, not x, ~x

Binary a + b, a > b, x and y

Arithmetic +, -, *, /, **

Comparison ==, !=, >, <

Logical and, or, not

Assignment =, +=, -=

Identity is, is not

Membership in, not in

Multiple Choice Questions (MCQs)


Now, let's test your understanding with some MCQs:

MCQ 1

Q: What is an operand in Python?

A. A special keyword used in loops


B. A value or variable on which an operator acts
C. A symbol that performs an operation
D. A built-in Python function

✔️ Answer: B. A value or variable on which an operator acts


Explanation: An operand is the input for an operator. For example, in 5 + 3, both 5 and 3 are
operands.

MCQ 2

Q: Which of the following is a unary operator?

A. +
B. -
C. *
D. //

✔️ Answer: B. -
Explanation: Unary operators operate on a single operand. -x negates the value of x.

MCQ 3

Q: What will be the output of the following code?

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

Q: Which function is used to convert an integer to a binary string in Python?

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

Q: What does the // operator do in Python?

A. Divides and gives float result


B. Finds the remainder
C. Divides and returns the integer part
D. Exponentiation
✔️ Answer: C. Divides and returns the integer part
Explanation: // is the floor division operator. It removes the decimal part of the result.

Would you like more MCQs or explanations on other Python concepts?

You might also like