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
Pyton program to calculate electicity bill
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…