Python OperatorsIntroduction:In this article, we are discussing Python Operators. The operator is a symbol that performs a specific operation between two operands, according to one definition. Operators serve as the foundation upon which logic is constructed in a program in a particular programming language. In every programming language, some operators perform several tasks. Same as other languages, Python also has some operators, and these are given below -
Arithmetic OperatorsArithmetic operators used between two operands for a particular operation. There are many arithmetic operators. It includes the exponent (**) operator as well as the + (addition), - (subtraction), * (multiplication), / (divide), % (reminder), and // (floor division) operators. Consider the following table for a detailed explanation of arithmetic operators.
Program Code: Now we give code examples of arithmetic operators in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - Addition of two numbers: 38 Subtraction of two numbers: 26 Multiplication of two numbers: 192 Division of two numbers: 5.333333333333333 Reminder of two numbers: 2 Exponent of two numbers: 1073741824 Floor division of two numbers: 5 Comparison operatorComparison operators mainly use for comparison purposes. Comparison operators compare the values of the two operands and return a true or false Boolean value in accordance. The example of comparison operators are ==, !=, <=, >=, >, <. In the below table, we explain the works of the operators.
Program Code: Now we give code examples of Comparison operators in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - Two numbers are equal or not: False Two numbers are not equal or not: True a is less than or equal to b: False a is greater than or equal to b: True a is greater b: True a is less than b: False Assignment OperatorsUsing the assignment operators, the right expression's value is assigned to the left operand. There are some examples of assignment operators like =, +=, -=, *=, %=, **=, //=. In the below table, we explain the works of the operators.
Program Code: Now we give code examples of Assignment operators in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - a=b: False a+=b: 38 a-=b: 26 a*=b: 192 a%=b: 2 a**=b: 1073741824 a//=b: 5 Bitwise OperatorsThe two operands' values are processed bit by bit by the bitwise operators. The examples of Bitwise operators are bitwise OR (|), bitwise AND (&), bitwise XOR (^), negation (~), Left shift (<<), and Right shift (>>). Consider the case below. For example, In the below table, we are explaining the works of the bitwise operators.
Program Code: Now we give code examples of Bitwise operators in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - a&b: 4 a|b: 7 a^b: 3 ~a: -6 a<>b: 0 Logical OperatorsThe assessment of expressions to make decisions typically uses logical operators. The examples of logical operators are and, or, and not. In the case of logical AND, if the first one is 0, it does not depend upon the second one. In the case of logical OR, if the first one is 1, it does not depend on the second one. Python supports the following logical operators. In the below table, we explain the works of the logical operators.
Program Code: Now we give code examples of arithmetic operators in Python. The code is given below - Output: Now we give code examples of Bitwise operators in Python. The code is given below - Is this statement true?: False Any one statement is true?: True Each statement is true then return False and vice-versa: True Membership OperatorsThe membership of a value inside a Python data structure can be verified using Python membership operators. The result is true if the value is in the data structure; otherwise, it returns false.
Program Code: Now we give code examples of Membership operators in Python. The code is given below - Output: Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below - Is value Present? True Is value not Present? True Identity Operators
Program Code: Now we give code examples of Identity operators in Python. The code is given below - Output: Now we compile the above code in python, and after successful compilation, we run it. Then the output is given below - True False False True True False Operator PrecedenceThe order in which the operators are examined is crucial to understand since it tells us which operator needs to be considered first. Below is a list of the Python operators' precedence tables.
Conclusion:So, in this article, we are discussing all the Python Operators. We briefly discuss how they work and share the program code using each operator in Python. Next TopicPython Comments |