0% found this document useful (0 votes)
7 views16 pages

Operators in Python

The document provides an overview of operators in Python, categorizing them into arithmetic, relational, logical, assignment, ternary, and bitwise operators. It includes examples and explanations for each type of operator, along with sample programs demonstrating their usage. Additionally, the document features multiple-choice questions to test understanding of Python operators.

Uploaded by

lilzeeeforreal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Operators in Python

The document provides an overview of operators in Python, categorizing them into arithmetic, relational, logical, assignment, ternary, and bitwise operators. It includes examples and explanations for each type of operator, along with sample programs demonstrating their usage. Additionally, the document features multiple-choice questions to test understanding of Python operators.

Uploaded by

lilzeeeforreal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operators in Python

What is an Operator?
An operator is a symbol that performs a specific operation on one or more operands (values or variables).​
For example:

n
a = 10

ha
b=5
print(a + b) # + is an operator

ad
1. Arithmetic Operators
Used to perform mathematical operations.

Operator Description Example


Pr
Output
ka
+ Addition 10 + 5 15

- Subtraction 10 - 5 5
an

* Multiplication 10 * 5 50

/ Division (float) 10 / 5 2.0


iy

// Floor Division 10 // 3 3
Pr

% Modulus (Remainder) 10 % 3 1

** Exponent (Power) 2 ** 3 8
r.

Example Program:
D

a = 10
b=3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)

1
print("Modulus:", a % b)
print("Power:", a ** b)

2. Relational (Comparison) Operators


Used to compare two values and return True or False.

n
Operator Description Example Output

ha
== Equal to 5 == 5 True

!= Not equal to 5 != 3 True

ad
> Greater than 10 > 5 True

< Less than 2<8 True

>=

<=
Greater than or equal to

Less than or equal to


10 >= 10

4 <= 7
Pr
True

True
ka
Example Program:

a = 10
an

b=5

print(a == b)
iy

print(a != b)
print(a > b)
Pr

print(a < b)
print(a >= b)
print(a <= b)
r.
D

3. Logical (Boolean) Operators


Used to combine conditional statements.

Operator Description Example Output

and True if both are True (5 > 3) and (10 > 5) True

2
or True if at least one is True (5 < 3) or (10 > 5) True

not Reverses the result not(5 > 3) False

Example Program:

a = 10
b=5

n
print((a > b) and (b > 0))
print((a < b) or (b > 0))

ha
print(not(a == b))

ad
4. Assignment Operators
Used to assign values to variables.​

Operator Example Same As


Pr
Can also perform an operation and assign at the same time.
ka
= x=5 x=5

+= x += 3 x=x+3
an

-= x -= 2 x=x-2

*= x *= 4 x=x*4
iy

/= x /= 2 x=x/2
Pr

%= x %= 3 x=x%3

**= x **= 2 x = x ** 2
r.

//= x //= 3 x = x // 3
D

Example Program:

x = 10
x += 5
print("x after += 5:", x)

x *= 2
print("x after *= 2:", x)

3
x -= 4
print("x after -= 4:", x)

5. Ternary Operator (Conditional Expression)


Used to write if-else in a single line.

n
Syntax:

ha
value_if_true if condition else value_if_false

ad
Example Program:

a = 10
b = 20

min_value = a if a < b else b


print("Smaller number is:", min_value)
Pr
ka
an

6. Bitwise Operators
Used to perform bit-level operations.
iy

Operator Description Example Result (Binary)


Pr

& Bitwise AND 5&3 1 (0101 & 0011 → 0001)

| Bitwise OR 5|3 7 (0101 | 0011 → 0111)


r.

^ Bitwise XOR 5^3 6 (0101 ^ 0011 → 0110)


D

~ Bitwise NOT ~5 -6

<< Left Shift 5 << 1 10

>> Right Shift 5 >> 1 2

Example Program:

4
a=5
b=3

print("a & b =", a & b)


print("a | b =", a | b)
print("a ^ b =", a ^ b)
print("~a =", ~a)
print("a << 1 =", a << 1)
print("a >> 1 =", a >> 1)

n
ha
7. Increment and Decrement Operators

ad
Python does not have ++ or -- operators like other languages (C, Java).​
Instead, we use += and -=.

Example Program:

x = 10
x += 1 # increment by 1
Pr
ka
print("After increment:", x)

x -= 1 # decrement by 1
print("After decrement:", x)
an
iy
Pr

Operator Type Example

Arithmetic +, -, *, /, %, **, //
r.

Relational ==, !=, >, <, >=, <=


D

Logical and, or, not

Assignment =, +=, -=, *=, /=, %=

Ternary a if condition else b

Bitwise &, |, ^, ~, <<, >>

Increment/Decrement +=, -=

5
Python programs based on Operators in Python

1. Arithmetic Operator – Addition


a = 10

b=5

n
print("Sum =", a + b)

ha
2. Arithmetic Operator – Subtraction

ad
a = 10

b=5

print("Difference =", a - b) Pr
ka
3. Arithmetic Operator – Multiplication
an

a = 10

b=5
iy

print("Product =", a * b)
Pr

4. Arithmetic Operator – Division


r.
D

a = 10

b=2

print("Quotient =", a / b)

6
5. Arithmetic Operator – Modulus
a = 10

b=3

print("Remainder =", a % b)

n
6. Relational Operator – Greater Than

ha
a = 10

ad
b=5

print(a > b)

Pr
7. Relational Operator – Less Than
ka
a = 10

b = 20
an

print(a < b)
iy
Pr

8. Relational Operator – Equal To


a = 10
r.

b = 10
D

print(a == b)

9. Relational Operator – Not Equal To


a = 10

7
b=5

print(a != b)

10. Logical Operator – AND


a = 10

n
b=5

ha
print(a > 5 and b < 10)

ad
11. Logical Operator – OR
a = 10

b=5
Pr
ka
print(a > 20 or b < 10)
an

12. Logical Operator – NOT


iy

a = 10
Pr

print(not(a > 5))


r.

13. Assignment Operator


D

a = 10

a += 5

print("Value =", a)

8
14. Bitwise Operator – AND
a=5

b=3

print("Bitwise AND =", a & b)

n
15. Bitwise Operator – OR

ha
a=5

ad
b=3

print("Bitwise OR =", a | b)

16. Bitwise Operator – XOR


Pr
ka
a=5

b=3
an

print("Bitwise XOR =", a ^ b)


iy
Pr

17. Ternary Operator (Conditional Expression)


a = 10
r.

b=5
D

max = a if a > b else b

print("Maximum =", max)

18. Membership Operator – in

9
list1 = [1, 2, 3, 4, 5]

print(3 in list1)

19. Identity Operator – is


a = 10

n
b = 10

ha
print(a is b)

ad
20. Simple Calculator Using Operators
a = int(input("Enter first number: "))

b = int(input("Enter second number: "))


Pr
ka
print("Addition =", a + b)
an

print("Subtraction =", a - b)

print("Multiplication =", a * b)
iy

print("Division =", a / b)
Pr
r.
D

10
MCQs
1. Which operator is used for addition in Python?
A) *​
B) -​
C) +​

n
D) /​
Answer: C) +

ha
ad
2. Which operator gives the remainder of a division?
A) /​
B) %​
C) //​
D) **​
Pr
ka
Answer: B) %
an

3. What is the output of:


iy

print(10 // 3)
Pr

A) 3.3​
B) 3​
C) 1​
D) 0​
r.

Answer: B) 3
D

4. Which operator is used for exponentiation?


A) ^​
B) **​
C) //​

11
D) %​
Answer: B) **

5. Which operator checks if two values are equal?


A) =​
B) ==​

n
C) !=​

ha
D) <=​
Answer: B) ==

ad
6. Which logical operator returns True if both conditions are
True?
A) or​
Pr
ka
B) not​
C) and​
D) xor​
an

Answer: C) and
iy

7. What is the output of:


Pr

print(5 > 3 and 10 < 5)


r.

A) True​
B) False​
D

C) 0​
D) 1​
Answer: B) False

8. Which operator checks membership in a list?

12
A) is​
B) in​
C) not​
D) ==​
Answer: B) in

9. Which operator checks if two variables refer to the same

n
object?

ha
A) ==​
B) =​

ad
C) is​
D) !=​
Answer: C) is

Pr
ka
10. What is the output of:
print(not(10 > 5))
an

A) True​
B) False​
iy

C) 10​
D) Error​
Pr

Answer: B) False
r.

11. Which of the following is an assignment operator?


D

A) ==​
B) +=​
C) !=​
D) <=​
Answer: B) +=

13
12. What is the output of:
print(10 % 4)

A) 2​
B) 3​
C) 4​
D) 0​
Answer: A) 2

n
ha
13. Which operator is used for floor division?

ad
A) /​
B) %​
C) //​
D) **​
Answer: C) // Pr
ka

14. Which bitwise operator is used for AND?


an

A) &&​
B) &​
iy

C) |​
D) ^​
Pr

Answer: B) &
r.

15. What is the output of:


D

print(5 & 3)

A) 1​
B) 2​
C) 3​

14
D) 5​
Answer: A) 1

16. Which logical operator negates a condition?


A) and​
B) or​

n
C) not​

ha
D) !​
Answer: C) not

ad
17. What is the output of:
print(4 > 3 or 2 > 5)

A) True​
Pr
ka
B) False​
C) Error​
D) None​
an

Answer: A) True
iy

18. Which operator is used in Python’s ternary expression?


Pr

A) ?:​
B) if...else​
C) switch​
r.

D) ->​
D

Answer: B) if...else

19. Which of the following is NOT a Python operator?

15
A) +​
B) and​
C) <>​
D) is​
Answer: C) <>

20. What is the output of:

n
a=5

ha
b=5
print(a is b)

ad
A) True​
B) False​
C) Error​
D) None​
Answer: A) True Pr
ka
an
iy
Pr
r.
D

16

You might also like