Operators of Python
# Operators Of Python

### Assignment Operator Of Python:

#### Program for demonstration of assignment operator

#### Output

### Arithmetical Operators of Python:

## Arithmetical Operator Table-

#### Program for demonstration of arithmetical operators of Python

#### Output

#### Value of r =7

#### Value of r = 3

#### Value of r = 10

#### Value of r = 2.5

#### Value of r = 2

#### Value of r = 1

#### Value of r 25

## Short-hand Assignment Operators of Python

#### Program for demonstration of short-hand assignment operators of python

#### Output

#### Relational or Comparison Operators of python

#### Program for demonstration of relational operators of python

### Logical Operators of Python

#### Program for demonstration of logical operators of Python

### Output

Operators of python is either a special symbol or keyword used to perform operation on variable or value. Python have different kind of operators to perform different kind of operations. The value that the operator operates on is called the operand.

The = sign is a known as assignment operator and used to assign value to computer memory.The following statements showing using assignment operator to assign value

For example:

a = 2 #this statement will assign value 2 variable a

1 2 3 4 5 6 7 8 9 10 11 |
a, b = 2, 3 print("Value of a=",a) print("Value of b"=, b) a=b=5 print("Value of a=", a) print( " Value of b = " , b ) |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Value of a= 2 Value of b= 3 Value of a =5 Value of b =5 |

Arithmetical operators are used to perform arithmetical operation like addition, subtraction, multiplication, division etc.

Operator | Name | meaning |
---|---|---|

+ | Addition Operator | Add two number or unary plus |

– | Subtraction Operator | Subtract right operand from the left or unary minus |

* | Multiplication Operator | Multiply two operands |

/ | True division Operator | Divide left operand by the right one and returns results into float |

% | Modulus Operator | Returns remainder of the division of left operand by the right |

// | Floor division Operator | Divide left operand by the right one and returns results into whole number adjusted to the left in the number line |

** | Exponent Operator | Returns value of left operand raised to the power of right |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
n1=5 n2 = r=nl+n2 print("Value of r = ",r) r=n1-n2 print("Value of r =" ,r) r= n1 *n2 print("Value of r = ",r) r=n1/n2 print("Value of r = ",r) r =n1// n2 print("Value of r =, r) r=n1%n2 print("Value of r=",r) r=n1**n2 print ( " Value of r = " , r ) |

there are various short-hand assignment operators in Python like a +=5 .It will add 5 to the variable a and later will assign the result to variable ‘a’. .It is equivalent to a =a +5. These short-hand assignment operators are also known as compound assignment operators.Following are the short-hand assignment operators in python:

Operator | meaning |
---|---|

+= | It adds right operand to the left operand and assign the result to left operand |

-= | It subtracts right operand from the left operand and assign the result to left operand |

*= | It multiplies right operand with the left operand and assign the result to left operand |

/ | Divide left operand by the right one and returns results into float |

/= | It divides left operand with the right operand and assign the result to left operand |

%= | It takes modulus using two operands and assign the result to left operand |

//= | Performs exponential (power) calculation on operators and assign value to the left operand |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
a =2 a+=3 print (a) a-=3 print(a) a*=3 print(a) a/=3 print(a) a%=3 print(a) a**=3 print(a) a//=3 print(a) |

1 2 3 4 5 6 7 8 9 |
2 6 2.0 8.0 2.0 |

Comparison operators are used to compare values on left hand side and right hand side and fine relation between them. Following are the relational operators in python:

Operator | Name | meaning |
---|---|---|

< | less than operator | Returns True if left operand is less than the right |

> | Greater than operator | Returns True if left operand is greater than the right |

== | equal to operator | Returns True if both operands are equal |

!= | Not equal to operator | Returns True if operands are not equal |

>= | Greater than or equal to operator | Return True if left operand is greater than or equal to the right |

<= | Less than or equal to operator | Return True if left operand is less than or equal to the right |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
a=2 b=3 c=a<b print(c) c=a>b print(c) c=a==b print(c) c=a!=b print(c) c=a<=b print(c) c=a>=b print(c) |

Logical operators are used to combine two or more conditions and perform the logical operations using Logical AND, Logical OR and Logical NOT. Following are the logical operators in python:

Operator | Name | meaning |
---|---|---|

and | Logical AND operator | Returns True if both operands return True |

or | Logical OR operator | Returns True if either of the operands True |

not | Logical NOT operator | Returns True if either of operands is False and returns False if operands is True |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
a =2 <3 and 4>3 print(a) a= 2 or 3 or 4> 3 print(a) a=not a print(a) a = not a print(a) |

1 2 3 4 |
True True False True |