OPERATORS IN PYTHON

 Introduction


• Operators are used to perform operations in python. Operators are symbols used for performing mathematical, logical, and relational operations.


• For any operation, we need the operator and the operand. For example: x + y Here x and y are operands and + is the operator.


• These operands can also be assigned values as follows: x = 12, y = 13. Then x + y would be equal to 25 (12 + 13)


• Types of operators used in python include :


• Assignment operator ( = )


• Arithmetic operator (+,-, /, *)


• Comparison operator (<,>, !=, == etc)


• Logical operator (and, or, not)


Arithmetic Operator


Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division etc. on operands.



                 


                

Assignment Operator


• An assignment operator is used to assign the value to its left operand with the value present in the right operand.


• '=' sign is used as the assignment operator in Python. For example


age = 15


Here the variable is assigned the value 15.


• Below are some examples of variations of assignment operators in python.


Comparison Operator


• The comparison operators are used to compare two operands and return if the comparison is True or False.


• For example, if we are checking (5 > 4) then it will return True but if we are checking (5 < 4), then it will return false.


• Some other variations of the comparison operators are shown below.


Logical Operator


• Logical operator is used mostly with two or more boolean values. Logical operators used in python are 'and' 'or' and "not'


• 'And' operator will return True if both the conditions are True or else it will return False. For example:


x = 5


print ( x > 3 and x < 10 )


It returns True because 5 is greater than 3 and 5 is less than 10.


• 'or' operator will return True if either of the conditions are True and return False if both are False. For example:


x = 5


print ( x > 3 or x < 4 )


It returns True because one of the conditions are True (5 is greater than 3, but 5 is not less than 4)


• 'not' reverses the result i.e. if condition is true, it will return False. For example:

x = 5 print ( x > 3 and x < 10 )


It returns False because not is used to reverse the result.


Next topic is srings


Comments