-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperators.py.py
More file actions
167 lines (131 loc) · 5.06 KB
/
Operators.py.py
File metadata and controls
167 lines (131 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def demonstrate_arithmetic_operators():
# Arithmetic Operators
countOfMangos, countOfApples = 10, 3
# Addition
answer = countOfMangos + countOfApples
print("Arithmetic Operators:")
print(f"Addition: {countOfMangos} + {countOfApples} = {answer}")
# Subtraction
answer = countOfMangos - countOfApples
print(f"Subtraction: {countOfMangos} - {countOfApples} = {answer}")
# Multiplication
answer = countOfMangos * countOfApples
print(f"Multiplication: {countOfMangos} * {countOfApples} = {answer}")
# Division
answer = countOfMangos / countOfApples
print(f"Division: {countOfMangos} / {countOfApples} = {answer}")
# Modulus
answer = countOfMangos % countOfApples
print(f"Modulus: {countOfMangos} % {countOfApples} = {answer}")
# Exponentiation
answer = countOfMangos ** countOfApples
print(f"Exponentiation: {countOfMangos} ** {countOfApples} = {answer}")
# Floor Division
answer = countOfMangos // countOfApples
print(f"Floor Division: {countOfMangos} // {countOfApples} = {answer}")
def demonstrate_relational_operators():
# Relational Operators
countOfMangos, countOfApples = 10, 3
# Equal to
answer = countOfMangos == countOfApples
print("\nRelational Operators:")
print(f"Equal to: {countOfMangos} == {countOfApples} -> {answer}")
# Not equal to
answer = countOfMangos != countOfApples
print(f"Not equal to: {countOfMangos} != {countOfApples} -> {answer}")
# Greater than
answer = countOfMangos > countOfApples
print(f"Greater than: {countOfMangos} > {countOfApples} -> {answer}")
# Less than
answer = countOfMangos < countOfApples
print(f"Less than: {countOfMangos} < {countOfApples} -> {answer}")
# Greater than or equal to
answer = countOfMangos >= countOfApples
print(f"Greater than or equal to: {countOfMangos} >= {countOfApples} -> {answer}")
# Less than or equal to
answer = countOfMangos <= countOfApples
print(f"Less than or equal to: {countOfMangos} <= {countOfApples} -> {answer}")
def demonstrate_logical_operators():
# Logical Operators
isReady, isGood = True, False
# AND
answer = isReady and isGood
print("\nLogical Operators:")
print(f"AND: {isReady} and {isGood} -> {answer}")
# OR
answer = isReady or isGood
print(f"OR: {isReady} or {isGood} -> {answer}")
# NOT
answer = not isReady
print(f"NOT: not {isReady} -> {answer}")
def demonstrate_bitwise_operators():
# Bitwise Operators
redTeamScore, whiteTeamScore = 5, 2 # redTeamScore = 101 in binary, whiteTeamScore = 10 in binary
# AND
answer = redTeamScore & whiteTeamScore
print("\nBitwise Operators:")
print(f"AND: {redTeamScore} & {whiteTeamScore} -> {answer}")
# OR
answer = redTeamScore | whiteTeamScore
print(f"OR: {redTeamScore} | {whiteTeamScore} -> {answer}")
# XOR
answer = redTeamScore ^ whiteTeamScore
print(f"XOR: {redTeamScore} ^ {whiteTeamScore} -> {answer}")
# NOT
answer = ~redTeamScore
print(f"NOT: ~{redTeamScore} -> {answer}")
# Left shift
# Left Shift Increases the value
# example : 25 <- left shift we get 250
answer = redTeamScore << whiteTeamScore
print(f"Left shift: {redTeamScore} << {whiteTeamScore} -> {answer}")
# Right shift
# Right Shift Dcreases the Value
# example : 25 -> Right Shift we get 2
answer = redTeamScore >> whiteTeamScore
print(f"Right shift: {redTeamScore} >> {whiteTeamScore} -> {answer}")
def demonstrate_assignment_operators():
# Assignment Operators
totalScore = 5
# Assign
answer = totalScore
print("\nAssignment Operators:")
print(f"Assign: totalScore = {answer}")
# Add and assign
# totalScore = totalScore + 2
totalScore += 2
answer = totalScore
print(f"Add and assign: totalScore += 2 -> {answer}")
# Subtract and assign
totalScore -= 1
answer = totalScore
print(f"Subtract and assign: totalScore -= 1 -> {answer}")
# Multiply and assign
totalScore *= 3
answer = totalScore
print(f"Multiply and assign: totalScore *= 3 -> {answer}")
# Divide and assign
totalScore /= 2
answer = totalScore
print(f"Divide and assign: totalScore /= 2 -> {answer}")
# Modulus and assign
totalScore %= 3
answer = totalScore
print(f"Modulus and assign: totalScore %= 3 -> {answer}")
# Floor divide and assign
totalScore //= 2
answer = totalScore
print(f"Floor divide and assign: totalScore //= 2 -> {answer}")
# Exponent and assign
totalScore **= 3
answer = totalScore
print(f"Exponent and assign: totalScore **= 3 -> {answer}")
# Call each function to demonstrate operators
def demonstrate_operators():
demonstrate_arithmetic_operators()
demonstrate_relational_operators()
demonstrate_logical_operators()
demonstrate_bitwise_operators()
demonstrate_assignment_operators()
# Run the main demonstration function
demonstrate_operators()