-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_9Operators.py
More file actions
134 lines (111 loc) · 4.42 KB
/
Python_9Operators.py
File metadata and controls
134 lines (111 loc) · 4.42 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
#Operators
"""
Operands : It is the elements on which the operations are going to be performed
Operators : It is the special symbols that perform operations on variables and values
In Python🐍 Operators are of various types such as :
1- Arithmetic operators
2- Assignment operators
3- Comparision operators
4- Logical operators
5- Bitwise operators
6- Identity operators
7- Membership operators
1️⃣Arithmetic Operators
➡️These operators are used with numeric values to perform common mathematical operations.
+ - * / // %
"""
print("⭐ARITHMATIC OPERATORS⭐")
a = 20
b = 5
print("First number is ",a)
print("Second number is ",b)
print("Addition ", a + b) #addition operator -> adds two operands
print("Subtraction ", a - b) #subtraction operator -> subtracts second operand from the first operand
print("Multiplication ", a * b) #multiplication operator -> multiply two operands
print("Division ", a / b) #division operator -> divides first operand by second operand
print("Floor Division ", a // b) #⭐floor division operator -> it divides the first by the second and then rounds the result down to the nearest whole number
print("Modulus ", a % b) #⭐modulus operator -> returns the remainder
"""
2️⃣Comparision Operator
➡️These operators compare the values
"""
a = 1
b = 2
print("\nAGAIN\n")
print("⭐COMPARISION OPERATORS⭐")
print("First number is ",a)
print("Second number is ",b)
print("equal to : ", a == b) #equal to
print("not equal to : ", a != b) #not equal to
print("greater than : ", a > b) #greater than
print("lesser than : ", a < b) #lesser than
print("greater than equal to : ", a >= b) #greater than equal to
print("lesser than equal to : ", a <= b) #lesser than equal to
"""
3️⃣Logical Operator
➡️Used to combine conditional statements
1- AND : both conditions should be true and satisfied⭐⭐⭐
2- OR : atleast one condition should be true for the overall true solution , basically it will give TRUE as output.⭐⭐⭐⭐⭐
3- NOT : it will basically inverse the output that has been produced.⭐⭐
"""
print("\n\n⭐LOGICAL OPERATORS⭐\n")
a = 5
b = 6
print("AGAIN\n")
print("First number is ",a)
print("Second number is ",b)
print( a>4 and b>4 ) #5>4 and 6>4 both are true ✅✅ so output will be TRUE ✅.
print( a<b and b<a ) #5<6 is true ✅ but 6<5 is not true 🚫 so the output will be FALSE 🚫.
print( a<b or b<a ) #5<6 is true ✅ but 6<5 is not true 🚫 so the output will be TRUE ✅.because one condition is satisfied
print(not(a<b and b<a)) #not of FALSE 🚫 is TRUE ✅.
"""
4️⃣Assignment Operators
➡️Used to assign the values
1- Basic Assignment operator =
2- Addition Assignment +=
3- Subtration Assignment -=
4- Multiplication Assignment *=
5- Division Assignment /=
6- Modulus Assignment %=
7- Exponent Assignment *=
8- Floor Division Assignment //=
"""
print("\n\n⭐ASSIGNMENT OPERATORS⭐\n")
a = 10 #value 10 is assigned to the variable 'a'
b = 10 #value 10 is assigned to the variable 'b'
# Addition Assignment
print("1- Addition Assignment , lets take an example.")
a = a + 5
print(a) #Output will be 15 , as a is 5
print("But now for b lets do it differently by the addition assignment")
b += 5 #same as b = b + 5
print(b)
#all are the same for 2,3,4,5,6,7 and 8.
"""
5️⃣Identity Operators
➡️Used to compare the memory locations of two objects
1- IS
2- IS NOT
"""
print("\n\n⭐IDENTITY OPERATORS⭐\n")
l1 = [1,2,3]
l2 = [1,2,3]
l3 = l1 #Here the memory location has been successfully assigned
#Note that the objects within the list doesnt matter
#How the data is in the list whether is it same or not it doesn't matter.
print( l1 is l2) # as we can look l3 is assigned to l1 not l2. If there was in the code that l1 = l2 or l2 = l1 then it would be TRUE.
print( l1 is l3) # It is true because we have assigned.
print( l3 is l1) # Viceversa of that is also possible truly.
print( l1 is not l2 ) #We have not assigned but we know that it is false but with the not it will be TRUE.
"""
7️⃣Membership Operators
➡️Used to test if a sequence is presented in an object.
1- IN
2- NOT IN
"""
print("\n\n⭐MEMBERSHIP OPERATORS⭐\n")
list = [1,2,3,4,5]
print(3 in list) #3 is present in my list. So the output will be TRUE.
print(3 not in list) #Opposite of the above output that will be FALSE.
print(7 in list) #7 is absent in my list. So the output will be FALSE.
print(7 not in list) # as we all know opposite of above output. So the output will be TRUE.