-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03.Operators.py
More file actions
111 lines (78 loc) · 2.36 KB
/
03.Operators.py
File metadata and controls
111 lines (78 loc) · 2.36 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
# 1.Arithmetic Operators: +, -, *, /, //, %, **
a = 15
b = 4
print("Arithmetic Operators:")
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)
# 2.Comparison Operators: ==, !=, >, <, >=, <=
a1 = 10
b1 = 20
print("\nComparison Operators:")
print(a1 > b1)
print(a1 < b1)
print(a1 == b1)
print(a1 != b1)
print(a1 >= b1)
print(a1 <= b1)
# 3.Logical Operators: and, or, not
a2 = True
b2 = False
print("\nLogical Operators:")
print("a2 and b2:", a2 and b2)
print("a2 or b2:", a2 or b2)
print("not a2:", not a2)
# 4.Bitwise Operators: &, |, ^, ~, <<, >>
a3 = 5 # 0101 in binary
b3 = 3 # 0011 in binary
print("\nBitwise Operators:")
print("a3 & b3:", a3 & b3) # Bitwise AND -> Compares each bit → 1 & 1 = 1, else 0.
print("a3 | b3:", a3 | b3) # Bitwise OR -> Compares each bit → 1 | 0 = 1, 0 | 1 = 1, 1 | 1 = 1.
print("a3 ^ b3:", a3 ^ b3) # Bitwise XOR -> XOR → 1 if bits are different, 0 if same.
print("~a3:", ~a3) # Bitwise NOT
print("a3 << 1:", a3 << 1) # Left Shift
print("a3 >> 1:", a3 >> 1) # Right Shift
# 5.Assignment Operators: =, +=, -=, *=, /=, //=, %=, **=
a4 = 10
print("\nAssignment Operators:")
a4 += 5
print("a4 += 5:", a4)
a4 -= 3
print("a4 -= 3:", a4)
a4 *= 2
print("a4 *= 2:", a4)
a4 /= 4
print("a4 /= 4:", a4)
a4 //= 2
print("a4 //= 2:", a4)
a4 %= 3
print("a4 %= 3:", a4)
a4 **= 2
print("a4 **= 2:", a4)
# 6.Identity Operators: is, is not
a5 = 34
b5 = 98
c = a5
print("\nIdentity Operators:")
print("a5 is b5:", a5 is b5)
print("a5 is not b5:", a5 is not b5)
print("a5 is c:", a5 is c)
# 7.Membership Operators: in, not in
list1 = [1, 2, 3, 4, 5]
print("\nMembership Operators:")
print("2 in list1:", 2 in list1)
print("6 not in list1:", 6 not in list1)
# 8.Conditional Operator (Ternary Operator): condition_if_true if condition else condition_if_false
x,y = 10, 20
print("\nTernary Operator:")
min = x if x < y else y
print("Minimum of x and y:", min)
# 9. Operator Precedence
print("\nOperator Precedence:")
print("Result of 10 + 20 * 30:", 10 + 20 * 30) # Multiplication has higher precedence
print("Result of (10 + 20) * 30:", (10 + 20) * 30) # Parentheses change precedence
print("Result of 10 + 20 - 5 * 2:", 10 + 20 - 5 * 2) # Multiplication before addition and subtraction