Arithmetic operation is also called a basic operation where we are going to perform this operation with numerical values.
The different type of arithmetic operations are :
The first five operators are basic arithmetic operators let us understand these operators, consider an example as
a=20 b=10 print(a+b)= ? print(a-b)= ? print(a*b)= ? print(a%b)=?
The division operator (/) :
In python, the division operator always going to return the answer in floating point format only,
b=5 then if you divide this
a/b then the answer is
a=10 b=5 print(a/b)
a=10 b=3 print(a/b) print(10/3)
The floor division operator (//):
This operator is meant for both integral and floating point output depending on our arguments, If both the arguments are an integer then it will return an integer and if anyone argument is float then the result will be a float.
a=10 b=2 print(a//b) print(10//2)
a=10.0 b=2 print(10.0//2)
Let us understand the normal division and floor division with example
b=3 then in case of normal division in python, it will always result in floating point and hence the answer is
a=10 b=3 print(10/3)
b=3 then in case of normal division in python, it will always result in floating point hence the answer is
a=10.0 b=3 print(10.0/3)
b=3 then, in case of floor division in python if both the arguments are an integer then the result will be an integer only but here if we divide
3 then the result is
3.3333 but floor value is the nearest value to the
3.33 so the actual answer is
3.0 but both the arguments are of integer type and hence the answer is
a=10 b=3 print(10//3)
b=3 then, in case of floor division in python if one argument is float then the result will be in float and hence the answer is
3.0 because the nearest value for
a=10.0 b=3 print(10.0//3)
Exponential operator : This operator is also called a power operator which is represented by two stars (
print (10**2) means
102so the answer is
a=10 b=2 print(a**B) print(10**2)
print (3**3) =33 =27
a=3 b=3 print(3**3)
For addition operation in python, both the arguments should be a string type only, if anyone argument is not string type then python will throw an error, for more information you can go through our previous article typecasting
a="chercher" b=5 print(a+b) print("chercher"+5)
a="apple" b="mango" print("apple"+"mango")
a="banana" b=10 print("banana"+"10") or print("banana"+str(10))
In the same way, to apply an
* operator to the string, one argument should be of int type and another should be of string type if both the arguments are of string type then python will throw an error
a=grapes b=3 print("grapes"*3)
a="apple" b="mango" print("apple"*"mango")
Relational operators are comparison operators which are used to compare the equality between the two entities and returns the result in the boolean format depending on the comparison condition.
Relational operators :
Let us understand this with an example:
a=10 b=20 print(a>b) print(a>=b) print(a<b) print(a<+b)
Even we can apply relational operators between string data types also, but before comparing the string datatypes you should know the ASCI values of strings, if you do not know the value of the string, python is having two inbuilt functions ord and char to find the ASCI values of the string
ord is to find the Unicode value of the given character as a
print(ord(a)=97 and if you give as a
Another one inbuilt function is to know the character of the given Unicode value as a print(char(a))
print(chr(66)) print(chr(45)) print(chr(78))
The Following example of comparing two string by using the relational operators, in the below example I had taken
s2='Dog' so here python compares the Unicode value of first letters of both the strings and returns the result.
s1='Cat' s2='Dog' print(s1>s2) print(s1>=s2) print(s1<s2) print(s1<=s2)
s1='apple' s2='apple' print(s1>s2) print(s1>=s2) print(s1<s2) print(s1<=s2)
If s1="Mango" and s2="mango"
s1="Mango" s2="mango" print(s1>s2) print(s1>=s2) print(s1<s2) print(s1<=s2)
And if we compare between the string and the int then python will throw a type error
Let us consider another one example
a=10 b=20 if a>b: print("a is greater than b") else: print("a is not greater than b")
In the same way, the relational operators chaining is also possible, that means when we are having multiple operators, if one operator comparison become False then the result become False and if all the comparison become True then the result will be True
print(10<20) print(10<20<30<40) print(10<20<30<40>50) print(20<10<30<20)
The Equality operator compares two operands whether the two operands are equal or not and returns the result in boolean format.
The Equality Operators are:
Understand these concepts with an example
if a=10 and b=20 10==20 10!=20
1==True 0==True 0==False 10==10.0 "cherchertech"=="cherchertech"
In the case of Equality operators, if compare the arguments between string and integer type, that means even after comparing the incompatible datatypes also python never give any type error or value error, instead, it just returns the False as the answer.
We know that the relational operator is applicable for chaining, in the same way, equality operators chaining is also possible, if all the comparison become True then the answer will be True and if anyone comparison become False then the result will be False only.
10==20==30==40 2==2==2 2!=3!=6!=7
L1=[10,20,30] L2=[10,20,30] print(L1==L2) print(L1 is L2)
in the above example, I created two objects, L1 and dL2 are the reference variables pointing to the objects, but when we compare between the contents of the two objects by using the equal to operator, python returns True because both contents are same.
And when we compare between the reference variable by using is operator, python returns the False, because the two objects are pointing to the two different address
Logical Operators are used to compare the conditional statements.
The Three Logical Operators are:
In Boolean datatypes And operator returns True only when both arguments are True
True and True True and False False and True False and False
Or operator returns true if at least one argument is True
True or True True or False False or True False or False
The following example demonstrates clearly how to use
username=input('Enter user nme :') password=input('Enter password :') if username=='chercher' and password=='123' : print('valid user') else: print('invalid user')
Not Operator is always going to return the reverse complement
Not True Not False
Whenever we apply an AND Operator to the Non-Boolean datatypes the result always will be Non-boolean datatype only.
But in Non-boolean types, the same concept will be going to apply but it will always result in
y format only.
xevaluates to False then the result is
xevaluates to True then the result is
x=0 y=10 0 and 10
0evaluates to False and hence the result is
x=10 y=20 10 and 20
So in the above example, the value
10 evaluates to True then the answer is
y=" " then
x="chercher" y=" " "chercher" and " "
In the above example, the
x evaluates to True and the
y value evaluates to False and hence the answer is
x="apple" y="mango" "apple" and "mango"
In the above example, the
x value evaluates to True and hence the answer is
That means if
x evaluates to True then the result is
x and if
x Evaluates to False then the result is
x=10 and y=20 10 or 20 x=0 and y=20 0 or 20
Consider another example as shown below
[ ] or "chercher" "chercher" or "tech" " " or "tech"
not "orange" not 0 not 10
In the above, First example, we have taken as
Not Orange, where the is a string is not empty, so it will result as True.
In the Second example, I took Empty string as not Empty string and hence it will result as False.
In the third example,
Not 0, 0 value in boolean is False, hence I took here as Not False then it will result as True.
In the Fourth example, I have taken as
Not 10, where
10 is non zero, non-zero means True and hence it will result as False.
Bitwise operators are those which operates on one or more bits patterns or Binary Numerals at the level of there individual bits.
There are Six Bitwise operators in python are as follow:
We can apply Bitwise operators only for integers and Boolean Datatypes
16 & 18 False & True
And if you try to apply these operators between the float or string values then python will throw an error.
1.2 & 1.5
"war" & "love"
Bitwise And (&) :
In Bitwise And Operator if Both the bits are one(1) then the result is 1 otherwise result is Zero(0)
Bitwise Or (|):
In the Bitwise Or Operator if at least one bit is one(1) then the result is one(1) otherwise Zero(0).
Bitwise exclusive Or(^) :
In the bitwise Exclusive Operator, If both the bits are different then the result is one(1) else the Result is zero(0).
print(3&5) print(3|5) print(3^5)
To understand how Bitwise complement operator works in python you should aware of few important things listed below:
Let us consider an example, find the
find the complement of 4 or ~4
Let us find the complement of negative value,
The shift operator performs the bit manipulation on data by shifting the bits of its first operand by left or right.
In the bitwise left operator, the left operand value will be shifted to left by the number of bits specified with the right operand.
x<<2, here the
x operand bits will be shifted to the left side and the value will be specified by right operand that means by
2 values. When the bits moved to the left-hand side than on right side vacant cells are formed and these vacant cells are filled with the zeroes.
In the above example, we have to left shift two bits of 10, so here the binary value of 10 is 0000...01010 now if we shift two bits to the left side, two vacant cells will be formed on right side 00...01010.., now add two zeroes to the right side, it becomes 00..0101000.
If we convert the 00...0101000 to decimal then the value becomes 40 and hence
In the bitwise left operator, the left operand value will be shifted to the right by the number of bits specified with the right operand.
Let us consider the same example
10>>2, here when we shift two bits to the right side, then on the left side two vacant cells will be formed and then the vacant cells will be filled with sign bits.
The binary value of 10 is 000....01010, now if shift two bits to the right-hand side then the value becomes 000...010 and on the left-hand side two vacant cells will be formed and these vacant cells will be filled with sign bits.
So here, 10 is already a positive number and significant bit will be 0 and remaining bits are stored directly in the memory and hence the value becomes 00000...010 and when we convert it to the decimal form the value becomes 2, so
Working with IDLE Python
Assignment Operators are those which are used to assign the values to the variables in python, Equal(=) is called as an assignment operator.
a=5, the value 5 is assigned to the variable an on the left-hand side.
There are some compound assignment Operators also there in python which is represented as
x+=20, that means
x=x+20 and hence
x=10 x+=20 print(x)
Every arithmetic operators can be combined with an assignment operator to form the compound operator.
Let us consider an example
x=10 x&=5 print(x)
x=10 x**=2 print(x)
Python does not support increment and decrement operators
x=10 x++ print(x) x=10 x-- print(x)
Let us consider below examples, in that the
+acts just as a sign and not as an increment or decrement numbers.
x=10 print(+x) print(++x) print(+++x) print(++++++++++++x)
And in the same way, if we use
- it will acts as a sign only
x=20 print(-20) print(--20) print(----20)
In the above example
--20 results as
20 that means
Ternary Operator is nothing but the conditional operator, as it reduces the code size and increases the readability of code, this is because it evaluates a statement with a condition being True or False.
There is no particular keyword for the Ternary Operator and the syntax is as shown below:
T=first value if condition else second value
if a>b else 40
a=10 b-20 c=30 if a>b else 40 print(c)
Let us consider another one example
#Read 2 int values from the keyboard and print min value a=int(input("Enter first number:")) b=int(input("Enter second number:")) min= a if a<b else b print("The minimum value:", min)
Next, we can use one ternary operator inside the other one, this we call it as Nesting of Ternary Operator.
Let us understand this with an example
#Read 3 int values from the keyboard and print the minimum value of three numbers: a=int(input("Enter the first number:")) b=int(input("Enter second number:")) c=int(intput("Enter third number :")) min=a if a<b and a<c else b if b<c else c print("The minimum value:", min)
# Read 3 int values from the keyboard and print max value from the three numbers a=int(input("Enter first number:")) b=int(input("Enter second number:")) c=int(input("Enter third number")) max=a if a>b and a>c else b if b>c else c print("The maximum value:", max)
Let us consider one more example
#There are two numbers if two numbers are equal then print #Both numbers are Equal #The first number is smaller than the second number #The first number is greater than the second number a=int(input("Enter the first number:")) b=int(input("Enter the second number:")) result="Equal" if a==b else "Smaller" if a<b else "Bigger" print(result)
There are two types of special operators in python which are :
Let us understand these operators in detail:
Identity operator compares the address location of the two objects.
There are two Identity operators:
R1 is R2 Returns True only when two reference variables are pointing to the same object.
And R1 is not R2 Returns True only when two reference variables are not pointing to the same object.
As we know, object reusability concept is applicable in all the Datatypes except Complex Datatypes so let us consider below examples:
Example1: In this example, I am considering int Datatypes, as we know object reusability concept is applicable in int datatypes.
a=10 b=10 print(a is b) print(a is not b)
Example2: In this, I am considering string Datatypes
a="ash" b="ash" print(a is b) print(a is not b)