In this tutorial, we will discuss Operator in Python programming language
Python programming language provides a rich set of operators to manipulate variables. We can divide all the Python operators into the following groups:
Arithmetic Operators
Relational Operators
Assignment Operators
Unary Operator
Logical Operators
Bitwise Operators
Arithmetic operators are used to calculating mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
>>> //#example for Arithmetic operator in Python >>> //#we can use Python Shell like a calculator >>> 56+46 102 >>> 58-35 23 >>> 21*20 420 >>> 24/6 4 >>> 34%4 2 >>> 54//6 9 >>> 4**5 1024 >>>
Example 2
>>> x=12 >>> y=5 >>> print("x+y =",x+y) ('x+y =', 17) >>> print("x-y =",x-y) ('x-y =', 7) >>> print("x*y =",x*y) ('x*y =', 60) >>> print("x/y=",x/y) ('x/y=', 2) >>> print("x%y=",x%y) ('x%y=', 2) >>> print("x//y=",x//y) ('x//y=', 2) >>> print("x**y=",x**y) ('x**y=', 248832) >>>
Example
In the Python programming language, assignment operators are used to assigning a value to a particular variable
for example – x=10 is a simple assignment operator that assigns the value 10 to the variable x
Python assignment operators and examples are shown below.
Example
>>> >>> x=16 >>> y=4 >>> z=x+y >>> z 20 >>> x+=y >>> x 20 >>> x+=y >>> x 24 >>> x/=y >>> x 6 >>> x*=y >>> x 24 >>> x&=y >>> >>> x 0 >>>
>>> >>> x=5 >>> y=3 >>> >>> x**y 125 >>> x=5 >>> y=3 >>> x**=y >>> >>> x 125 >>> x//=y >>> x 41 >>> x^=y >>> x 42 >>> x>>=y >>> x 5 >>> x<<=y >>> x 40 >>>
Relational operators are used to finding the relationship(compare) of values. It returns either true or false according to the given condition
Example
>>> >>> 15>12 True >>> 13>56 False >>> 23<45 True >>> 35<28 False >>> 15==15 True >>> 15==16 False >>> 34!=23 True >>> 37!=37 False >>> 12<=23 True >>> 34<=23 False >>> 25>=25 True >>> 34>=35 False >>>
Three logical operators are available in the Python language. like and, not, or
>>> >>> >>> x=True >>> y=False >>> >>> x and y False >>> x and x True >>> y and y False >>> >>> x or y True >>> x or x True >>> y or y False >>> >>> not x False >>> not y True >>> >>>
Some type of special operators available in Python language for the special purpose
Python language provides some identity operators for identification purpose
They are used to check when two values are located in the same memory location. It returns either true or false
>>> a=5 >>> b=5 >>> c='Hello world' >>> d='hello World' >>> e=[34,56,78,43] >>> f=[34,65,78,43] >>> print(a is b) True >>> print(a is not b) False >>> print(c is d) False >>> print(c is not d) True >>> print(e is f) False >>> print(e is not f) True >>>
Python language provides some membership operators for a value or variable is found in a given sequence
>>> >>> x='Python language' //string >>> y={1,'H',2,'e',3,'l'} //tuple >>> z=[34,67,85,43,25] //list >>> >>> print('h' in x) True >>> print('b' in x) False >>> print(3 in y) True >>> print(5 in y) False >>> print(85 in z) True >>> print(75 in z) False >>>
learn more about Python bitwise operator
Suggested for you
Data type and variable in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…