Welcome to the Python Assignment Operators Quiz! Test your understanding of essential operators like +=, -=, *=, and more, which empower you to perform operations and assignments in a single line. Strengthen your Python skills with practical examples and discover the efficiency of concise coding.
Python assignment operators are used to assign values to variables with an operation in a single statement. These operators combine the assignment (=) operator with another arithmetic or bitwise operator to perform the operation and assignment at the same time. The commonly used assignment operators in Python include +=, -=, *=, /=, //=, %=, **=, &=, |=, ^=, and <<=, and >>=. These operators help to write concise and efficient code by reducing the number of lines required to perform arithmetic or bitwise operations on variables.
Here are some examples of all Python Assignment Operators:
x = 5 x += 3 # equivalent to x = x + 3 print(x) # Output: 8
Example2:
x = 10 x += 5 print(x) # Output: 15 y = "Hello" y += " World" print(y) # Output: "Hello World"
x = 10 x -= 4 # equivalent to x = x - 4 print(x) # Output: 6
x = 3 x *= 5 # equivalent to x = x * 5 print(x) # Output: 15
Example2:
x = 5 x *= 3 print(x) # Output: 15 y = "Hi " y *= 3 print(y) # Output: "Hi Hi Hi "
x = 10 x /= 2 # equivalent to x = x / 2 print(x) # Output: 5.0 (note that division operator always returns a float)
x = 10 x //= 3 # equivalent to x = x // 3 print(x) # Output: 3
x = 10 x %= 3 # equivalent to x = x % 3 print(x) # Output: 1
x = 3 x **= 4 # equivalent to x = x ** 4 print(x) # Output: 81
x = 10 x &= 3 # equivalent to x = x & 3 print(x) # Output: 2
x = 10 x |= 3 # equivalent to x = x | 3 print(x) # Output: 11
x = 10 x ^= 3 # equivalent to x = x ^ 3 print(x) # Output: 9
x = 10 x <<= 2 # equivalent to x = x << 2 print(x) # Output: 40
x = 10 x >>= 2 # equivalent to x = x >> 2 print(x) # Output: 2
x = 5 x += 3 print(x)
a. Subtraction Assignment
b. Addition Assignment
c. Multiplication Assignment
d. Division Assignment
a. //=
b. /=
c. %=
d. **=
y = "Hello" y += " World" print(y)
a. “World”
b. “Hello World”
c. “Hello”
d. ” World”
x = 10
x **= 3
print(x)
a. Exponentiation Assignment
b. Bitwise AND Assignment
c. Bitwise XOR Assignment
d. Left Shift Assignment
a. |=
b. &=
c. ^=
d. >>=
x = 10 x >>= 2 print(x)
a. 5
b. 2
c. 10
d. 20
a. x = x % 5
b. x = x ** 2
c. x = x & 3
d. x = x << 2
a. Avoid using them as they may lead to errors
b. Use them without considering the result
c. Ensure the result of the operation is as intended
d. They are only suitable for simple operations
Answers:
1-b. Addition Assignment
2-a. //=
3-b. “Hello World”
4-a. Exponentiation Assignment
5-a. |=
6-b. 2
7-a. x = x % 5
8-c. Ensure the result of the operation is as intended
x = 7
x &= 3
print(x)
a. 3
b. 1
c. 7
d. 2
a. <<=
b. >>=
c. ^=
d. |=
y = “Hi “
y *= 3
print(y)
a. Addition Assignment
b. Multiplication Assignment
c. Division Assignment
d. Modulus Assignment
a. &=
b. ^=
c. |=
d. >>=
x = 20
x //= 3
print(x)
a. 5
b. 6
c. 7
d. 2
a. They make the code longer and more complex
b. They can only be used with simple operations
c. They combine an operation and assignment in a single
statement, making the code concise
d. They are not recommended for efficient coding
a. Using them without understanding their functionality
b. Avoiding them altogether
c. Using them only for complex operations
d. Ensuring the result of the operation is what you intend
Answers:
1-b. 1
2-a. <<=
3-b. Multiplication Assignment
4-b. ^=
5-a. 5
6-c. They combine an operation and assignment in a single
statement, making the code concise
7-d. Ensuring the result of the operation is what you intend
*****
x = 15
x <<= 3
a. Right Shift Assignment by 3
b. Left Shift Assignment by 3
c. Bitwise AND Assignment by 3
d. Bitwise XOR Assignment by 3
x = 8
x %= 3
print(x)
a. 2
b. 0
c. 1
d. 3
a. /=
b. //=
c. %
d. &=
y = “Good”
y += ” Morning”
a. Subtraction Assignment
b. Addition Assignment
c. Multiplication Assignment
d. Division Assignment
a. They make the code longer and more complex
b. They are only suitable for simple operations
c. They combine an operation and assignment, reducing the number
of lines needed
d. They can only be used with integer variables
a. It doesn’t matter as Python automatically handles it
b. The result is always correct
c. To avoid unintended behavior or errors
d. It is not necessary; Python Assignment Operators are
foolproof
1-b. Left Shift Assignment by 3
2-c. 1
3-a. /=
4-b. Addition Assignment
5-c. They combine an operation and assignment, reducing the number
of lines needed
6-c. To avoid unintended behavior or errors
x = 12
x ^= 9
a. Left Shift Assignment by 9
b. Bitwise XOR Assignment by 9
c. Right Shift Assignment by 9
d. Addition Assignment by 9
a. &=
b. |=
c. ^=
d. <<=
x = 25
x >>= 2
print(x)
a. 10
b. 25
c. 6
d. 2
a. To make the code longer and more complex
b. They are only suitable for simple operations
c. To perform an operation and assign the result in a single
statement
d. To use them exclusively with strings
y = “Nice”
y *= 2
a. Division Assignment
b. Modulus Assignment
c. Multiplication Assignment
d. Right Shift Assignment
a. They are only applicable to integer variables
b. Their order doesn’t matter in the statement
c. Ensuring the result of the operation is as intended
d. They can only be used with boolean variables
1-b. Bitwise XOR Assignment by 9
2-b. |=
3-c. 6
4-c. To perform an operation and assign the result in a single
statement
5-c. Multiplication Assignment
6-c. Ensuring the result of the operation is as intended