Introduction to Unary Operators in Python
In this article, we will discuss on unary Operators in Python. An operator is a symbol that demonstrates a specific procedure is done. An operator in programming dialects is taken from mathematics. An operand is an information thing on which an operator act. Let’s take an example: +A (where + symbol is an operator) and A. An operator may have a couple of operands. An operand is one of the sources of info (contentions) of an operator. Those operators that work with just a single operand are called unary operators. Consider the function f: A → A, where A will be a set. The function f is a unary activity on A.
Examples of Different Unary Operators
Examples of unary operators in python are given below:
1. Unary Arithmetic Operator
This area clarifies the models (language structure) and semantics of all arithmetic operators in Python, utilizing its three numeric sorts: int, float, and complex.
Addition
The + operator in Python can be utilized in a unary form. The unary structure implies character, restoring the same value as its operand.
Example:
Prototype | Example |
+ (int) -> int | +4 returns the result 4 |
+ (float) -> float | +4.0 returns the result 4.0 |
+ (complex) -> complex | +4j returns the result 4j |
Code:
print +3
print +3.0
print +3j
Output:
Subtraction
The – operator in Python can be utilized in a unary form. The unary structure implies character, restoring the same value as its operand. The unary structure implies negate, restoring the nullified incentive as its operand: zero to zero, positive to negative, and negative to positive.
Example:
Prototype | Example |
– (int) -> int | -4 returns the result -4 |
– (float) -> float | -4.0 returns the result -4.0 |
– (complex) -> complex | -4j returns the result -4j |
Code:
print -4
print -4.0
print -4j
Output:
Multiplication
The * operator in Python can be utilized distinctly in the paired structure, which implies increase, restoring an outcome that is the standard arithmetic product result of its operands.
Example:
Prototype | Example |
* (int, int) -> int | 4* 5 returns the result 20 |
* (float, float) -> float | 4.0 * 5.0 returns the result 20.0 |
* (complex, complex) -> complex | 4j * 5j returns the result (-20+0j) |
Code:
print 3*5
print 3.0*5.0
print 3j*5j
Output:
Division
The / Operator(cut) in Python can be utilized distinctly in the parallel structure, which implies division, restoring an outcome that is the standard number juggling the remainder of its operands: left operand partitioned by the right operand.
Example:
Prototype | Example |
/ (int, int) -> int | 4 / 5 returns the result 0.8 |
/ (float, float) -> float | 4.0 / 5.0 returns the result 0.8 |
/ (complex, complex) -> complex | 4j / 5j returns the result (.08+0j) |
Code:
print 3/5
print 3.0/5.0
print 3j/5j
Output:
print 3/5
print 3.0/5.0
print 3j/5j
Floor Division
The // operator (twofold slice) in Python can be utilized as it were in the twofold structure, which additionally implies division, yet restoring a vital outcome of the standard number juggling the remainder of its operands: left operand partitioned by the right operand.
Example:
Prototype | Example |
// (int, int) -> int | 3 // 5 returns the result 0 |
// (float, float) -> float | 3.0 // 5.0 returns the result 0.0 |
Code:
print 3//5
print 3.0//5.0
Output:
Modulo
The % operator in Python can be utilized distinctly in the parallel structure, which implies the leftover portion after the left operand partitioned by the correct operand.
Model Example:
Prototype | Example |
% (int, int) -> int | 3%4 returns the result 0 |
% (float, float) -> float | 3.0%5.0 returns the result 3.0 |
Python Code:
print 3%5
print 3.0%5.0
Output:
Power
The ** operator in Python can be utilized distinctly in the double structure, which implies power restoring an outcome that is the left operand raised to the correct operand’s intensity.
Example:
Prototype | Example |
** (int, int) -> int | 3 ** 5 returns the result 243 |
** (float, float) -> float | 3.0 ** 5.0 returns the result 243.0 |
** (complex, complex) -> complex | 3j ** 5j returns the result (0.00027320084764143374-0.00027579525809376897j |
Python Code:
print 3**5
print 3.0**5.0
print 3j**5j
Output:
2. Boolean Operators
In Python, and, or and not are Boolean operators. With Boolean operators, we perform legitimate tasks. These are regularly utilized with if and while keywords.
Python Code:
print (True and True)
print (True and False)
print (False and True)
print (False and False)
Output:
3. Relational Operator
Relational operators used for comparing values. It returns a Boolean value.
Symbol | Meaning |
< | Less than |
<= | Less than equal to |
> | Greater than |
>= | Greater than equal to |
== | Equal to |
!= | Not equal to |
According to the above table Python relational operators’ examples:
Code:
print 3 < 4
print 4 == 3
print 4 >= 3
As we previously referenced, the social administrators return Boolean qualities: True or False.
Output:
4. Python Object Identity Operators
The object identity operators consist of is and not is; it checks if its operators are a similar item.
Let’s take an example.
Code:
print (Equals == Equals)
print (Equals is Equals)
print (Equals is Equals)
print ([] == [])
print ([] is [])
print ("Python" is "Python")
Output:
5. Python Bitwise Operators
Decimal numbers are normal for people. Double numbers are local to PCs. Double, octal, decimal, or hexadecimal images are just documentation of a similar number. Bitwise administrators work with bits of a double number.
Symbol | Meaning |
~ | negation |
^ | exclusive or |
& | and |
| | or |
<< | left shift |
>> | right shift |
The bitwise negation operator makes the changes to every 1 to 0 and 0 to 1.
Code:
print ~7
print ~-8
Output:
The administrator returns all bits of a number 7.
Conclusion
In this article, we have presented all the necessary unary as well as other operators used in python coding. We have learned about the assorted operators Python supports to consolidate objects into articulations. A large portion of the models you have seen so far have included just basic nuclear information.
Recommended Articles
This is a guide to Unary Operators in Python. Here we discuss the basic concept with various Unary Operators in Python along with different examples and code implementation. You may also look at the following articles to learn more –