Python Operators

 Python Operators

                    An operator is a symbol that specifies an operation to be performed on the operands. The data items that operators act upon are called operands. Some operators require two operands called binary operators, while other act upon the operator. The operators usually form a part of mathematical or logical expressions.

Types of Operators

simplepycoder.pythonoperators

Arithmetic Operators

                    Arithmetic Operators carry basic arithmetic operators like addition, subtraction, multiplication, division.

Operators

Description

Example

 

+

 

Addition-Adds values on either side of the operator.

 

 

X + Y = 30

 

 -

 

Subtraction-Subtract right-hand operand from the left hand operand. 

 

 

X – Y = 10

 

 *

 

Multiplication-Multiplies values on either side of the operator.

 

 

X * Y = 200

 

 /

 

Division-Divides left-hand operand by right-hand operand. It returns always a float number.

 

 

Y / X = 2.0

 

 %

 

Modulus-Divides left-hand operand by right-hand operand and returns reminder.

 

 

Y % X = 0

 

 **

 

Exponent-Performs exponential (power) calculation on operators.

 

 

X ** Y = 10240000000000

 

 //

Floor Division – The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored.

 

X // Y = 2

Example

simplepycoder.arithmetic_operator

Comparision Operators or Relational Operation

                    Relational operators are used to comparing two or more operands. Operands may be variables, constants, or expressions. For example, we may compare the age of two persons, or the price of two items, and so on. These comparisons can be done with the help of relational operators.

 

Operators

 

 

Description

 

Example

 

==

 

If the values of two operands are equal, then the condition becomes true.

 

 

X ==Y    False

 

 

!=

 

If values of two operands are not equal, then condition becomes true

 

 

X != Y  True

 

 

> 

 

If the value of the left operand is greater than the value of right operand, then condition true

 

 

 

X > Y    False

 

 

< 

 

If the value of the left operand is less than the value of the right operand, then the condition becomes true.

 

 

 

X < Y   True

 

 

>=

 

If the value of the left operand is greater than the value of the right operand, then the condition becomes true.

 

 

 

X >= Y   False

 

 

 

<=

 

If the value of the left operand is less than or equal to the value of right operand, then condition becomes true.

 

 

 

X <= Y   True

Examples

simplepycoder.comparision_operator

Logical Operator

                    Logical operators are used to combining the result of two or more conditions. There are the following logical operators supported by Python language.

 

 Operator

 

 Description

 Example

 

and Logical AND

 

If both the operands are true then the condition becomes true.

 

 

 ( X and Y )  is true.

 

or Logical OR

 

If any of the two operands are true then the condition becomes true.

 

 

( X or Y )  is true.

 

Not Logical NOT

 

To reverse the logical state of its operand.

 

 

Not ( X and Y )  is false













Example

simplepycoder.logic_operator

simplepycoder.logic_operator



Assignment Operator

                    Assignment operators are used to assigning a value or an expression or a value of a variable to another variable.
 

Operator

Description

Example

=

 

Assign values from the right side operands to left side operand.

 

z = x + y assigns value of

 x +y into z

+=   Add

 

It adds the right operand to the left operand and assign the result to the left operand.

 

y += x  is equivalent to

 y =  y + x

-=  Subtract

 

It subtracts the right operand from the left operand and assign the result to the left operand.

 

y -= x  is equivalent to

 y = y – x

 

*=  Multiply

 

It multiplies the right operand with the left operand and assign the result to the left operand.

 

Y *= x is equivalent to

 y = y * x

/= Divide

 

It divides left operand with the right operand and assign the result to the left operand.

 

y /= x is equivalent to

y = y / x

%= Modulus

 

It takes modulus using two operands and assign the result to the left operand.

 

y %= x is equivalent to

y = y % x

**= Exponent

 

Performs exponential calculation on operators and assign value to the left operand.

 

y **= x is equivalent to

 y = y ** x

//=  Floor Division

 

It performs floor division on operators and assign value to the left operand.

 

y //= x is equivalent to

 y = y // x


Example

simplepycoder.assignment_operator





Membership Operators

                    Python's membership operators test for membership in a sequence, such as strings, lists, or tuples. 

 

Operator

 

 

Description

 

Example

in

 

Evaluates to true if it finds a variable in the specified sequence and false otherwise.

 

 

x in y, If the x is a member of sequence y, then the result is true.

not in

 

Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

 

 

x not in y, If the x is not a member of sequence y, then the result is true.

Examples

simplepycoder.membership_operator







Bitwise Operators

                    Bitwise operators are used to manipulating the data at the bit level. It operates on integers only. Bitwise operators act on operands as if they were strings of binary digits. It operators bit by bit, hence the name.

 

Operator

 

Description

Example

&

 

Binary AND – Operator copies a bit to the result if it exists in both operands.

 

( x & y ) 

(means  0000  1100)

|

 

Binary OR – It copies a bit if it exists in either operand.

 

( x | y) = 61

 (means 0011  1101)

^

 

Binary XOR – It copies the bit if it is set in one operand but not both.

 

( x ^ y ) = 49

(means 0011  0001)

~

 

Binary Ones Complement – It is unary and has the effect of ‘fliping’ bits.

 

(~ x) = -61

(means  1100 0011 in 2’s complement form due to a signed binary number.)

<< 

 

Binary Left Shift – The left operands value is moved left by the number of bits specified by the right operand.

 

x << = 240

(means  1111  0000)

>> 

 

Binary Right Shift – The left operands value is moved right by the number of bits specified by the right operand.

 

x >>= 15 

(means  0000  1111)















































Examples

simplepycoder.bitwise_operator







Identity Operator

                    Identity operators compare the memory locations of two objects. They are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.

Operator

Description

 

Example

 

is

 

Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

 

x is y,  If id(x) equals id(y), then the result is True.

is not

 

Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

 

X is not y If id(x)  is not equal to id(y), then the result is True.

Example

simplepycoder.identity_operator






Unary Arithmetic Operator

                   1.   All unary operations have the same priority.
                            The unary '-' operator yields the negation of its numeric argument.

simplepycoder.unary_operator

               2.   The unary '+' operator yields its numeric argument unchanged.

simplepycoder.unary_operator

If the argument does not have the proper type, a TypeError exception is raised

0/Post a Comment/Comments

Previous Post Next Post