You are on page 1of 1

Basics of Python Programming Language (Continued)

Operators
Terminology of an Operation:
Every operation consists two parts

1. Operand(s)
2. Operator

Operand(s): On which the operation is being performed


Operator: The one which is performing the operation

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

On two floating point values

In [4]: x = 12.5
y = 13.5
print(x + y)

26.0

On two sequence types

In [5]: name1 = 'Iron'


name2 = 'Man'
print(name1 + name2)

IronMan

In [6]: list1 = [10, 20, 30]


list2 = [40, 50, 60]
print(list1 + list2)

[10, 20, 30, 40, 50, 60]

In [7]: t1 = ('a', 'b', 'c')


t2 = ('d', 'e', 'f')
print(t1 + t2)

('a', 'b', 'c', 'd', 'e', 'f')

Subtraction Operator (-)


Used on two integers or two floating point values

In [8]: a = 10
b = 20
print(b - a)

10

In [9]: a = 10.2
b = 9.0
print(a - b)

1.1999999999999993

Multiplication Operator (*)


Used on two integers and two point values
Used on a string and an integer
When used on a string and an integer * symbol acts as a repetition operator.

In [10]: a = 10
b = 3
print(a * b)

30

In [11]: name = 'John'


x = 5
print(name * x)

JohnJohnJohnJohnJohn

Division (/)
Used to get actual quotient of a division operation (float)

In [14]: 10/2
5.0
Out[14]:

Floor division or Integer Division (//)


Used to get floored quotient
Rounds down the actual quotient to it's nearest integer.

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]:

Modulo Division (%)


Used to get the reaminder of a division operation

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 10000 and 20000 is: 30000

Using .format method


In [33]: a = 10
b = 20
c = a + b
print("Sum of {} and {} is {}".format(a, b, c))

Sum of 10 and 20 is 30

Using literal string interpolation (f strings)


In [36]: a = 10
b = 20
c = a + b
print(f"Sum of {a} and {b} is {c}")

Sum of 10 and 20 is 30

Decimal point adjustment


In [42]: a = 146.456464665
print(f'{a:.3f}') # 146.456
print('{:.2f}'.format(a)) # 146.46

146.456
146.46

You might also like