Unary and Binary Operators in Python
What are Unary and Binary Operators and why do we use them?
Python practitioners use unary and binary operators constantly and as you prepare for the PCEP exam it may be useful to know what these are.
A Unary Operator is a computational operator that takes any action on one operand and produces only one result.
For example, the “-” binary operator in Python turns the operand negative (if positive) and positive (if negative).
Example: x=56 y=-(x) print(y) >>-56
A Binary Operator is a computational operator that works with two or more operands.
Initial Example
For example, “*” multiplies both of its arguments together. There are seven binary operators that are used in Python3.
The Addition Operator
The “+” binary operator in Python adds two equal numerical values together.
The Subtraction Operator
The “-” binary operator in Python subtracts the first value from the second value. If the first value is negative, then the second is multiplied by -1. If the first value is positive, then it is added to its negated counterpart.
The Multiplication Operator
The “*” binary operator in Python multiplies both of its arguments together.
The Division Operator
The “/” binary operator in Python divides the first value by the second value and returns the quotient. If either argument is zero, an ArithmeticError is raised.
The Exponent Operator
The “**” binary operator in Python raises the number to the power of another number. For example, 2**10 means 2*2*2*2*2*2*2*2*2, which equals 256.
The Modulus (modulo) Operator
The “%” binary operator in Python returns the numerical sum of both operands.
The Floor Division Operator
The “//” binary operator in Python divides the first number by the second and returns an integer. The result is rounded down to the closest possible integer.
References
https://www.ibm.com/docs/en/itcam-app-mgr/7.2.0?topic=tesl-operators-expressions-1






