Operators
The Operations being carried out on data are represented by operators. The Symbols that trigger the operation/action on data are called operators. The operations(specific tasks) are represented by Operators and the objects of the operation(s) are referred to as Operands.
Python's rich set of operators comprises of these types of operators :
(i) Arithmetic operators
(ii) Relational operators
(iii) Identity operators
(iv) Logical operators
(v) Bitwise operators
(vi) Membership operators
Out of these, we shall talk about membership operators later when we talk about strings/text, lists, and dictionaries.
In this post, we will discuss arithmetic operators and Unary operators in detail.Arithmetic Operators
To do arithmetic, Python uses arithmetic operators. Python provides operators for basic calculations, as given below :
+ Addition // Floor Division
- Subtraction % Remainder
* Multiplication ** Exponentiation
/ Division
Each of these operators is a binary operator i.e., it requires two values (operands) to calculate, a final answer. Apart from this binary operator, Python provides two unary arithmetic operators(that requires one operand) also, which are unary +, unary -.
Unary Operators
Unary +
The operator's unary '+' precedes an operand. The operand (the value on which the operators operate) of the unary + operators must have an arithmetic type and the result is the value of the argument. For example,
if a = 5 then + a means 5 .
if a = 0 then + a means 0.
if a = -4 then = a means -4.
Unary -
The operator's unary - precedes an operand. The operand of the unary - operator must have an arithmetic type and the result is the negation of its operand's value. For example,
if a = 5 then -a means -5.
if a = 0 then -a means 0 (there is no quantity known as -0)
if a = -4 then -a means 4.
Binary Operator
Operators that act upon two operands are referred to as Binary Operators. The operands of a binary operator are distinguished as the left or right operand. Together, the operator and its operands constitute an expression.
- Addition Operator +
For Example,
4 + 20 results in 24
a +5 (where a = 2) results in 7
a + b ( where a = 4, b = 6) results in 10
For Addition operator + operands may be of number types.
Python also offers + as a concatenation operator when used with strings, tuples, and lists. This functionality for strings will be covered in upcoming posts.
- Subtraction Operator -
For Example,
14 - 3 evaluates to 11
a - b (where a = 7, b = 5) evaluates to 2
x - 3 (where x = -1) evaluates to -4
The operand may be of number types.
- Multiplication Operator *
For Example,
3 * 4 evaluates to 12
b * 4 (where b = 6) evaluates to 24
p * 2 (where p = -5) evaluates to -10
a * c (where a = 3, c = 5) evaluates to 15
The operands may be of integer or floating-point number types.
Python also offers * as to replication operator when used with strings. This functionality will be covered in upcoming posts.
- Division Operator /
4/2 evaluates to 2.0
100/10 evaluates to 10.0
7/2.5 evaluates to 2.8
100/32 evaluates to 3.125
13.5/1.5 evaluates to 9.0
- Floor Division Operator //
- Modulus Operator %
- Exponentiation Operator **
So with this, we end with this Topic, don't forget to follow me so that you can get the latest updates of my new posts that I upload weekly, I will try my best to upload daily OR you can Sign up for Newsletters at the top of the Blog and that will give you E-mail updates of my New Post.
Jai Hind!!!!
No comments:
Post a Comment
If you have any doubts, please do let me Know.