Skip to content

Commit 4358ff9

Browse files
committed
Add more DS
1 parent 3342ca4 commit 4358ff9

File tree

1 file changed

+79
-40
lines changed

1 file changed

+79
-40
lines changed

Diff for: README.md

+79-40
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ This guide aims to provide a comprehensive overview of Python basics, data struc
3636
### Data Types
3737

3838
```python
39-
integer = 5
40-
float_number = 5.0
41-
string = "Hello"
42-
list_example = [1, 2, 3]
43-
tuple_example = (1, 2, 3)
44-
set_example = {1, 2, 3}
45-
dictionary_example = {"a": 1, "b": 2}
39+
integer = 5 #int
40+
float_number = 5.0 #float
41+
string = "Hello" #str : immutable
42+
list_example = [1, 2, 3] #list : mutable
43+
tuple_example = (1, 2, 3) #tuple : immutable
44+
set_example = {1, 2, 3} #set : unordered, unique elements
45+
dictionary_example = {"a": 1, "b": 2} #dict : key-value pairs
4646
```
4747

4848
### Control Structures
@@ -53,18 +53,24 @@ dictionary_example = {"a": 1, "b": 2}
5353
if x > 0: # checking if x is positive
5454
elif x < 0: # checking if x is negative
5555
else: # if x is zero
56+
57+
# Ternary Operator
58+
result = "val1" if True else "val2"
5659
```
5760

5861
- **Loops:**
5962

6063
```python
6164
# For Loop
62-
for i in range(5): # prints numbers 0 to 4
63-
for i in reversed(range(5)): # prints numbers 4 to 0
64-
for i in range(4, -1, -1): # prints numbers 4 to 0
65-
for num in arr: # iterate over elements in an array
65+
for i in range(5): pass # prints numbers 0 to 4
66+
for i in reversed(range(5)): pass # prints numbers 4 to 0
67+
for i in range(4, -1, -1): pass # prints numbers 4 to 0
68+
for num in arr: pass # iterate over elements in an array
6669
for index, num in enumerate(arr): # iterate with index
6770
print(index, num)
71+
72+
#single line for loop
73+
squared_list = [x**2 for x in range(5)] # squared_list is [0, 1, 4, 9, 16]
6874

6975
# Using break and continue
7076
for i in range(10):
@@ -82,6 +88,10 @@ i = 0
8288
while i < 5: # prints numbers 0 to 4
8389
print(i)
8490
i += 1
91+
92+
# sort object using attrgetter
93+
from operator import attrgetter
94+
sorted_students = sorted(students, key=attrgetter('grade', 'age'), reverse=True)
8595
```
8696

8797

@@ -165,7 +175,13 @@ sub_list = my_list[1:4] # sub_list is [2, 3, 4]
165175

166176
```python
167177
# Extend list by appending elements from another list
168-
my_list.extend([7, 8]) # my_list is now [1, 2, 3, 4, 5, 6, 7, 8]
178+
my_list.extend([7, 8]) # my_list is now [4, 5, 6, 7, 8]
179+
180+
# Remove and return last element
181+
last_element = my_list.pop() # complexity O(1) (average case, else can be O(n) if resizing is needed)
182+
183+
#combine list uisng plus operator
184+
combined_list = my_list + [9, 10] # combined_list is [4, 5, 6, 7, 8, 9, 10]
169185

170186
# Get the index of an element
171187
index_of_four = my_list.index(4) # index_of_four is 3
@@ -249,58 +265,54 @@ sample_set = {1, 2, 3}
249265
sample_set2 = set([1, 2, 3])
250266
```
251267

268+
269+
270+
### Dictionaries
271+
Dictionaries are a fundamental data structure in Python that allows for the storage of key-value pairs. They are mutable, unordered collections, and each key must be unique.
272+
273+
## Initialization of Dictionaries
274+
275+
**Syntax:**
276+
```python
277+
# Empty dictionary
278+
empty_dict = {}
279+
280+
# Dictionary with elements
281+
sample_dict = {"key1": "value1", "key2": "value2"}
282+
283+
# Using the dict() function
284+
sample_dict2 = dict(key1="value1", key2="value2")
285+
```
286+
252287
- **Common Methods:**
253288
```python
254289
# Adding or Updating Elements
255-
sample_dict = {"key1": "value1"}
290+
sample_dict = dict(key1="value1", key2="value2")
256291
sample_dict["key2"] = "value2" # sample_dict is now {"key1": "value1", "key2": "value2"}
257-
sample_dict["key1"] = "updated_value1" # sample_dict is now {"key1": "updated_value1", "key2": "value2"}
258292

259293
# Removing Elements
260-
sample_dict = {"key1": "value1", "key2": "value2"}
261294
del sample_dict["key2"] # sample_dict is now {"key1": "value1"}
262295
value = sample_dict.pop("key1") # sample_dict is now {}, value is "value1"
263296

264297
# Accessing Elements
265-
sample_dict = {"key1": "value1", "key2": "value2"}
266298
value1 = sample_dict["key1"] # value1 is "value1"
267299
value2 = sample_dict.get("key3", "default_value") # value2 is "default_value"
268300

269301
# Iterating Through a Dictionary
270-
sample_dict = {"key1": "value1", "key2": "value2"}
271302
for key in sample_dict:
272303
print(key, sample_dict[key]) # prints keys and values
304+
273305
for key, value in sample_dict.items():
274306
print(key, value) # prints keys and values
275307

276308
# Checking Membership
277-
sample_dict = {"key1": "value1", "key2": "value2"}
278309
is_present = "key1" in sample_dict # is_present is True
279310

280311
# Finding Length
281-
sample_dict = {"key1": "value1", "key2": "value2"}
282-
dict_length = len(sample_dict) # dict_length is 2
283-
312+
dict_length = len(sample_dict) # returns the number of key-value pairs in the dictionary (2 in this case)
284313
```
285314

286315

287-
### Dictionaries
288-
Dictionaries are a fundamental data structure in Python that allows for the storage of key-value pairs. They are mutable, unordered collections, and each key must be unique.
289-
290-
## Initialization of Dictionaries
291-
292-
**Syntax:**
293-
```python
294-
# Empty dictionary
295-
empty_dict = {}
296-
297-
# Dictionary with elements
298-
sample_dict = {"key1": "value1", "key2": "value2"}
299-
300-
# Using the dict() function
301-
sample_dict2 = dict(key1="value1", key2="value2")
302-
```
303-
304316
**Default Dictionary**
305317

306318
```python
@@ -313,8 +325,7 @@ default_dict = defaultdict(list)
313325
count_dict = defaultdict(int)
314326

315327
# Creating a defaultdict with a custom function
316-
default_dict_custom = defaultdict(lambda: "default_value")
317-
328+
default_dict_custom = defaultdict(lambda : "default_value")
318329
```
319330

320331
- **Common Methods:**
@@ -350,7 +361,35 @@ sample_dict = {"key1": "value1", "key2": "value2"}
350361
dict_length = len(sample_dict) # dict_length is 2
351362

352363
```
364+
365+
### Strings
366+
Strings are a fundamental data type in Python that represent sequences of characters. They are immutable, meaning their elements cannot be changed after creation.
353367

368+
```python
369+
# Ways to Initialize a string
370+
my_string = "Hello, World!"
371+
my_string = str(123) # Convert an integer to a string
372+
# loop through string
373+
for char in my_string:
374+
print(char)
375+
# character ascii
376+
ascii_value = ord('a') # ascii_value is 97
377+
char = chr(97) # char is 'a'
378+
379+
# format string
380+
name = "Alice"
381+
age = 30
382+
formatted_string = f"My name is {name} and I am {age} years old." # Using f-strings
383+
formatted_string = "My name is {} and I am {} years old.".format(name, age) # Using format method
384+
385+
escaped_string = "He said, \"Hello, World!\"" # Use backslash to escape quotes
386+
387+
# String Methods like upp
388+
upper_case = my_string.upper() # upper_case is "HELLO, WORLD!"
389+
lower_case = my_string.lower() # lower_case is "hello, world!"
390+
391+
392+
```
354393

355394
## Advanced Data Structures
356395

0 commit comments

Comments
 (0)