Skip to content

Commit 4993370

Browse files
author
majid shahbaz
committed
Operators in Python
1 parent f5f2b5b commit 4993370

File tree

2 files changed

+201
-2
lines changed

2 files changed

+201
-2
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,49 @@
3535
2.2.14. Advanced Checks: isdecimal() vs isdigit(), isidentifier(), isprintable()
3636
2.2.15. Join and Strip usage
3737
2.2.16. Character Replacement using translate() and maketrans()
38-
### 2.3. Booleans
38+
#### 2.3. Booleans
3939
2.3.1. What are Booleans
4040
2.3.2. Boolean Expressions (`==`, `>`, `<`)
4141
2.3.3. `if` Condition with Boolean Result
4242
2.3.4. `bool()` Function Overview
4343
2.3.5. Values that Return `True`
4444
2.3.6. Values that Return `False`
4545
2.3.7. Class Example Returning False via `__len__`
46-
### 2.4. Type Casting
46+
#### 2.4. Type Casting
4747

4848
2.4.1. What is Type Casting?
4949
2.4.2. Using `int()`, `float()`, and `str()`
5050
2.4.3. Convert `int` to `str`
5151
2.4.4. Convert `float` to `int`
5252
2.4.5. Convert `str` to `float`
5353
2.4.6. Convert `int` to `float`
54+
55+
### 3.Operators
56+
#### 3.1. Assignment Operators
57+
3.1.1. = Equal to
58+
3.1.2. += Add and Assign
59+
3.1.3. -= Subtract and Assign
60+
3.1.4. *= Multiply and Assign
61+
3.1.5. /= Divide and Assign
62+
3.1.6. %= Modulus and Assign
63+
3.1.7. //= Floor Divide and Assign
64+
3.1.8. **= Power and Assign
65+
3.1.9. &= Bitwise AND and Assign
66+
3.1.10. |= Bitwise OR and Assign
67+
3.1.11. ^= Bitwise XOR and Assign
68+
3.1.12. >>= Right Shift and Assign
69+
3.1.13. <<= Left Shift and Assign
70+
3.1.14. := Walrus Operator
71+
72+
#### 3.2. Logical Operators
73+
3.2.1. and Logical AND
74+
3.2.2. or Logical OR
75+
3.2.3. not Logical NOT
76+
77+
#### 3.3. Identity Operators
78+
3.3.1. is Identity Equals
79+
3.3.2. is not Identity Not Equals
80+
81+
#### 3.4. Membership Operators
82+
3.4.1. in Check if present in sequence
83+
3.4.2. not in Check if not present in sequence

src/operators/operators.py

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Operators are used to perform operations on variables and values.
2+
3+
"""
4+
Python divides the operators in the following groups:
5+
6+
1. Arithmetic operators
7+
2. Assignment operators
8+
3. Comparison operators
9+
4. Logical operators
10+
5. Identity operators
11+
6. Membership operators
12+
7. Bitwise operators
13+
14+
Note: For Arithmetic operators and Operator Precedence
15+
See README.md 2.1.1. Basic Arithmetic Operations for explanation on operators
16+
"""
17+
"""
18+
1. Assignment Operators
19+
Assignment operators are used to assign values to variables
20+
"""
21+
# 1.1. Equal operator (=)
22+
x = 3
23+
print(f"Value of x = 3 -> {x}",end='\n')
24+
# Prints -> Value of x = 3 -> 3
25+
26+
# 1.2. Add and equal (+=)
27+
x += 3
28+
print(f"Value of x += 3 -> {x}",end='\n')
29+
# Prints -> Value of x += 3 -> 6
30+
31+
# 1.3. Subtract and equal (-=)
32+
x -= 3
33+
print(f"Value of x -= 3 -> {x}",end='\n')
34+
# Prints -> Value of x -= 3 -> 3
35+
36+
# 1.4. Multiply and equal (*=)
37+
x *= 3
38+
print(f"Value of x *= 3 -> {x}",end='\n')
39+
# Prints -> Value of x *= 3 -> 9
40+
41+
# 1.5. Divide and equal (/=)
42+
x /= 3
43+
print(f"Value of x/=3 -> {x}",end='\n')
44+
# Prints -> Value of x/=3 -> 3.0
45+
46+
# 1.6. Mod and equal (%=)
47+
x %= 3
48+
print(f"Value of x%=3 -> {x}",end='\n')
49+
# Prints -> Value of x%=3 -> 0.0
50+
51+
# 1.6. Floor and equal (//=)
52+
x = 9
53+
x //= 3 # equals -> x = x//3
54+
print(f"Value of x //= 3 -> {x}",end='\n')
55+
# Prints -> Value of x //= 3 -> 3
56+
57+
#1.7. Power Operator (**=)
58+
x **=3
59+
print(f"Value of x **=3 -> {x}",end='\n')
60+
#prints -> Value of x **=3 -> 27
61+
62+
#1.8. And and Equal &=
63+
"""
64+
It compares by bitwise
65+
like x and 3 are applied using AND operator
66+
lets say x = 6
67+
6 -> 110
68+
3 -> 011
69+
------------
70+
AND -> 010 -> 2
71+
72+
it only keeps that both has
73+
"""
74+
x &=3
75+
print(f"Value of x &=3 -> {x}",end='\n')
76+
#prints -> Value of x &=3 -> 3
77+
78+
#1.9. OR and Equal |=
79+
"""
80+
It compares by bitwise
81+
like x OR 3 are applied using OR operator
82+
lets say x = 3
83+
3 -> 011
84+
3 -> 011
85+
------------
86+
OR -> 011 -> 3
87+
88+
it only keeps that is present in any of them like x or 3
89+
"""
90+
x |=3
91+
print(f"Value of x |=3 -> {x}",end='\n')
92+
# prints -> Value of x |=3 -> 3
93+
94+
#1.10. This is a bitwise XOR assignment operation. ^=
95+
96+
"""
97+
XOR (^) — "Exclusive OR"
98+
Compares each bit of two numbers
99+
100+
If the bits are different, the result is 1
101+
If the bits are the same, the result is 0
102+
103+
x = 0 → 000
104+
3 → 011
105+
----------------
106+
Result → 011 = 3
107+
108+
5 → 101
109+
3 → 011
110+
----------
111+
110 → 6
112+
"""
113+
x = 5
114+
x ^= 3
115+
116+
print(f"Value of x ^=3 -> {x}",end='\n')
117+
118+
#1.11. Bitwise right shift assignment operator by n bits (>>=)
119+
x = 8 # binary: 1000
120+
x >>= 2 # shift 2 bits to the right 0010 -> 2
121+
print(f"Value of x >>=3 -> {x}",end='\n')
122+
# Value of x >>=3 -> 2
123+
124+
#1.12. Left shift by n bits (<<=)
125+
x = 5 # binary: 0000 0101
126+
x <<= 2 # shift 2 bits to the right 0001 0100 -> 20
127+
print(f"Value of x <<=2 -> {x}",end='\n')
128+
129+
#1.13. := (Walrus Operator)
130+
# It allows you to assign a value to a variable as part of an expression.
131+
n = len("hello")
132+
if n > 3:
133+
print(n)
134+
# Output: 5
135+
if (n := len("hello")) > 3:
136+
print(n) # Output: 5
137+
# It assigned on same line and compared values also
138+
139+
#2. Logical Operators
140+
"""
141+
and Returns True if both statements are true x < 5 and x < 10
142+
or Returns True if one of the statements is true x < 5 or x < 4
143+
not Reverse the result, returns False if the result is true
144+
"""
145+
#2.1. AND, OR and NOT
146+
x,y = 3,2
147+
if x>3 and y>3:
148+
print(f"Value of x and y greater than 3 ", end='\n') # Didn't Execute
149+
elif not(x<3 or y<3):
150+
print(f"Not x less than 3 or y less than 3 ", end='\n') #Didn't Execute
151+
elif x>=3 or y<3:
152+
print(f"x greater or equal to 3 or y less than 3 ", end='\n') # x greater or equal to 3 or y less than 3
153+
else:
154+
print(f"x less than 3 or y less than 3 ", end='\n')
155+
156+
# 3. Python Identity Operators (IS or IS NOT)
157+
print(f"Check x is y {x is y}", end='\n')
158+
# Check x is y False
159+
print(f"Check x is y {x is not y}", end='\n')
160+
# Check x is y True
161+
162+
#4. Python Membership Operators (in or not in)
163+
x = "This is a member"
164+
y = "This is a member of Company"
165+
print(f"Check x in y {x in y}", end='\n')
166+
# Check x in y True
167+
print(f"Check x not in y {x not in y}", end='\n')
168+
# Check x not in y False
169+

0 commit comments

Comments
 (0)