Operators in Python.

Operator:

Operators are special symbol in python that carry out arithmatic or logical computation, Used to perform specific operation.

For Example:

>>>2+3 5

Types of Operator:

1. Arithmatic operator 2. Assignment operator 3. Comparison operator 4. Logical operator 5. Bitwise operator 6. Identity operator 7. Membership operator

Arithmatic operator:

Arithmatic operators are used to perform mathematical operation like addition,subtraction ,multiplication etc.

Addition(+):

Adds value on either of the operators.

Example:

a=10 b=20 print(a+b)

Output:

30

Subtraction(-):

Subtracts right hand operands form left hand oprand.

Example:

a=20 b=10 print(a-b)

Output:

10

Multiplication(*):

Multiply values on either side of the operators.

Example:

a=20 b=10 print(a*b)

Output:

200

Division(/):

divides left hand operand by right hand operand.

Example:

a=20 b=10 print(a/b)

Output:

2

Module(%):

Divides left hand operand by right hand operand and returns remainder.

Example:

a=20 b=10 print(a%b)

Output:

0

Assignment operators:

Assignment operators are used in python to assign values to variables.

=,+=,-=,*=,/=,%=

Example:

a=20 print(a) a+=10 print(a)

Output:

20 30

Comparison operators:

Comparision operators are used to compare values. It either returns True or False accoriding to the condition.

Grater than(>):

x>y

it gives True if left operand is grater than the right.

Example:

a=20 b=10 print(a>b)

Output:

true

Less than(<):

x < y

it gives True if left operand is grater than the right.

Example:

a=10 b=20 print(a<b)

Output:

true

Equal to(==):

x==y

True if both operands is equal.

Example:

a=10 b=10 print(a==b)

Output:

true

Not equal to(!=):

x!=y

True if operands are not equal.

Example:

a=20 b=10 print(a!=b)

Output:

true

Grater than or equal to(>=):

x>=y

True if left operand is grater than or equal to the right.

Example:

a=20 b=10 print(a>=b)

Output:

true

Grater than or equal to(>=):

x>=y

True if left operand is grater than or equal to the right.

Example:

a=20 b=10 print(a>=b)

Output:

true

Less than or equal to (<=):

x=

True if right operand is grater than or equal to the right.

Example:

a=20 b=10 print(a<=b)

Output:

false

logical Operator:

Logical operator are the and,or,not operators.

AND

True if both the operands are true.

x and y

Example:

x=True y=False print('x and y is',x and y)

Output:

x and y is False

OR

True if either of the operands is true.

x or y

Example:

x=True y=False print('x or y is',x or y)

Output:

x and y is true

NOT:

True if operand is false (complements of the operand).

not y

Example:

x=True print('not x is ',not x)

Output:

not x is False

Bitwise operators:

Bitwise operator works on bits and perform bit by bit operation.

Bitwise AND(&):

Example:

a = 24 b = 14 print(a & b)

Output:

8

Bitwise OR(|):

Example:

a = 24 b = 14 print(a | b)

Output:

30

Bitwise NOT(~):

Example:

a = 24 print(~a)

Output:

-25

Bitwise XOR(^):

Example:

a = 24 b=10 print(a ^ b)

Output:

18

Bitwise right shift(>>):

Example:

x=10 y=x>>2 print(y)

Output:

2

Bitwise left shift (<<):

Example:

x=18 y=x<<2 print(y)

Output:

72

Indentity operator:

'is' and 'is not' are the identity operators in python. They are check if two values (or variables) are located on the same part of memory . Two variables that are equal does not imply that they are identical.

IS:

True if the variable on either side of the operator point to the same object and false otherwise.

Example:

a=10 c=a is 10 print(c)

Output:

true

IS NOT:

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

Example:

a=20 c=a is not 20 print(c)

Output:

false

Membership operator:

'in' and 'not in' are the membership operator in python.

IN:

True if the finds a variable in the specified sequence and false otherwise.

Example:

subject=['DBMS','os','python','java'] TEMP='os' in subject print(TEMP)

Output:

true

NOT IN:

True if it does not finds a variable in the specified sequence and false otherwise.

Example:

subject=['DBMS','os','python','java'] TEMP='os' not in subject print(TEMP)

Output:

false

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.