8000 Operators example · dsabhrawal/python-examples@3f3125c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f3125c

Browse files
committed
Operators example
1 parent 9b83874 commit 3f3125c

File tree

8 files changed

+153
-2
lines changed

8 files changed

+153
-2
lines changed

Operators/ArithmeticOperators.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#Following are the operators supported
2+
# + Addition
3+
# - Subration
4+
# * Multiplication
5+
# / Division
6+
# % Modulus
7+
# // Integer Division
8+
# ** Exponential
9+
#
10+
11+
#If any of operand is float the result is float
12+
print(3+2) #prints 5
13+
print(3-2) #prints 1
14+
print(3*2) #prints 6
15+
print(2.5+2) #Prints 4.5 (float)
16+
17+
#In division result is always float irrespective of Operand
18+
print(10/2) #Prints 5.0
19+
20+
#In Modulus if both the operands are Integer the result is Integer and If one operand is float the result is float
21+
print(5%2) #Prints 1
22+
print(14.75%4) #prints 2.75
23+
24+
#In Exponential if both the operands are Integer the result is Integer and If one operand is float the result is float
25+
print(3.5**2) #Prints 12.25
26+
print(3**3) #Prints 27
27+
28+
#Integer division is also called as floor division. First performs the normal division and then applies floor() to result
29+
print(10.5//2) #Prints 5.0
30+
print(-5//2) #Prints -3
31+
32+
#Arithmetic Operations on Strings
33+
print('2'+'3') #Prints 23
34+
print('abc'+str(2+3)) #Prints abc5
35+
36+
print(3*'Hello') #Prints HelloHelloHello
37+
print(3*True) #Prints 3 (True converted to 1)
38+
39+
#Arithmetic Operations on Complex Numbers
40+
e = 2+3j
41+
f = 4-6j
42+
43+
print(e+f) #Prints (6-3j)
44+
print(e*f) #Prints (26+0j)
45+
print(e-f) #Prints (-2+9j)

Operators/AssignmentOperator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# += and -=
2+
3+
a = 10
4+
b = 20
5+
a +=12 #(a = a + 12)
6+
b -=3 #(b = b - 3)
7+
8+
print(a) #22
9+
print(b) #17

Operators/EqualityOperator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# == , != (Int, float, str, bool)
2+
3+
x,y=6,7
4+
p=6
5+
6+
print(x == y) #False
7+
print(p == x) #True
8+
9+
print(p == 6.0) #True
10+
11+
print('Hello' == 'Hello') #True
12+
13+
#Chaining
14+
print(10 == 20 == 6 == 3) #False
15+
print(1 == 1 == 1 < 2) #True
16+
print(1 !=2 < 3 > 1) #True

Operators/IdentityOperator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#is or is not
2+
3+
a = 10;
4+
b = 20;
5+
c = 10;
6+
7+
print(a is b) #False
8+
print(b is c) #False
9+
print(a is c) #True
10+
11+
print('Hello' is 'Python') #Flase

Operators/LogicalOperator.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#and , or , not (Str, bool ,int)
2+
3+
a = True
4+
b = False
5+
6+
print(a and a) #True
7+
print(a and b) #False
8+
print(b and b) #False
9+
print(a or b) #True
10+
print(not b) #True
11+
12+
p = 10
13+
q = 20
14+
print(p and q) #20 (as p is considered True and for 'and' operator the second operand will be checked to get final result)
15+
print(p or q) #10 (as p is considered True therefore second operand will not be checked for 'or' operation)
16+
print(0 and p) #0 (as 0 is considered as False and for 'and' operator if first operand is False second is not considered)
17+
print(0 or p) #10 (as 0 is considered False second operand will be checked to get final result)
18+
19+
x = ''
20+
y = ' '
21+
z = 'can'
22+
23+
print(not x) #True (Empty string is considered false)
24+
print(not y) #False (As string contains space)
25+
print(not z) #False (As string is not empty)
26+
print('ab' or 'abc')

Operators/MembershipOperator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#in or not in
2+
3+
a = 'John Snow'
4+
5+
print('a' in a) #False
6+
print('z' not in a) #True
7+
print('now' in a) #True

Operators/RelationalOperators.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#<, <=, >=, >
2+
3+
print(2 < 3) #True
4+
print(4 > 1) #Falsle
5+
6+
#In String the ascii value is compared
7+
8+
a = 'aaa';
9+
b = 'aad';
10+
11+
print(a > b) #False
12+
print(a < b) #True
13+
14+
#chaining of relational operators
15+
16+
p,q,r,s = 10,20,30,40
17+
18+
print(p < q > r < s) #False (Individual expression evaluation is followed by AND)
19+
print(p < q < r < s) #True

TypeConversion/TypeConversion.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#This examples talks about int() float() bool() str() complex() conversion functions
1+
#This examples talks about int() float() bool() str() complex() eval() conversion functions
22

33
#int()
44
x = 10.5
@@ -20,4 +20,22 @@
2020
#complex()
2121
print(complex(2)) #prints (2+0j)
2222
print(complex(2,1)) #prints (2+1j)
23-
print(complex(x)) #prints (10.5+0j)
23+
print(complex(x)) #prints (10.5+0j)
24+
print(complex(10.5,4)) #prints (10.5+4j)
25+
print(complex(0b1001,0o34)) #prints (9+28j) Converts to decimal
26+
27+
28+
#eval()
29+
p = eval('10')
30+
print(p)
31+
print(type(p))
32+
#10
33+
#<class 'int'>
34+
35+
q = eval('10.5')
36+
print(q)
37+
print(type(q))
38+
39+
#10.5
40+
#<class 'float'>
41+

0 commit comments

Comments
 (0)
0