7. Python - Operators
7. Python - Operators
7. PYTHON – OPERATORS
Table of Contents
1. Operator ............................................................................................................................................. 2
2. Type of operators ............................................................................................................................... 2
1. Arithmetic Operators: (+, -, *, /, %, **, //) ...................................................................................... 3
2. Assignment operator: (=, +=, -=, *=, /=, %=, **=, //=) .................................................................... 5
3. Unary minus operator: (-) ............................................................................................................... 6
4. Relational operators (>, >=, <, <=, ==, !=)........................................................................................ 7
5. Logical operators (and, or, not) ...................................................................................................... 9
6. Membership operators(in, not in) ................................................................................................ 11
1|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
7. PYTHON – OPERATORS
1. Operator
a = 10
b = 20
c=a+b
print(c)
output
30
2. Type of operators
2|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
Make a note:
✓ These operators will do their usual job, so please don’t expect any
surprises.
Assume that,
a = 13
b=5
+ Addition a+b 18
- Subtraction a-b 8
* Multiplication a*b 65
% Modulus a%b 3
(Remainder of
division)
** Exponent a ** b 371293
operator
(exponential
power value)
3|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
// Integer division a // b 2
(gives only integer
quotient)
Make a note
a = 13
b=5
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b)
print(a**b)
print(a//b)
Output
18
8
65
2.6
3
371293
2
4|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
2. Assignment operator: (=, +=, -=, *=, /=, %=, **=, //=)
Assume that,
a = 13
= a = 13 a = 13 13
+= a += 6 a=a+6 19
-= a -= 6 a=a-6 7
a = 13
print(a)
a += 5
print(a)
Output
13
18
5|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
a = 10
print(a)
print(-a)
Output
10
-10
6|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
Assume that,
a = 13
b=5
== a == b False
!= a != b True
7|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
a = 13
b=5
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(a == b)
print(a != b)
Output
True
True
False
False
False
True
8|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
o and
o or
o not
✓ and
o If both arguments are True, then only result is True
✓ or
o If at least one argument is True, then result is True
✓ not
o complement
a = True
b = False
print(a and a)
print(a or b)
print(not a)
Output
True
True
False
9|Page 7.PYTHON-OPERATORS
Data Science – Python Operators
a=1
b=2
c=3
print((a>b) and (b>c))
print((a<b) and (b<c))
Output
False
True
10 | P a g e 7.PYTHON-OPERATORS
Data Science – Python Operators
The in operator
print(20 in values)
print(22 in values)
output
True
False
11 | P a g e 7.PYTHON-OPERATORS
Data Science – Python Operators
print("Welcome" in text)
print("welcome" in text)
print("nireekshan" in text)
print("Daniel" not in text)
output
True
False
False
True
12 | P a g e 7.PYTHON-OPERATORS