This is Python Operators
Operator:
There are two types of operator.
i. Binary Operator.
ii. Unary Operator.
i. Binary Operator: Binary operators are which operator that give two operands.
Example: 2 + 3 , 2/3, 2-3, 2*3
2+3
2,3 is a operands and +(Plus) is a operator.
2/3
2,3 is a operands and / (Division) is a operator.
2-3
2,3 is a operands and - (Subs traction) is a operator.
2*3
2,3 is a operands and * (Multiplication) is a operator.
ii. Unary Operator: -5, ! (exclamation mark) these are unary operator.
Unary operators have one operator.
ii. Arithmetic Operator:
Most of the arithmetic operators are Binary operators.
-, +, *,/,// -----> these are arithmetic operator.
/ ---->floating Division.
ex: 5/2=2.5
//------> integer Division.
ex: 5//2=2
% ----> Modules or Mod
ex: 5%2 =1
9%2=1
9%2=1
8%4=0
123 % 10 =3
123 % 100 = 23
123 % 1000= 123
222021 % 10 =1
22021 % 100 = 21
22021 % 1000 =021
22021 % 10000 = 2021
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
5 % 2 = 1
6 % 2 = 0
7 % 2 = 1
8 % 2 = 0
9 % 2 = 1
10 % 2 = 0
2 ----> (0,1)
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
3 --->(0,1,2)
8 % 4 = 0
9 % 4 = 1
10 % 4 = 2
11 % 4 = 3
12 % 4 = 0
13 % 4 = 1
14 % 4 = 2
15 % 4 = 3
16 % 4 = 0
17 % 4 = 1
18 % 4 = 2
19 % 4 = 3
20 % 4 = 0
4 --->(0,1,2,3)
5 --->(0,1,2,3,4)
6 --->(0,1,2,3,4,5)
6 --->(0,1,2,3,4,5,6)
n ---> n remainders possible
0,1,2,3,4,5.......(n-1)
* ----> asterisk
Even number:
If number is Divisible by 2 then that is even number.
Example: 2,4,,6,8,10,12,14,16,18,20,22,24,26,28,30.........
2 % 2=0
4 % 2 = 0
6 % 2 = 0
8 % 2 = 0
100 % 2 = 0
198 % 2 = 0
101028 % 2 = 0
odd number:
If number is not divisible by 2 then that is odd number.
Example: 1,3,5,7,9,11,13,15,17,19,21,23,25,27..........
1 % 2 = 1
3 % 2 = 1
5 % 2 = 1
7 % 2 = 1
11 % 2 = 1
13 % 2 = 1
21 % 2 = 1
31 % 2 = 1
101 % 2 = 1
105 % 2 = 1
111 % 2 = 1
201 % 2 = 1
** (Double asterisk) ------> Exponent ion or Power function
example:
2 ** 2 = 4
2 ** 3 = 8
10 ** 2 = 100
"""
Arithmetic operator
+ ---- addition
- ----- subs traction
/ ---- floating division
// ---- integer division
% -----modulus - remainder
** ---- exp-power
"""
print(6+5)
print(10-5)
print(5*6)
print(15/5)
print(3%2)
print(3**2)
Output:
11
5
30
3.0
1
9
11
5
30
3.0
1
9
Comparison Operator:
Sometimes it's also called Boolean Operator.
== ----> Equal
!= -----> Not Equal
4 == 4 -------> This is True
4 ==5 --------> This is False
4 != 5 -------> This is True
4 != 4 -------> This is False
# Comparison Operator
a = 1
b = 3
print(a==b)
print(a!=b)
output:
False
True
True
s1 = "japan"
s2 = "japan"
print(s1==s2)
print(s1!=s2)
Output:
True
False
s1 = "japan"
s2 = "Japan"
print(s1==s2)
print(s1!=s2)
Output:
False
True
> ---- Greater Than
< ------ Less Than
>= ------- Greater Than equal to
<= ------- Lesser than equal to
> , < , >=, <=
2 > 2
False
2 < 2
False
2 >= 2
True
print(2<=5)
True
print(2<5)
true
This is Infinity symbol.
infinity mean very big number.
minus -infinity mean very small number.
Logical Operators:
In python only three operators and, or, not.
and:
If both Operands are true then it will give answer True.
and operator is a binary operator. It takes left(L) operand and right(R) operand. (L and R)
and operator is a binary operator. It takes left(L) operand and right(R) operand. (L and R)
and operand works on Boolean.
Condition always give you Boolean answer.
This is "and" operand
Left Right L and R
true + true = true
true + false = false
false + true = false
false + false = false
a = 1
b = 2
print( a<b and a>b)
output:
False
or:
If one condition is True or Other condition is true.
if any condition is true then answer is True.
This is "or" operand
Left Right L and R
true + true = true
true + false = true
false + true = true
false + false = false a = 1
b = 2
print(a>b or a<b)
output:
True
not:
not R not (R)
T F
F T
print(not(True))
print(not(False))
print(not(not(False)))
print(not(False))
output:
False
True
False
True
a = 1
b = 2
print(not(a<b and a>b))
output:
True
Input:
Take input from the user it will always string.
Example: input("This is input string")
Type Casting:
Example:
int("5")------>5
float("5") -------->5.0
