Professional Documents
Culture Documents
Operators
Terminology of an Operation:
Every operation consists two parts
1. Operand(s)
2. Operator
Ex:
a+b
Here Operands: a, b
Operator: +
Types of Operators:
Arithmetic Operators
Relational or Comparison Operators
Logical Operators
Assignment Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic Operators
+ --> Addition --> Sum
- --> Subtraction --> Difference
* --> Multiplication --> Product
/ --> Division --> Quotient (Actual)
// --> Floor or Integer Division --> Quotient (Integer)
% --> Modulo Division --> Remainder
** --> Exponentiation --> Power
Arithmetic Operators
Addition (+)
Used on two integers
Used on two floating values
Used on two sequence types (list, str, tuple)
When used on sequence types + symbol acts as concatenation operator
On two integers
In [3]: a = 10
b = 20
print(a + b)
30
In [4]: x = 12.5
y = 13.5
print(x + y)
26.0
IronMan
In [8]: a = 10
b = 20
print(b - a)
10
In [9]: a = 10.2
b = 9.0
print(a - b)
1.1999999999999993
In [10]: a = 10
b = 3
print(a * b)
30
JohnJohnJohnJohnJohn
Division (/)
Used to get actual quotient of a division operation (float)
In [14]: 10/2
5.0
Out[14]:
In [15]: 10/3
3.3333333333333335
Out[15]:
In [16]: 10//3
3
Out[16]:
In [17]: -10/3
-3.3333333333333335
Out[17]:
In [18]: -10//3
-4
Out[18]:
In [19]: a = 10
b = 8
print(a % b)
In [20]: a = 12345
print(a % 10)
Exponentiation (**)
a ** b --> a to the power of b
In [21]: a = 10
b = 3
print(a ** b)
1000
In [22]: a = 2
b = 3
print(a ** b)
Formatting Output
In [31]: a = 10000
b = 20000
c = a + b
print("Sum of", a, "and", b, "is:", c)
Sum of 10 and 20 is 30
Sum of 10 and 20 is 30
146.456
146.46