Python: Basic Operations
Like any other language in python as well we can do all operations with help of various operators, we just need to identify which operator should used in place.So in this post we will see Python Basic Operations, check below examples
>>> 89+34 123 >>> 9*23 207 >>> 78.0/4 19.5 >>> 78.8/4.0 19.7 >>>
Not only this, we have all types of operators for operations.
- Arithmetic operators
- Assignment operators
- Identity operators
- Logical operators
- Bitwise operators
- Membership operators
- Comparison operator
Let’s see these operators in detail with description and examples of them one by one.
These operators work for various mathematical calculation see below examples for each.
This used to add two values on either side of it.
>>> 45+34 79 >>> "Hello" + " World" 'Hello World' >>> lst=[1,2,3,4,5] >>> lst2=[6,7,8,9] >>> lst+lst2 [1, 2, 3, 4, 5, 6, 7, 8, 9]
As we saw above this operator could also work on string values as well, it quit useful in program.
This used to subtract right side from left side value.
>>> 76-23 53 >>> "Hello" - "World" Traceback (most recent call last): File "
", line 1, in TypeError: unsupported operand type(s) for -: 'str' and 'str'
See above this operator will not work on string and will reflect error.
As name suggest this operator is used to multiple values of either side.
>>> 8*12 96 >>> "Hello " * 5 'Hello Hello Hello Hello Hello ' >>> lst=[1,2,3,4,5] >>> lst * 5 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
As mentioned above, this would work on string and list as well.
As name suggest, this operator is used to divide values of left from right side
>>> 96/8 12
This used to divides left hand value through right hand and returns remainder.
>>> 96%8 0 >>> 97%8 1
This used to do calculation of multiplication of its own called exponential calculations.
>>> 5**3 125 >>> 8**2 64
- Floor Divisiedon
Floor division is used to return division value quotient and leave reminder value. In case of minus values it round of values from 0.
>>> 96//8 12 >>> 97//8 12 >>> -96//8 -12 >>> -15//4 -4
See above we have 96/8 and 97/8 return same value because one has 0 and 1 reminder. but when we do same for negative values we found -15/4 Floor division is 4 because it counts from 0.
Python comparison Operators
These comparison operator are used to compare both side values.
Assignment Operator, used to assign value >>> a=4 >>> b=5 Equality operator >>> a==b False Smaller than >>> a <b True Bigger than >>> b>a True Not equals to >>> a!=b True Smaller than or equals to >>> a<=b True Greater than or equals to >>> a>=b False
Above we defined assignment operator, in same way we have many types of assignment operator which could have many useful cases. see some examples below.
>>> a=4 >>> a+=1 >>> a 5
In Above example we had assigned one variable which has increment value placed on right side and then assigned to value which was mentioned on left, please care left side operand should defined first, otherwise it will out with an error.Same way it will act on subtract, multiple and other operator.
>>> a 5 >>> a-=2 >>> a 3 >>> a*=2 >>> a 6
In python we have identity operator which is used identify each side of operator are equal or not.
>>> a 6 >>> b 5 >>> a is b False >>> a is not b True
This is another example, how python syntax is easy to write and understand.
Like any other languages we have logical operators in python that are used to decided results of multiple conditions.
To understand it better way i like you see below image first, in this image 0 means False and 1 means True. Python also behave in same way.
>>> int(True) 1 >>> int(False) 0
If All given two or more conditions are True then final result will be True. See examples.
>>> a = 3 >>> b = 8 Because a is not greater than 5, both condition are not true and hence its false >>> a > 5 and b > 5 False Because a is less than 5 and b is greater than 5 , both condition are true and hence its true >>> a < 5 and b > 5 True
If Any condition in give two or more conditions is true, then final conditions is true. See examples.
>>> a = 3 >>> b = 8 Because both condition are not true and hence its false >>> a > 5 or b < 5 False Because first condition is true and hence its true >>> a < 5 or b < 5 True Because both condition are true and hence its true >>> a < 5 or b > 5 True
This is quit simple and useful to reverse conditions results like below examples
>>> x=True >>> x True >>> not x False >>> x True
I hope you some idea that how not act on x variable which was true and changed to false.
This Python Operator used to work on binary values, I hope we clearly know what binary means for. every number has its binary value. In python there is quit easy way to know binary value any int value. see examples
>>> bin(22) '0b10110'
In Above example 22 int value has 10110 binary value which used in bitwise operations. we have some bitwise operation that work in Python.
- & Binary And
Actually this is works as similar to Logical one, but we have to understand it bit different way because this works on binary values. Let’s see with some examples.
>>> 22 & 11 2
So result of above & operator is 2, quit wired, its nothing like True or False or nothing like any of given values. inside this , we have see binary values of these decimals.
>>> bin(22) '0b10110' >>> bin(11) '0b1011'
Let’s try to understand it the with help of diagram.
10110 -- Binary of 22 and 01011 -- Binary of 11 ------ 00010
and if we try to convert this binary to decimal,then we will get 2 in return and above with & operator.
>>> int("00010",2) 2
I Hope this is enough to understand how bitwise operator, in same way Or and Xor work. let’s try to explain them as well.
- | Binary Or
So this will work same as AND expect it work like OR conditions, if anyone of binary values is 1 it means 1 for final value.
>>> 11 | 22 31 >>> bin(11) '0b1011' >>> bin(22) '0b10110' 01011 10110 ------ 11111 >>> int("11111",2) 31
I hope this enough explainable to anyone how it becomes 31 out of 11 and 22 bitwise OR (|) operator.
- ^ Binary Xor
This Bitwise operator is work return 1 if odd number of 1 and return 0 if even number of 1
>>> 11 ^ 22 ^ 35 62 >>> bin(11) '0b1011' >>> bin(22) '0b10110' >>> bin(35) '0b100011' 11 -- 001011 22 -- 010110 35 -- 100011 ------ 111110 >>> int("111110",2) 62
with this example , i think it’s quit easy to understand how bitwise Xor works.
- << Binary left shift
This Binary left shift is used to shift binary values of left operand towards left and add same number of 0s that mentioned in right operand.
>>> 11<<2 44 >>> bin(11) '0b1011' 11 -- 1011 -- 101100 >>> int("101100",2) 44
Same way Binary right shift work
- << Binary right shift
This Binary right shift is used to shift binary values of left operand towards right and add same number of 0s that mentioned in right operand.
>>> 11>>2 2 >>> bin(11) '0b1011' 11 -- 1011 -- 0010 >>> int("0010",2) 2
This is all Bit wise operator work in python language.
python membership operator is quit easy and helpful to identify string present in specified sequence. sequence could be anything like list, string etc. Let’s see example of same.
>>> lst [1, 2, 3, 4, 5] >>> 1 in lst True >>> 1 not in lst False >>> a="Hello World" >>> "H" in a True >>> "H" not in a False
So we have two types of membership operator in python (in and not in). Both work as requested in above examples. In case of integer numbers we could also work with same but with bit way.
>>> a=678567 >>> "6" in str(a) True
Python Comparison Operators
Like any other language, python also has many comparison operator, which used to compare either side of value mentioned in given equation.
>>> a=8 >>> b=20 two in value defined >>> a > b a is greater than b False >>> a < b a is smaller than b True >>> a == b a is equals to b False >>> a != b a is not equals to b True >>> a >= b a is greater than or equals to b False >>> a <= b a is smaller than or equals to b True
So we have plenty of Comparison operator in python mentioned above.