Python Operators
Let's start with new session and new topic in python, i.e., Python Operators.
Before starting in detail it is important to understand answers for some questions:
Here we have completed Python Operators, hope you have understood in details.
If any queries mail me at abolikhedkar@gmail.com
Thank You...
Before starting in detail it is important to understand answers for some questions:
- What operators means?
- Why to use them?
- How to use them?
- What are their types?
We will be covering answers to all this questions with few examples so that it will be easy for you to understand operators in deep and have fun while programming.
So let's begin....
What operators means?
The general purpose of a program is to accept data from user and to perform some operations on them. The data in the program is saved into variables. To perform this operations Operators are required.
In other words operators are the symbol that performs operations on the variables which are also called as operands.
In other words operators are the symbol that performs operations on the variables which are also called as operands.
Why to use them?
Using this operator, writing the program and performing actions becomes very easy. It also makes your complex program simple.
How to use them?
Using any operator is very simple, in detail we will discuss further. As said they are symbols used to perform an operations on variable. For example, if you want to provide addition of variable A and B, operator Symbol used is '+' and equation becomes 'A + B'.
What are their types?
- Based on their actions their actions operators are classified as Unary Operator, Binary Operator and Ternary Operator.
- Unary Operator : If the operator acts on single variable it is called as Unary Operator.
- Binary Operator : If the operator acts on two variable it is called as Binary Operator.
- Ternary Operator : If the operator acts on two variable it is called as Binary Operator.
Operator Classifications depending on their nature:
- Arithmetic Operators
- Assignment Operators
- Unary Operators
- Relational Operators
- Logical operators
- Boolean Operators
- Bitwise Operators
I hope you may have got answers to all of your questions. Even if you don't understand anything you can contact Me.
Now we will see each Operator Types in detail.
Arithmetic Operators
·
The Arithmetic Operators are used to perform
operations like addition, subtraction, division, multiplication etc.
·
These operator acts on two operands, so they are
called as “Binary Operators”.
Arithmetic Operators in Python:
Operator
|
Meaning
|
Example
|
+
|
Addition Operator. It adds two given operands
|
A + B
|
-
|
Subtraction Operator. Subtracts one value from another.
|
A - B
|
*
|
Multiplication Operator. Multiplies two given operands.
|
A * B
|
/
|
Division Operator. Divides left operand with right operand and gives
value of quotient.
|
A / B
|
%
|
Modulus Operator. Divides left operand with right operand and gives
value of remainder.
|
A % B
|
**
|
Exponent Operator. Calculates exponential power value. Value of a to
the power of b.
|
A ** B
|
//
|
Integer Division Operator. It is also called as Floor division, it
gives only integer quotient.
|
A // B
|
Order of evaluation of an expression:
- Parentheses are evaluated.
- Exponentiation is done next.
- Multiplication, Division, Modulus and Integer Division has same priority.
- Addition and Subtraction is done next.
- Finally assignment operation is performed.
Assignment Operators
·
These operators are useful to store the right
sight value into a left side variable.
Assignment Operators in Python:
Operator
|
Meaning
|
Example
|
=
|
Assignment Operator, stores right side value into left side variable
|
Z=X+Y
|
+=
|
Addition assignment Operator. Adds right side operand to left side
operand and stores the result in left side operand.
|
Z+=X =>
Z = Z+X
|
-=
|
Subtraction assignment Operator. Subtracts right side operand to left
side operand and stores the result in left side operand.
|
Z-=X =>
Z = Z-X
|
*=
|
Multiplication assignment Operator. Multiplies right side operand to
left side operand and stores the result in left side operand.
|
Z*=X =>
Z = Z*X
|
/=
|
Division assignment Operator. Divides right side operand to left side
operand and stores the quotient result in left side operand.
|
Z/=X =>
Z = Z/X
|
%=
|
Modulus assignment Operator. Divides right side operand to left side
operand and stores the remainder in left side operand.
|
Z%=X =>
Z = Z%X
|
**=
|
Exponentiation assignment Operator. Left side operand power of right
side operand and stores the result in left side operand.
|
Z**=X =>
Z = Z**X
|
//=
|
Floor division assignment Operator. Performs floor division and
stores the result in left side operand.
|
Z//=X =>
Z = Z//X
|
Unary Minus Operator
- It is denoted by symbol minus (-).
- When this operator is used before a variable, its value is negated.
- Example :
n = 10
print(-n) # display -10
Relational Operator
- It is used to compare two quantities.
- Depending on the values compared the result will be True or False.
Operator
|
Meaning
|
Example
|
>
|
Greater than Operator. If A is Greater than B, the result is True
otherwise is False
|
A > B
|
>=
|
Greater than or Equal Operator. If A is Greater than or Equal to B,
the result is True otherwise is False
|
A >= B
|
<
|
Less than Operator. If A is Less than B, the result is True otherwise
is False
|
A < B
|
<=
|
Less than or Equal Operator. If A is Less than or Equal to B, the
result is True otherwise is False
|
A <= B
|
==
|
Equals Operator. If A is Equal to B, the result is True otherwise is False
|
A == B
|
!=
|
Not Equals Operator. If A is Not Equal to B, the result is True
otherwise is False
|
A != B
|
Logical Operators
- This operators are used to construct compound conditions.
- A compound condition of more than one simple condition.
- Each of simple condition is evaluated to True or False and then the decision is taken to know weather the total condition is True or False.
- False indicates 0 and True indicates any other number.
Operator
|
Meaning
|
Example
|
and
|
And Operator. If A is False, it returns A, otherwise it returns B.
|
A and B
|
or
|
Or Operator. If A is False, it returns B, otherwise it returns A.
|
A or B
|
not
|
Not Operator. If A is False, it returns True, otherwise it returns
False.
|
not A
|
Boolean Operators
- It act upon 'bool' type literals and they provide 'bool' type output.
Operator
|
Meaning
|
Example
|
and
|
Boolean and Operator. If both A and B are True, it returns True,
otherwise it returns False.
|
A and B
|
or
|
Boolean or Operator. If either A or B is True, it returns True,
otherwise it returns False.
|
A or B
|
not
|
Boolean not Operator. If A is False, it returns True, otherwise it
returns False.
|
not A
|
Bitwise Operators
- These operators acts on individual bits i.e; 0 and 1 of the operand.
- This operators can be used directly on binary numbers or on an integers also.
- When used on integer, the number is converted into bits (binary form) and then bitwise operators acts on those bits.
- The result given is also in the form of integer.
Types of Bitwise Operators
- Bitwise Complement Operator (~)
- Bitwise AND Operator (&)
- Bitwise OR Operator (|)
- Bitwise XOR Operator (^)
- Bitwise Left Shift Operator (<<)
- Bitwise Right Shift Operator (>>)
Symbol
|
Pronunciation
|
Gate
|
Truth Table
|
Meaning
|
|||||||||||||||
~
|
Tilde
|
NOT
|
|
It gives complement form of given number
|
|||||||||||||||
&
|
Ampersand
|
AND
|
|
It performs AND operation on the individual bits of given number.
Follow the truth table for output.
|
|||||||||||||||
|
|
Pipe
|
OR
|
|
It performs OR operation on the individual bits of given number.
Follow the truth table for output.
|
|||||||||||||||
^
|
Circumflex
|
XOR
|
|
It performs XOR operation on the individual bits of given number.
Follow the truth table for output.
|
|||||||||||||||
<<
|
Double less than
|
--
|
This operator shifts the bits of the number towards left a specified
number of positions. Remaining position is filled with zeros, as shown in
table. Some bits are lost in this operation.
|
||||||||||||||||
>>
|
Double greater than
|
--
|
This operator shifts the bits of the number towards Right a specified
number of positions. Remaining position is filled with zeros, as shown in
table. Some bits are lost in this operation.
|
Here we have completed Python Operators, hope you have understood in details.
If any queries mail me at abolikhedkar@gmail.com
Thank You...
Comments
Post a Comment