Showing posts with label Python: Operators. Show all posts
Showing posts with label Python: Operators. Show all posts

Python: Operators

 Operator is a symbol that performs a specific operations between two operands. There are many kinds of operators, these are:

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Logical operators
  • Bitwise operators
  • Membership operators &
  • Identity operators
Arithmetic operators: Arithmetic operators used between two operands for a particular operation. It includes the addition, subtraction, multiplication, division, exponentiation, remainder, floor division etc.
Below is the table explaining the details of each arithmetic operator.

Operator

Description

Addition (+)

It is used to add two operands. For example: A=10, B=20, A+B=30

Subtraction (-)

It is used to subtract the second operand from the first operand. For example: A=10, B=20, A-B=-10

Division (/)

It is used to divide the first operand by the second operand. If A=70, B=5, A/B=14

Multiplication (*)

It is used to multiply both the operands. A=70, B=5, A*b=350

Remainder (%)

It returns the remainder after dividing the first operand by the second operand. A=37, B=9, A%B=1

Exponent (**)

It calculate the first operand’s power to the second operand.

Floor Division (//)

It provides the quotient’s floor value, which is obtained by dividing the two operands.


Program code:

a=99
b=6
print('Addition of two numbers:', a+b)
print('Subtraction of two numbers:',a-b)
print('Multiplication of two numbers:',a*b)
print('Division of two numbers:',a/b)
print('Remainder of two numbers:',a%b)
print('Exponent of two numbers:',a**b)
print('Floor division of two numbers:',a//b)

Output
Addition of two numbers: 105
Subtraction of two numbers: 93
Multiplication of two numbers: 594
Division of two numbers: 16.5
Remainder of two numbers: 3
Exponent of two numbers: 941480149401
Floor division of two numbers: 16

Comparison Operators: Comparison operators are used to compare two values and returns boolean value like 'true' or 'false' accordance. Find below the table to know all the camparison operators with definition.

Operator

Description

==

If the value of two operands is equal, then the condition becomes true

!=

If the value of two operands is not equal, then the condition becomes true

<=

If the value of first operand is smaller than or equal to the second operand, the condition is true.

>=

If the value of first operand is greater than or equal to the second operand, the condition is true.

> 

If the first operand is greater than the second operand, the condition is true

< 

If the first operand is smaller than the second operand, the condition is true


Program code of comparison operator:
a=30
b=4
print('Two numbers are equal.' ,a==b)
print('Two numbers are not equal.' ,a!=b)
print('a is less than or equal to b.',a<=b)
print('a is greater than or equal to b.',a>=b)
print('a is greater than b.',a>b)
print('a is less than b.',a<b)

Output:
Two numbers are equal. False
Two numbers are not equal. True
a is less than or equal to b. False
a is greater than or equal to b. True
a is greater than b. True
a is less than b. False

Assignment Operators: Using the assignment operator, the expression's value in the right side is assigned to the operand in left side. Some examples of assignment operators are =, +=, -=, *=, %=, **=, //=. 
Find below the table to get definition of each assignment operator.

Operator

Description

=

It assigns the value of the right expression to the left operand.

+=

Add and Assign: Add right side operand with left side operand and then assign to left operand.

-=

Subtract And: Subtract right operand from left operand and then assign to left operand. True if both operands are equal.

*=

Multiply And: Multiply left operand with right operand and then assign to the left operand.

/=

Divide And: Divide left operand with right operand and then assign to the left operand.

%=

Modulus And: Takes modulus using left and right operand and assign value to left operand.

//=

Divide And: Divide left operand with right operand and then assign floor value to the left operand.

**=

Exponent And: Calculate exponent value using operand and assign value to the left operand.

&=

Performs bitwise AND on operands and assign value to left operand.

I=

Performs bitwise OR on operands and assign value to left operand.

˄=

Performs bitwise xOR on operands and assign value to left operand.

>>=

Performs bitwise right side on operands and assign value to left operand.


Program code of assignment operator:
a=30
b=4
print('a=b:',a==b)
print('a+=b:',a+b)
print('a-=b:',a-b)
print('a*=b:',a*b)
print('a%=b:',a%b)
print('a**=b:',a**b)
print('a//=b:',a//b)

Output:
a=b: False
a+=b: 34
a-=b: 26
a*=b: 120
a%=b: 2
a**=b: 810000
a//=b: 7

Bitwise Operators: In Python, Bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, thats why the name bitwise operators. The result is then returned in decimal format.
Python bitwise operators work only on integers.
Find below the table to get definition of each bitwise operator.

Operator

Name

Description

Syntax

&

Bitwise AND

If both operands are bits  1, result bit 1 otherwise results bit 0.

a&b

|

Bitwise OR

If any of the two operands is bits  1, result bit 1 otherwise results bit 0.

A|b

~

Bitwise NOT

Inverts individual bits

~a

^

Bitwise XOR

If any of the two operands is bits  1, but not the both are 1, result bit 1 otherwise results bit 0.

a^b

>> 

Bitwise right shift

The left operand’s value is moved towards right by number of bits specified by the right operand.

a>>

<< 

Bitwise left shift

The left operand’s value is moved towards left by number of bits specified by the right operand.

a<<


Program code of bitwise operator:
a=6 
b=7 
print('a&b:',a&b)
print('a|b:',a|b)
print('a^b:',a^b)
print('~a:',~a)
print('a<<b:',a<<b)
print('a>>b:',a>>b)

Output:
6
7
1
-7
768
0

Logical Operators: Logical operators are used to combine two conditional statements by "and", "or" and "not" operators.
Find below the table to get details of the logical operators:

Operator

Name

Description

Syntax

and

And

Returns True if both statements are true.

x>8 and x<16

or

Or

Returns True if any one statement is true.

X<20 or x>15

not

Not

Reverse the result, return False if the result is true.

Not(x<5 and x<9)

Program code of logical operator:
a=6  
print('Are these expressions true:',a>6 and a<5)
print('Is any of the two statements true:',a>5 or a>6)
print('Each statement is true then return False and vice versa:',(not(a>2 and a>5)))

Output:
Are these expressions true: False
Is any of the two statements true: True
Each statement is true then return False and vice versa: False

Membership Operators: Membership operators in Python are operators used to test whether a value exists in a sequence, such as list, tuple or string. The membership operators available in Python are:

Operator

Description

Syntax

in

Returns True if the given value is found in the sequence.

x=[“Rose”, “Lotus”]

print(‘Is value present?’, ”Rose” in x)

not in

Returns True if the given value is not found in the sequence.

x=[“Rose”, “Lotus”]

print(‘Is value not present?’, ”Rice” not in x)

Program code of membership operators:
a=["pen","pencil"]
print('Is value present?', "pencil" in a)
print('Is value not present?', "notebook" not in a)

Output:
Is value present? True
Is value not present? True

Identity Operators: Identity operators are used to compare the objects, not if they are equal, but if they are actually the same objects with same memory location. Following are the identity operators:

Operator

Description

Example

Is

Returns True if both variables are the same object

x is y

Is not

Returns True if both variables are not the same object

x is not y


Program code of identity operators:
x=["apple", "banana"]
y=["apple", "banana"]
z=x
print(x is z)
print(x is y)
print(x==z)
Output:
True
False
True

Python: Functions