Page 468 - ComputerScience_Class_11
P. 468
Operators can also be classified based on the number of operands they work on:
• Unary
• Binary
• Ternary
12.1.1 Unary
A unary operator in Python is a type of operator that works on a single operand to perform an operation. Common
unary operators include + (positive), - (negation) and not (logical NOT). They are used to change the sign of a number,
reverse a Boolean value or perform an operation on a single value, without needing a second operand.
Program 1: To display the output of Unary operator.
Program 1.py
File Edit Format Run Options Window Help
# Assign 5 to variable x
x = 5
# Unary minus operator: changes the sign of x, so y becomes -5
y = -x
# Assign Boolean value True to variable flag
flag = True
# Unary NOT operator: reverses the Boolean value of flag, so result becomes False
result = not flag
# Print the value of x
print("x =", x)
# Print the value of y (negated x)
print("y =", y)
# Print the value of flag
print("flag =", flag)
# Print the value of result (reversed flag)
print("result =", result)
Output
x = 5
y = -5
flag = True
result = False
466 Touchpad Computer Science (Ver. 3.0)-XI

