WEB DEVELOPER SITE

Python Operator Basic

Previous chapter you study about string In this chapter you learn about Operator

Operator are used to operations on variables and value

If consider 3 + 2 = 5 WHERE 3 and 2 are called operands and + is called operator.

Python types of Operator:

Following types are listed

• Arithmetic Operators
• Logical Operators
• Assignment Operators
• Comparison (Relational) Operators
• Bitwise Operators
• Identity Operators
• Membership Operators

Python Arithmetic Operators

Python arithmetic operators are listed following:

assume x = 30 and y = 10

Operator Description example
addition + add both side or either side of operand x+y = 40
subtractions - subtract from right side to left side operand x - y= -20
Multiplication * Multiply value on either side operand x * y = 300
Division / Divides left side operand to right side operand y / x = 0.33
Modulus % Divides left side operand to right side operated and returns remainder
** Exponent power calculation on operators x ** y
// rounded away from zero (towards negative infinity) 9.0//2.0 = 4.0

Python Logical Operators

Python Logical operators are listed following:

Assume variable a = 20 and variable b = 30 then

Operator Description example
and both statements are true then condition are true a and b are true
or if any of two operated are non zero then condition are true i.e a are true or b are true a or b are true
not It is reversed the logical state of its operand Not(a and b) is false.

Assignment Operators

Operator Description example
= duty for right side operands to left side operand x+y=z duty value of x + y into z
+= adds right operand to the left operand and duty the result to left operand. z += x is equivalent to z = z + x
-= subtracts right operand from the left operand and duty the result to left operand z-= y is equivalent to z = z - y
*= multiplies right operand with the left operand and duty the result to left operand z *= y is equivalent to z = z * y
/= divides right operand with the left operand and assign the result to left operand z /= y is equivalent to z = z / zy /= x is equivalent to z = z / x
%= modulus using two operands and duty the result to left operand z %= x is equivalent to z = z % x
**= power operators and duty value to the left operand z **= x is equivalent to z = z ** x

Comparison Operators

Operator Description example
== value of two operands are equal then it condition is true (x==y) is not true
!= value of two operands are not true then it condition true. (X != Y)true
<> values of two operands are not equal, then condition are true. (x < > y) is true.
< If the value of left operand is less than the value of right operand, then condition becomes true. (x < y) is true
> value right operand is greater than left operand then condition are true (x>y) is true
>= value of left operand is greater than or equal to the value of right operand, then condition are true. (x >= y) is not true.
<= value of left operand is less than or equal to the value of right operand, then condition is true. (x<= y) is true

Bitwise Operators

Operator Description example
& AND Operator copies a bit to the result if it exists in both operands (X & Y) (means 0000 1100)
| OR It mean copies of bit if existed either operand. (x | y) (mean 1101 0011)
^ XOR when copies of bit if it is set in one operand but not both. (a ^ b)
~ Ones Complement Inverts all the bits. NOT
<< left operands value is moved left by the number of bits specified by the right operand. X << 5
>> left operands value is moved right by the number of bits specified by the right operand.. (means 0000 1111)

Identity Operators

Operator Description example
is both variables are the same object when return are true a is b mean 1 if id(a) equals of id(b)
is not both variables are the same object when return are false a is not b mean 1 if id(a) not equals of id(b)

Membership Operators

Operator Description example
in if a sequence with the specified value is when return is true a in b
not in if a sequence with the specified value is when return is false a in not b