Skip to content

Commit 649f4fe

Browse files
committed
added git ignore and more file
1 parent 194d569 commit 649f4fe

File tree

3 files changed

+365
-6
lines changed

3 files changed

+365
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
more-projects.ipynb
2+
decision-control-in-python.ipynb

Operators_in_Python.ipynb

+284-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": 25,
24+
"execution_count": 17,
2525
"id": "579476a5-b4da-4a82-a81f-eed0955293cd",
2626
"metadata": {},
2727
"outputs": [
@@ -57,7 +57,56 @@
5757
"\n",
5858
"\n",
5959
"Bitwise Operators\n",
60-
"2\n"
60+
"x & y = 2\n",
61+
"x | y = 3\n",
62+
"x ^ y = 1\n",
63+
"~x = -3\n",
64+
"x << 1 = 4\n",
65+
"x >> 1 = 1\n",
66+
"\n",
67+
"\n",
68+
"Assignment operators\n",
69+
"Initial x: 5\n",
70+
"After x += 3: 8\n",
71+
"After x -= 2: 6\n",
72+
"After x *= 4: 24\n",
73+
"After x /= 2: 12.0\n",
74+
"After x //= 3: 4.0\n",
75+
"After x %= 4: 0.0\n",
76+
"After x **= 2: 0.0\n",
77+
"After x &= 2: 0\n",
78+
"After x |= 1: 1\n",
79+
"After x ^= 2: 3\n",
80+
"After x <<= 1: 6\n",
81+
"After x >>= 2: 1\n",
82+
"\n",
83+
"\n",
84+
"Identity Operators\n",
85+
"True\n",
86+
"True\n",
87+
"True\n",
88+
"True\n",
89+
"True\n",
90+
"True\n",
91+
"\n",
92+
"\n",
93+
"Membership Operators\n",
94+
"Is 'Python' in sentence? True\n",
95+
"Is 'java' in sentence? False\n",
96+
"Is 'banana' in fruits? True\n",
97+
"Is 'grape' in fruits? False\n",
98+
"Is 3 in numbers? True\n",
99+
"Is 10 in numbers? False\n",
100+
"Is 'age' in person? True\n",
101+
"Is 'address' in person? False\n",
102+
"Is 'Java' not in sentence? True\n",
103+
"Is 'Python' not in sentence? False\n",
104+
"Is 'orange' not in fruits? True\n",
105+
"Is 'banana' not in fruits? False\n",
106+
"Is 6 not in numbers? True\n",
107+
"Is 2 not in numbers? False\n",
108+
"Is 'phone' not in person? True\n",
109+
"Is 'city' not in person? False\n"
61110
]
62111
}
63112
],
@@ -106,9 +155,238 @@
106155
"## Bitwise Operators\n",
107156
"print('\\n')\n",
108157
"print('Bitwise Operators')\n",
109-
"x=6\n",
110-
"y=3\n",
111-
"print(x & y) ## The & operator compares each bit and set it to 1 if both are 1, otherwise it is set to 0\n"
158+
"# 1. Bitwise AND (&)\n",
159+
"x = 2 # binary: 10\n",
160+
"y = 3 # binary: 11\n",
161+
"## The & operator compares each bit and sets it to 1 if both are 1, otherwise it is set to 0\n",
162+
"print(\"x & y =\", x & y) # Bitwise AND\n",
163+
"\n",
164+
"# 2. Bitwise OR (|)\n",
165+
"## The | operator compares each bit and sets it to 1 if one or both bits are 1, otherwise it is set to 0\n",
166+
"print(\"x | y =\", x | y) # Bitwise OR\n",
167+
"\n",
168+
"# 3. Bitwise XOR (^)\n",
169+
"## The ^ operator compares each bit and sets it to 1 if the bits are different, otherwise it is set to 0\n",
170+
"print(\"x ^ y =\", x ^ y) # Bitwise XOR\n",
171+
"\n",
172+
"# 4. Bitwise NOT (~)\n",
173+
"## The ~ operator inverts each bit of the number (complement), changing 0 to 1 and 1 to 0\n",
174+
"print(\"~x =\", ~x) # Bitwise NOT\n",
175+
"\n",
176+
"# 5. Left Shift (<<)\n",
177+
"## The << operator shifts the bits to the left by a specified number of positions. \n",
178+
"## Each left shift by n positions multiplies the number by 2^n.\n",
179+
"print(\"x << 1 =\", x << 1) # Left Shift\n",
180+
"\n",
181+
"# 6. Right Shift (>>)\n",
182+
"## The >> operator shifts the bits to the right by a specified number of positions. \n",
183+
"## Each right shift by n positions divides the number by 2^n.\n",
184+
"print(\"x >> 1 =\", x >> 1) # Right Shift\n",
185+
"\n",
186+
"\n",
187+
"## Assignment operators\n",
188+
"print('\\n')\n",
189+
"print('Assignment operators')\n",
190+
"\n",
191+
"x = 5 # Initial value of x is 5\n",
192+
"print(\"Initial x:\", x) # Output: 5\n",
193+
"\n",
194+
"# 1. Add and Assign (+=)\n",
195+
"x += 3 # Same as x = x + 3\n",
196+
"print(\"After x += 3:\", x) # Output: 8 (5 + 3)\n",
197+
"\n",
198+
"# 2. Subtract and Assign (-=)\n",
199+
"x -= 2 # Same as x = x - 2\n",
200+
"print(\"After x -= 2:\", x) # Output: 6 (8 - 2)\n",
201+
"\n",
202+
"# 3. Multiply and Assign (*=)\n",
203+
"x *= 4 # Same as x = x * 4\n",
204+
"print(\"After x *= 4:\", x) # Output: 24 (6 * 4)\n",
205+
"\n",
206+
"# 4. Divide and Assign (/=)\n",
207+
"x /= 2 # Same as x = x / 2\n",
208+
"print(\"After x /= 2:\", x) # Output: 12.0 (24 / 2), result is a float\n",
209+
"\n",
210+
"# 5. Floor Division and Assign (//=)\n",
211+
"x //= 3 # Same as x = x // 3\n",
212+
"print(\"After x //= 3:\", x) # Output: 4.0 (12.0 // 3), result is a float\n",
213+
"\n",
214+
"# 6. Modulus and Assign (%=)\n",
215+
"x %= 4 # Same as x = x % 4\n",
216+
"print(\"After x %= 4:\", x) # Output: 0.0 (4.0 % 4)\n",
217+
"\n",
218+
"# 7. Exponentiation and Assign (**=)\n",
219+
"x **= 2 # Same as x = x ** 2\n",
220+
"print(\"After x **= 2:\", x) # Output: 0.0 (0.0 ** 2)\n",
221+
"\n",
222+
"# 8. Bitwise AND and Assign (&=)\n",
223+
"x = 5 # Reset x to 5 for bitwise operations\n",
224+
"x &= 2 # Bitwise AND\n",
225+
"print(\"After x &= 2:\", x) # Output: 0 (binary: 010 & 001 = 000)\n",
226+
"\n",
227+
"# 9. Bitwise OR and Assign (|=)\n",
228+
"x |= 1 # Bitwise OR\n",
229+
"print(\"After x |= 1:\", x) # Output: 1 (binary: 000 | 001 = 001)\n",
230+
"\n",
231+
"# 10. Bitwise XOR and Assign (^=)\n",
232+
"x ^= 2 # Bitwise XOR\n",
233+
"print(\"After x ^= 2:\", x) # Output: 3 (binary: 001 ^ 010 = 011)\n",
234+
"\n",
235+
"# 11. Bitwise Left Shift and Assign (<<=)\n",
236+
"x <<= 1 # Bitwise Left Shift\n",
237+
"print(\"After x <<= 1:\", x) # Output: 6 (binary: 011 << 1 = 110)\n",
238+
"\n",
239+
"# 12. Bitwise Right Shift and Assign (>>=)\n",
240+
"x >>= 2 # Bitwise Right Shift\n",
241+
"print(\"After x >>= 2:\", x) # Output: 1 (binary: 110 >> 2 = 001)\n",
242+
"\n",
243+
"\n",
244+
"\n",
245+
"\n",
246+
"## Identity Operators\n",
247+
"print('\\n')\n",
248+
"print('Identity Operators')\n",
249+
"# 1. Using 'is' Operator: Check if two variables refer to the same object\n",
250+
"a = [1, 2, 3]\n",
251+
"b = a # 'b' points to the same list object as 'a'\n",
252+
"print(a is b) # Output: True, since 'a' and 'b' point to the same object in memory\n",
253+
"\n",
254+
"# Explanation:\n",
255+
"# Here, both 'a' and 'b' are referencing the same list object. Therefore, 'a is b' returns True.\n",
256+
"\n",
257+
"# 2. Using 'is not' Operator: Check if two variables do not refer to the same object\n",
258+
"\n",
259+
"x = [1, 2, 3]\n",
260+
"y = [1, 2, 3] # 'y' is a new list object with the same values as 'x'\n",
261+
"print(x is not y) # Output: True, since 'x' and 'y' are different objects in memory, even though they have the same values.\n",
262+
"\n",
263+
"# Explanation:\n",
264+
"# Even though the lists 'x' and 'y' have the same values, they are stored in different memory locations, \n",
265+
"# so 'x is not y' returns True.\n",
266+
"\n",
267+
"# 3. Using 'is' with None: Check if a variable is None\n",
268+
"\n",
269+
"z = None\n",
270+
"print(z is None) # Output: True, since 'z' is explicitly set to None\n",
271+
"\n",
272+
"# Explanation:\n",
273+
"# The 'is' operator is commonly used to check if a variable is 'None'. Since 'z' is set to 'None',\n",
274+
"# the result is True.\n",
275+
"\n",
276+
"# 4. 'is not' with None: Check if a variable is not None\n",
277+
"\n",
278+
"a = [10, 20, 30]\n",
279+
"print(a is not None) # Output: True, since 'a' is not None\n",
280+
"\n",
281+
"# Explanation:\n",
282+
"# 'a' is a list object and is not None. So, 'a is not None' returns True.\n",
283+
"\n",
284+
"# 5. Using 'is' for String Interning\n",
285+
"\n",
286+
"str1 = \"hello\"\n",
287+
"str2 = \"hello\" # Python optimizes string memory by pointing both 'str1' and 'str2' to the same memory location\n",
288+
"print(str1 is str2) # Output: True, because Python reuses the same object for identical strings\n",
289+
"\n",
290+
"# Explanation:\n",
291+
"# Python uses **string interning**, meaning identical strings (that are immutable) may refer to the same memory location.\n",
292+
"# So, 'str1 is str2' returns True.\n",
293+
"\n",
294+
"# 6. Using 'is not' for List Comparison\n",
295+
"\n",
296+
"list1 = [1, 2, 3]\n",
297+
"list2 = [1, 2, 3] # Although the lists have the same elements, they are different objects in memory\n",
298+
"print(list1 is not list2) # Output: True, since list1 and list2 are different objects, despite having the same values\n",
299+
"\n",
300+
"# Explanation:\n",
301+
"# Even though both lists have the same values, they are stored at different memory locations,\n",
302+
"# so 'list1 is not list2' returns True.\n",
303+
"\n",
304+
"# Conclusion:\n",
305+
"# - 'is' checks if two variables point to the same object in memory.\n",
306+
"# - 'is not' checks if two variables point to different objects in memory.\n",
307+
"# - These operators are used for identity comparisons (memory location), not for value comparisons.\n",
308+
"\n",
309+
"\n",
310+
"\n",
311+
"\n",
312+
"## Membership Operators\n",
313+
"print('\\n')\n",
314+
"print('Membership Operators')\n",
315+
"\n",
316+
"# Example of Membership Operators: 'in' and 'not in'\n",
317+
"\n",
318+
"# 1. Using 'in' with a String: Checks if a substring exists in the string\n",
319+
"sentence = \"Python programming is fun\"\n",
320+
"print(\"Is 'Python' in sentence?\", 'Python' in sentence) # Output: True, because 'Python' is in the string\n",
321+
"print(\"Is 'java' in sentence?\", 'java' in sentence) # Output: False, because 'java' is not in the string\n",
322+
"\n",
323+
"# Explanation:\n",
324+
"# 'Python' is a part of the string \"Python programming is fun\", so 'Python' in sentence returns True.\n",
325+
"# 'java' is not part of the string, so 'java' in sentence returns False.\n",
326+
"\n",
327+
"# 2. Using 'in' with a List: Checks if an element exists in the list\n",
328+
"fruits = ['apple', 'banana', 'cherry']\n",
329+
"print(\"Is 'banana' in fruits?\", 'banana' in fruits) # Output: True, because 'banana' is in the list\n",
330+
"print(\"Is 'grape' in fruits?\", 'grape' in fruits) # Output: False, because 'grape' is not in the list\n",
331+
"\n",
332+
"# Explanation:\n",
333+
"# 'banana' is present in the list ['apple', 'banana', 'cherry'], so 'banana' in fruits returns True.\n",
334+
"# 'grape' is not present in the list, so 'grape' in fruits returns False.\n",
335+
"\n",
336+
"# 3. Using 'in' with a Tuple: Checks if an element exists in the tuple\n",
337+
"numbers = (1, 2, 3, 4, 5)\n",
338+
"print(\"Is 3 in numbers?\", 3 in numbers) # Output: True, because 3 is in the tuple\n",
339+
"print(\"Is 10 in numbers?\", 10 in numbers) # Output: False, because 10 is not in the tuple\n",
340+
"\n",
341+
"# Explanation:\n",
342+
"# 3 is present in the tuple (1, 2, 3, 4, 5), so 3 in numbers returns True.\n",
343+
"# 10 is not in the tuple, so 10 in numbers returns False.\n",
344+
"\n",
345+
"# 4. Using 'in' with a Dictionary (Checking for Keys): Checks if a key exists in the dictionary\n",
346+
"person = {'name': 'Alice', 'age': 30, 'city': 'New York'}\n",
347+
"print(\"Is 'age' in person?\", 'age' in person) # Output: True, because 'age' is a key in the dictionary\n",
348+
"print(\"Is 'address' in person?\", 'address' in person) # Output: False, because 'address' is not a key in the dictionary\n",
349+
"\n",
350+
"# Explanation:\n",
351+
"# 'age' is a key in the dictionary {'name': 'Alice', 'age': 30, 'city': 'New York'}, \n",
352+
"# so 'age' in person returns True.\n",
353+
"# 'address' is not a key in the dictionary, so 'address' in person returns False.\n",
354+
"\n",
355+
"##===================\n",
356+
"\n",
357+
"# 5. Using 'not in' with a String: Checks if a substring does not exist in the string\n",
358+
"print(\"Is 'Java' not in sentence?\", 'Java' not in sentence) # Output: True, because 'Java' is not in the string\n",
359+
"print(\"Is 'Python' not in sentence?\", 'Python' not in sentence) # Output: False, because 'Python' is in the string\n",
360+
"\n",
361+
"# Explanation:\n",
362+
"# 'Java' is not part of the string \"Python programming is fun\", so 'Java' not in sentence returns True.\n",
363+
"# 'Python' is part of the string, so 'Python' not in sentence returns False.\n",
364+
"\n",
365+
"# 6. Using 'not in' with a List: Checks if an element does not exist in the list\n",
366+
"print(\"Is 'orange' not in fruits?\", 'orange' not in fruits) # Output: True, because 'orange' is not in the list\n",
367+
"print(\"Is 'banana' not in fruits?\", 'banana' not in fruits) # Output: False, because 'banana' is in the list\n",
368+
"\n",
369+
"# Explanation:\n",
370+
"# 'orange' is not present in the list ['apple', 'banana', 'cherry'], so 'orange' not in fruits returns True.\n",
371+
"# 'banana' is in the list, so 'banana' not in fruits returns False.\n",
372+
"\n",
373+
"# 7. Using 'not in' with a Tuple: Checks if an element does not exist in the tuple\n",
374+
"print(\"Is 6 not in numbers?\", 6 not in numbers) # Output: True, because 6 is not in the tuple\n",
375+
"print(\"Is 2 not in numbers?\", 2 not in numbers) # Output: False, because 2 is in the tuple\n",
376+
"\n",
377+
"# Explanation:\n",
378+
"# 6 is not in the tuple (1, 2, 3, 4, 5), so 6 not in numbers returns True.\n",
379+
"# 2 is in the tuple, so 2 not in numbers returns False.\n",
380+
"\n",
381+
"# 8. Using 'not in' with a Dictionary (Checking for Keys): Checks if a key does not exist in the dictionary\n",
382+
"print(\"Is 'phone' not in person?\", 'phone' not in person) # Output: True, because 'phone' is not a key in the dictionary\n",
383+
"print(\"Is 'city' not in person?\", 'city' not in person) # Output: False, because 'city' is a key in the dictionary\n",
384+
"\n",
385+
"# Explanation:\n",
386+
"# 'phone' is not a key in the dictionary {'name': 'Alice', 'age': 30, 'city': 'New York'},\n",
387+
"# so 'phone' not in person returns True.\n",
388+
"# 'city' is a key in the dictionary, so 'city' not in person returns False.\n",
389+
"\n"
112390
]
113391
},
114392
{
@@ -122,7 +400,7 @@
122400
],
123401
"metadata": {
124402
"kernelspec": {
125-
"display_name": "Python 3 (ipykernel)",
403+
"display_name": "Python 3",
126404
"language": "python",
127405
"name": "python3"
128406
},

0 commit comments

Comments
 (0)