-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1325 lines (884 loc) · 34.2 KB
/
main.py
File metadata and controls
1325 lines (884 loc) · 34.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import math
from leetcode import Solution
# from leetcode import ListNode
# firstName = "mahammad"
# print(f"hello, {firstName}")
# print("hello", "i'm", 21, "years", "old", end=" end of text\n")
# print("hello", "i'm", 21, "years", "old", sep=" ", end="\n")
# hello = "hello"
# world = "world"
# world = hello
# hello = "updated..."
# print(hello, world)
# name = input("enter your name: ")
# age = input("enter your age: ")
# print(f"hello, {name}. you are {age} years old.")
# x = 10
# y = 5.5
# result = x + y
# print(result)
# result = x - y
# print(result)
# result = x * y
# print(result)
# result = int(x / y)
# print(result)
# result = x**y
# print(result)
# result = x // y
# print(result)
# result = x % y
# print(result)
# num = input("number: ")
# 1. isdigit()
# Purpose: Returns True if all characters in the string are digits (0-9).
# if num.isdigit():
# print(int(num) - 5)
# else:
# print("exception: input is not a number.")
# "123".isdigit() # True
# "-123".isdigit() # False
# "3.14".isdigit() # False
# "hello".isdigit() # False
# 2. isnumeric()
# Purpose: Similar to isdigit(),
# but it also returns True for numeric characters like fractions or superscripts.
# The isnumeric() method in Python can return True for characters that represent numbers,
# including fractions, superscripts, or other special numeric characters, not just the digits 0-9.
# This is different from isdigit(), which only returns True for digits.
# However, the isnumeric() method does not work with numbers like -3.14 or other floating-point numbers,
# because those include the negative sign (-) and the decimal point (.),
# which are not considered numeric characters by isnumeric().
# "123".isnumeric() # True
# "ⅩⅩⅩ".isnumeric() # True (Roman numerals)
# "⅓".isnumeric() # True (fraction)
# "-3.14".isnumeric() # False (includes "-" and ".")
# "3.14".isnumeric() # False (includes ".")
# 3. isspace()
# Purpose: Returns True if the string contains only whitespace characters (spaces, tabs, etc.).
# " ".isspace() # True
# "abc".isspace() # False
# print(f"string 'hello ' contains only whitespace: {'hello '.isspace()}") # False
# 4. isalpha()
# Purpose: Returns True if all characters in the string are alphabetic (A-Z, a-z).
# "abc".isalpha() # True
# "abc123".isalpha() # False
# 5. isalnum()
# Purpose: Returns True if all characters in the string are alphanumeric (letters or digits).
# "abc123".isalnum() # True
# "abc!".isalnum() # False
# 6. startswith()
# Purpose: Returns True if the string starts with the specified prefix.
# "hello".startswith("he") # True
# "hello".startswith("lo") # False
# 7. endswith()
# Purpose: Returns True if the string ends with the specified suffix.
# "hello".endswith("lo") # True
# "hello".endswith("he") # False
# 8. strip() lstript() rstrip()
# Purpose: Removes leading and trailing whitespace characters.
# print(" hello ".strip()) # "hello"
# print(" hello ".rstrip()) # " hello"
# print(" hello ".lstrip()) # "hello "
# 9. replace()
# Purpose: Replaces a specified substring with another substring.
# print("hello world".replace("world", "Python")) # "hello Python"
# print("hello world".replace("world---", "Python")) # "hello world"
# 10. split()
# Purpose: Splits the string into a list of substrings
# based on a specified delimiter (default is any whitespace).
# print("hello world".split()) # ['hello', 'world']
# print("apple,banana,orange".split(",")) # ['apple', 'banana', 'orange']
# print(
# "apple,banana,orange,apple,banana,orange,apple,banana,orange".split(",", 2)
# ) # ['apple', 'banana', 'orange,apple,banana,orange,apple,banana,orange']
# 11. join()
# Purpose: Joins elements of an iterable (like a list) into a single string with a specified separator.
# print("".join(["apple", "banana", "cherry"])) # "apple-banana-cherry"
# 12. upper() lower() title()
# print("hello, i am mahammad".upper()) # HELLO, I AM MAHAMMAD
# print("HELLO, I AM MAHAMMAD".lower()) # hello, i am mahammad
# print("hello, i am mahammad".title()) # Hello, I Am Mahammad
# 13. capitalize()
# print("i'm 21 years old".capitalize()) # I'm 21 years old
# ---------------------------------------
# if re.fullmatch(r"-?\d+(\.\d+)?", num):
# print(float(num) - 5)
# else:
# print("exception: input is not a number.")
# print(
# """
# hello,
# it is
# multiline
# text
# """
# )
# name = "mahammad"
# print(len(name)) # 8
# print(name[1]) # a
# print(name[-1]) # d
# print(name[0:3]) # mah
# print(name[0:]) # mahammad
# print(name[:5]) # maham
# print(name[:]) # mahammad
# name = "mahammad"
# name[0] = "test" # errror because strings are immutable
# surname = "ahmadov"
# print(name + " " + surname)
# print(f"{name} {surname}")
# print(f"{len(name)}:{4+4+0}")
# print(name.find("m")) # 0
# print(name.find("m1")) # -1
# print("mah" in name) # True
# print("mah" not in name) # False
# print(round(2.4)) # 2
# print(round(2.61212, 2)) # 2.61
# print(abs(-212)) # 212
# print(math.ceil(2.1))
# squares = ["a", "A"]
# for val in squares:
# print(ord(val))
# temperature = int(input("enter a temperature: "))
# if temperature > 30:
# message = "Drink water"
# elif temperature > 20:
# message = "It's nice"
# else:
# message = "It's cold"
# print("Done.")
# message = (
# "Drink water"
# if temperature > 30
# else "It's nice" if temperature > 20 else "It's cold"
# )
# print(message)
# high_income = False
# good_credit = True
# student = False
# if (high_income or good_credit) and not student:
# print("Eligible")
# else:
# print("Not eligible")
# chaining comparison operators
# age = 21
# if age >= 20 and age < 30:
# if 20 <= age < 30:
# print("eligible")
# for i in range(3):
# num = i + 1
# print(f"attempt: {num} {num * '.'}")
# attempt: 1 .
# attempt: 2 ..
# attempt: 3 ...
# for i in range(1, 4):
# print(f"attempt: {i} {i * '.'}")
# attempt: 1 .
# attempt: 2 ..
# attempt: 3 ...
# for i in range(1, 10, 2):
# print(f"attempt: {i} {i * '.'}")
# attempt: 1 .
# attempt: 3 ...
# attempt: 5 .....
# attempt: 7 .......
# attempt: 9 .........
# In Python, a for loop can have an else block,
# which is executed only if the loop completes normally (i.e., it does not break early).
# for number in range(3):
# print("Attempt", number)
# if number == 1:
# print("Successful")
# break # Loop exits early
# else:
# print("Loop completed without break")
# Attempt 0
# Attempt 1
# Successful
# for number in range(3):
# print("Attempt", number)
# else:
# print("Loop completed without break")
# Attempt 0
# Attempt 1
# Attempt 2
# Loop completed without break
# The for-else structure is useful when searching for something.
# If we find the item, we break; if we don't find it, the else block executes.
# search_list = [1, 2, 3, 4, 7, 8, 9, 10]
# target = 5
# for num in search_list:
# if num == target:
# print("found", num)
# break
# else:
# print("target not found")
# for x in range(10):
# for y in range(10):
# print(f"({x},{y})", end=" ")
# print("\n")
# num = 100
# while num > 0:
# print(num)
# num //= 2
# def greet_people():
# print("hello guys")
# greet_people()
# def greet(first_name: str, last_name: str):
# print(f"name: {first_name}. surname: {last_name}")
# greet("mahammad", "ahmadov")
# Python File Handling (Working with Files)
# In Python, we use the open() function to work with files.
# Here’s a clear explanation with all file modes, including reading and writing, and a comparison with C#.
# 1. Opening a File in Python
# file = open("c:\Users\mahammada\Documents\schedule.txt", mode="r") # Opens the file in read mode
# open("filename", mode) → Opens the file with a specific mode.
# You must close the file after using it:
# File Modes (Understanding the Letters)
# Mode Meaning Creates File? Overwrites?
# "r" Read (default) ❌ No ❌ No
# "w" Write (erases content) ✅ Yes ✅ Yes
# "a" Append (adds content) ✅ Yes ❌ No
# "x" Exclusive create (fails if exists) ✅ Yes ❌ No
# "r+" Read & Write (no erase) ❌ No ❌ No
# "w+" Write & Read (erases content) ✅ Yes ✅ Yes
# "a+" Append & Read ✅ Yes ❌ No
# "b" Binary mode (rb, wb, etc.) ✅ Yes ✅ Yes/No
# 2. Reading a File (r Mode)
# with open(r"C:\Users\mahammada\Documents\blazor documentaion ch5.txt", "r") as file:
# content = file.read()
# print(content)
# with open(...) automatically closes the file after use.
# Reading Line by Line
# with open(r"C:\Users\mahammada\Documents\blazor documentaion ch5.txt", "r") as file:
# for line in file:
# print(line.strip()) # strip() removes newline characters
# Reading a Specific Number of Characters
# with open(r"C:\Users\mahammada\Documents\blazor documentaion ch5.txt", "r") as file:
# print(file.read(100)) # Reads only 100 characters
# file.seek(0)
# content = file.read()
# print(content[:100])
# def increment(
# num,
# another,
# by=1,
# ):
# return num + by
# print(increment(num=5))
# def sum(*nums):
# total = 0
# for num in nums:
# total += num
# return total
# print(sum(1, 2, 3, 4))
# list = [1, "i", "am", 21.5, "years", "old", "true"]
# for item in list:
# print(item)
# for index in range(len(list)):
# print(list[index])
# list.append("calling append...")
# list.extend(range(5))
# for item in list:
# print(item)
# while list:
# popped = list.pop()
# print(f"popped: {popped}")
# Python's ternary syntax follows this structure:
# value_if_true if condition else value_if_false
# For multiple conditions:
# value1 if condition1 else value2 if condition2 else value3
# Walrus Operator (:=) in Python
# The walrus operator (:=) is called the "assignment expression" in Python.
# It allows you to assign a value to a variable inside an expression
# (like in an if or while statement).
# Syntax & Meaning
# variable := expression
# := assigns the value of expression to variable.
# The whole expression evaluates to the assigned value.
# Example: Without Walrus
# Let's say we want to read user input and check if it's not "exit" before printing it.
# user_input = input("Enter something: ")
# while user_input != "exit":
# print("You entered:", user_input)
# user_input = input("Enter something: ") # Repeating assignment
# We repeat input() twice.
# Example: With Walrus
# Now, let's remove repetition using :=:'
# while (user_input := input("Enter something: ")) != "exit":
# print(f"you entered: {user_input}")
# list = [1, "i", "am", 21.5, "years", "old", "true"]
# list.append("calling append...")
# list.extend(range(5))
# while (popped := list.pop() if list else None) != None:
# print(f"popped element: {popped}")
# some important list operations
# numbers = [10, 20, 30, 40, 50, 10]
# ---
# 1. List Slicing (Getting Subsets of a List)
# print(numbers[1:4]) # [20, 30, 40] (start index 1, end index 4 - 1)
# print(numbers[:3]) # [10, 20, 30] (start from 0, end at 3-1)
# print(numbers[2:]) # [30, 40, 50] (start from 2, go till end)
# print(numbers[-2:]) # [40, 50] (last 2 elements)
# Slicing helps extract parts of a list.
# ---
# 2. Inserting Elements (insert)
# numbers.insert(2, 25) # Insert 25 at index 2
# print(numbers) # [10, 20, 25, 30, 40, 50]
# ---
# 3. Extending a List (extend)
# list1 = [1, 2, 3]
# list2 = [4, 5, 6]
# list1.extend(list2) # Adds elements of list2 to list1
# numbers.extend(list1)
# print(numbers) # [10, 20, 30, 40, 50, 1, 2, 3, 4, 5, 6]
# Use extend() instead of append() when adding multiple items.
# ---
# 4. Removing Elements
# print(numbers)
# num_to_remove = 10
# if num_to_remove in numbers:
# numbers.remove(num_to_remove) # Removes first occurrence of 10
# print(f"deleted. list: {numbers}")
# else:
# print(f"{num_to_remove} not found")
# -
# del numbers[1] # Deletes item at index 1
# print(numbers) # [10, 30, 40, 50, 10]
# ---
# 5. Sorting & Reversing
# numbers.sort() # Sorts in ascending order
# print(numbers) # [10, 10, 20, 30, 40, 50]
#
# numbers.sort(reverse=True) # Sorts in descending order
# print(numbers) # [50, 40, 30, 20, 10, 10]
#
# numbers.reverse() # Reverses the list
# print(numbers) # [10, 10, 20, 30, 40, 50]
# ---
# 6. Checking for an Item (in Operator)
# nums = [1, 2, 3, 4]
# print(3 in nums) # True
# print(5 in nums) # False
# ---
# 7. Getting Index of an Item (index)
# fruits = ["apple", "banana", "cherry"]
# print(fruits.index("apple")) # 0
# print(fruits.index("not known fruit")) # throws exception if item does not exists
# in string find method, if char does not exists, it returns just -1
# ---
# 8️. Counting Occurrences (count)
# nums = [1, 2, 2, 3, 2]
# print(nums.count(2)) # 3 (2 appears 3 times)
# ---
# 9. Clearing a List (clear)
# nums.clear()
# print(nums) # []
# nums = [1, 2, 3, 4, 5]
# for num in nums:
# print(num)
# The enumerate() function in Python is used to loop over a list (or any iterable)
# and get both the index and the value of each item.
# This is especially useful when you need both the index (position) and the item itself
# while iterating over the list.
# What does enumerate() return?
# enumerate() returns an iterator that produces pairs of the form (index, value).
# Each time you iterate over the enumerate object, it yields a tuple containing:
# The index (starting from 0 by default).
# The value from the iterable.
# Syntax of enumerate()
# enumerate(iterable, start=0)
# iterable: The iterable you want to loop over (e.g., a list, string, etc.).
# start: The starting index (optional). By default, it starts at 0,
# but you can specify a different value if needed.
# my_list = ["apple", "banana", "cherry"]
# Using enumerate in a for loop
# for index, value in enumerate(my_list):
# print(f"Index {index}: {value}")
# Index 0: apple
# Index 1: banana
# Index 2: cherry
# Starting the Index from a Different Value
# You can specify a different starting index by passing the start parameter to enumerate().
# Starting index from 1
# for index, value in enumerate(my_list, start=1):
# print(f"Index {index}: {value}")
# Index 1: apple
# Index 2: banana
# Index 3: cherry
# Why use enumerate()?
# Avoid manually tracking the index:
# When you need both the index and the value of the items in the iterable,
# enumerate() makes it easier without manually managing an index variable.
# Cleaner and more Pythonic code:
# It makes your code more concise and easier to read,
# avoiding the need for a separate index counter.
# Using enumerate() with a List and Modifying Items
# You can also use enumerate() if you want to modify elements of the list using the index:
# my_list = ["apple", "banana", "cherry"]
# for index, value in enumerate(my_list):
# my_list[index] = value.upper()
# print(my_list) # ['APPLE', 'BANANA', 'CHERRY']
# print(my_list[2:0:-1])
# print(my_list[2:-1:-1])
# print(my_list[-1::-1])
# print(my_list[::-2])
# --------------------
# What is a Set?
# A set is a collection data type in Python that is unordered and does not allow duplicates.
# It is similar to a mathematical set, where you only store unique items.
# Key Characteristics of Sets:
# 1. Unordered: The elements in a set are not stored in any specific order,
# meaning you can't rely on their position.
# 2. Unique elements: A set automatically removes duplicate values, so every element in a set is unique.
# 3. Mutable: You can add and remove elements from a set after it's created.
# 4. No indexing: Since sets are unordered, you cannot access elements using an index like you would with lists.
# Creating a Set
# You can create a set using curly braces {} or the set() function.
# Using curly braces
# my_set = {1, 2, 3, 4}
# Using set() function
# another_set = set([1, 2, 3, 4])
# Both methods create a set with the elements 1, 2, 3, 4.
# If there are any duplicates in the input, only one instance of each element will remain.
# Set Operations
# Here are some common operations you can perform with sets:
# my_set = {1, 2, 3}
# 1. Adding elements
# You can add elements to a set using the add() method.
# my_set.add(4) # Adds 4 to the set
# my_set.add(1) # Ignores adding 1 to the set
# print(my_set) # Output: {1, 2, 3, 4}
# 2. Removing elements
# You can remove elements using the remove() method,
# which will raise an error if the element does not exist.
# my_set.remove(3) # Removes 3 from the set
# print(my_set) # Output: {1, 2}
# --- If you want to remove an element without raising an error, you can use discard():
# my_set.discard(5) # Does nothing since 5 is not in the set
# print(my_set) # Output: {1, 2, 3}
# 3. Set Union
# The union of two sets combines all the elements from both sets, removing duplicates.
# You can do this using the | operator or the union() method.
# set_a = {1, 2, 3}
# set_b = {3, 4, 5}
# union_set = set_a | set_b # Union using operator
# print(union_set) # Output: {1, 2, 3, 4, 5}
#
# union_set_method = set_a.union(set_b) # Union using method
# print(union_set_method) # Output: {1, 2, 3, 4, 5}
# 4. Set Intersection
# The intersection of two sets gives you a new set with only the elements that are present in both sets.
# You can use the & operator or the intersection() method.
# intersection_set = set_a & set_b # Intersection using operator
# print(intersection_set) # Output: {3}
#
# intersection_set_method = set_a.intersection(set_b) # Intersection using method
# print(intersection_set_method) # Output: {3}
# 5. Set Difference
# The difference of two sets returns a new set with elements that are in the first set but not in the second.
# You can use the - operator or the difference() method.
# difference_set = set_a - set_b # Difference using operator
# print(difference_set) # Output: {1, 2}
#
# difference_set_method = set_a.difference(set_b) # Difference using method
# print(difference_set_method) # Output: {1, 2}
# 6. Set Symmetric Difference
# The symmetric difference of two sets gives you a new set with elements that are in either of the sets,
# but not in both.
# You can use the ^ operator or the symmetric_difference() method.
# symmetric_difference_set = set_a ^ set_b # Symmetric Difference using operator
# print(symmetric_difference_set) # Output: {1, 2, 4, 5}
#
# symmetric_difference_set_method = set_a.symmetric_difference(set_b) # Symmetric Difference using method
# print(symmetric_difference_set_method) # Output: {1, 2, 4, 5}
# Set Properties
#
# Sets are unordered: This means the elements inside the set don’t have a guaranteed order.
# Sets do not allow duplicates: If you try to add an item that already exists in the set,
# it will not be added again.
# my_set = {1, 2, 3, 1, 2}
# print(my_set) # Output: {1, 2, 3}
# --------------------
# What is a Dictionary?
# A dictionary is an unordered collection of items in Python, where each item consists of a key-value pair.
# It is similar to a real-world dictionary where you have a word (the key) and its definition (the value).
# Key Characteristics of Dictionaries:
# 1. Unordered: The items are stored in no particular order.
# However, starting from Python 3.7, dictionaries maintain the insertion order
# (the order in which items were added).
# 2. Key-Value Pairs: Each item in a dictionary is a pair where the key is unique,
# and the value can be any data type.
# 3. Mutable: You can change, add, or remove items from a dictionary after it’s created.
# 4. Keys are Unique: You cannot have duplicate keys in a dictionary.
# If you try to insert a new value for an existing key, the old value will be overwritten.
# 5. Unhashable Keys: Keys must be of a type that is immutable and hashable (e.g., strings, integers, tuples).
# You cannot use mutable types like lists as dictionary keys.
# Creating a Dictionary
# You can create a dictionary using curly braces {} with key-value pairs, separated by a colon :.
# Alternatively, you can use the dict() constructor.
# Using curly braces
# my_dict = {
# "name": "mahammad",
# "surname": "ahmadov",
# "age": 21,
# }
# Using dict() function
# another_dict = dict(name="mahammad", surname="ahmadov", age=21)
# Accessing Items in a Dictionary
# You can access dictionary values by referring to the key inside square brackets [] or using the get() method.
# Using square brackets
# print(my_dict["name"]) # Output: mahammad
# Using get() method
# print(my_dict.get("age")) # Output: 21
# NOTE:
# The get() method is useful because it returns None (or a default value you provide) if the key doesn’t exist,
# instead of raising an error.
# print(my_dict.get("salary", "not found"))
# Adding Items to a Dictionary
# You can add a new key-value pair to a dictionary by simply assigning a value to a new key.
# my_dict["salary"] = 10_000
# print(my_dict)
# Output: {'name': 'mahammad', 'surname': 'ahmadov', 'age': 21, 'salary': 10000}
# Modifying Items in a Dictionary
# You can modify an existing value by using its key.
# my_dict["age"] = 20 + 1
# Removing Items from a Dictionary
# There are several ways to remove items from a dictionary:
# 1. Using del: This will remove a key-value pair by key.
# del my_dict["age"]
# print(my_dict) # {'name': 'mahammad', 'surname': 'ahmadov'}
# 2. Using pop(): This method removes the key-value pair by key and returns the value.
# value = my_dict.pop("age")
# print(value) # Output: 21
# print(my_dict) # Output: {'name': 'mahammad', 'surname': ahmadov}
# 3. Using popitem(): This removes the last inserted key-value pair (from Python 3.7+) and returns it.
# item = my_dict.popitem()
# print(item) # Output: ('age', 21)
# Checking for Keys or Values
# You can check if a key or value exists in a dictionary using the in keyword.
# Checking if a key exists
# print("name" in my_dict) # Output: True
# print("address" in my_dict) # Output: False
#
# Checking if a value exists
# print(30 in my_dict.values()) # Output: False
# print("mahammad" in my_dict.values()) # Output: True
# Dictionary Methods
# Here are some commonly used dictionary methods:
# print(my_dict.keys()) # Output: dict_keys(['name', 'salary'])
# print(my_dict.values()) # Output: dict_values(['Alice', 50000])
# print(my_dict.items()) # Output: dict_items([('name', 'Alice'), ('salary', 50000)])
# another_dict = {"age": 32, "city": "Los Angeles"}
# my_dict.update(another_dict)
# print(
# my_dict
# ) # Output: {'name': 'mahammad', 'surname': 'ahmadov', 'age': 32, 'city': 'Los Angeles'}
# for key, value in my_dict.items():
# print(f"Key: {key}, Value: {value}")
# --------------------------------
# 1. Understanding List Comprehensions
# A list comprehension is a more concise way to create lists compared to using loops.
# Basic Structure:
# [expression for item in iterable]
# Example 1: Creating a List of Squares
# squares = [square**2 for square in range(10)]
# print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# Equivalent loop:
# squares = []
# for x in range(10):
# squares.append(x**2)
# 2. Adding Conditions (Filtering)
# You can add an if condition inside a list comprehension.
# Example 2: Even Numbers from 0 to 20
# even_nums = [num for num in range(21) if num % 2 == 0]
# print(even_nums) # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Equivalent loop:
# evens = []
# for x in range(21):
# if x % 2 == 0:
# evens.append(x)
# 3. Nested Loops in Comprehensions
# You can use multiple loops inside a list comprehension.
# Example 3: Generating (x, y) pairs
# pairs = [(x, y) for x in range(3) for y in range(3)]
# print(pairs)
# for index, value in pairs:
# print(f"Index: {index} = {value}") # list of tuples
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
# Index: 0 = 0
# Index: 0 = 1
# Index: 0 = 2
# Index: 1 = 0
# Index: 1 = 1
# Index: 1 = 2
# Index: 2 = 0
# Index: 2 = 1
# Index: 2 = 2
# 4. Using if-else in List Comprehensions
# You can include if-else statements inside list comprehensions.
# labels = ["even" if x % 2 == 0 else "odd" for x in range(20)]
# print(labels)
# ['even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd']
# Equivalent loop:
# labels = []
# for x in range(10):
# if x % 2 == 0:
# labels.append("Even")
# else:
# labels.append("Odd")
# 5. Multi-Dimensional Lists (Nested List Comprehensions)
# You can generate 2D lists using comprehensions.
# Example 5: Creating a 3×3 Matrix Filled with Zeros
# matrix = [[0 for _ in range(3)] for _ in range(3)]
# print(matrix)
# matrix[1][2] = 31
# print(matrix[1][2]) # Accessing row index 1, column index 2
# Equivalent loop:
# matrix = []
# for _ in range(3):
# row = []
# for _ in range(3):
# row.append(0)
# matrix.append(row)
# 6. Dictionary Comprehensions
# You can create dictionaries using comprehensions.
# squares_dict = {x: x**2 for x in range(5)}
# print(squares_dict)
# Equivalent loop:
# squares_dict = {}
# for x in range(5):
# squares_dict[x] = x**2
# Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
# -------------------------------------------------------
# Understanding *args and **kwargs in Python
# In Python, *args and **kwargs allow you to pass a variable number of arguments to a function.
# 1. *args (Non-Keyword Arguments)
#
# *args allows you to send any number of positional arguments to the function.
# Inside the function, args is treated as a tuple.
# def add_numbers(*args):
# print(args) # tuple containing all the arguments
# return sum(args) # sum all numbers
# print(add_numbers(1, 2, 3, 4, 5)) # Output: 15
# (1, 2, 3, 4, 5)
# 15
# Iterating Over *args
# def print_names(*args):
# for name in args:
# print(name)
# print_names("mahammad", "ahmadov")