Skip to content

Commit 4e46780

Browse files
committed
Day-20,21 added
1 parent 65f54bf commit 4e46780

File tree

2 files changed

+373
-0
lines changed

2 files changed

+373
-0
lines changed

Day-20/notes.md

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
# Day 20 – Dictionary in Python
2+
3+
## 🎯 Topics Covered
4+
5+
- Dictionary
6+
- `dict()`
7+
- `get()`
8+
- `update()`
9+
- `pop()`
10+
- `popitem()`
11+
- `clear()`
12+
- `items()`
13+
- `keys()`
14+
- `values()`
15+
16+
---
17+
18+
# What is a Dictionary?
19+
20+
- A dictionary in Python is an unordered, mutable, and indexed collection of key–value pairs.
21+
It is used to store data values like a real-world dictionary — where each key maps to a specific value.
22+
23+
# Example:
24+
```python
25+
student = {"name": "Vinaal", "age": 21, "course": "Python"}
26+
print(student)
27+
```
28+
29+
---
30+
31+
## Creating a Dictionary
32+
33+
You can create a dictionary using curly braces {} or the dict() constructor.
34+
35+
---
36+
37+
## Using curly braces
38+
```python
39+
info = {"brand": "Tesla", "model": "Model X", "year": 2025}
40+
```
41+
42+
## Using dict() constructor
43+
```python
44+
data = dict(name="Dark AI", skill="Full Stack Developer")
45+
```
46+
47+
---
48+
49+
## Accessing Dictionary Elements
50+
51+
### Using Key
52+
```python
53+
person = {"name": "Alice", "age": 25}
54+
print(person["name"]) # Output: Alice
55+
```
56+
57+
### Using get()
58+
59+
- Safely returns the value of a key. If the key doesn’t exist, it returns None instead of an error.
60+
```python
61+
person = {"name": "Alice", "age": 25}
62+
print(person.get("age")) # Output: 25
63+
print(person.get("city")) # Output: None
64+
```
65+
66+
### Modifying Dictionary Elements
67+
68+
- You can update an existing key or add a new one.
69+
```python
70+
car = {"brand": "BMW", "year": 2020}
71+
car["year"] = 2025 # Update value
72+
car["color"] = "Black" # Add new key-value pair
73+
print(car)
74+
```
75+
76+
---
77+
78+
## Dictionary Methods
79+
80+
### keys()
81+
82+
- Returns all keys from the dictionary.
83+
```python
84+
person = {"name": "Vinaal", "age": 21, "course": "Python"}
85+
print(person.keys())
86+
```
87+
88+
### Output
89+
```python
90+
dict_keys(['name', 'age', 'course'])
91+
```
92+
93+
### values()
94+
95+
- Returns all values from the dictionary.
96+
```python
97+
person = {"name": "Vinaal", "age": 21}
98+
print(person.values())
99+
```
100+
101+
### Output
102+
```python
103+
dict_values(['Vinaal', 21])
104+
```
105+
106+
### items()
107+
108+
- Returns all key–value pairs as tuples.
109+
```python
110+
person = {"name": "Vinaal", "age": 21}
111+
print(person.items())
112+
```
113+
114+
### Output
115+
```python
116+
dict_items([('name', 'Vinaal'), ('age', 21)])
117+
```
118+
119+
### update()
120+
121+
- Merges another dictionary or adds new key–value pairs.
122+
```python
123+
student = {"name": "Dark", "age": 20}
124+
student.update({"age": 21, "course": "Python"})
125+
print(student)
126+
```
127+
128+
### Output
129+
```python
130+
{'name': 'Dark', 'age': 21, 'course': 'Python'}
131+
```
132+
133+
### pop()
134+
135+
- Removes a specific key and returns its value.
136+
```python
137+
data = {"x": 10, "y": 20, "z": 30}
138+
data.pop("y")
139+
print(data)
140+
```
141+
142+
### Output
143+
```python
144+
{'x': 10, 'z': 30}
145+
```
146+
147+
### popitem()
148+
149+
- Removes the last inserted key–value pair.
150+
```python
151+
info = {"a": 1, "b": 2, "c": 3}
152+
info.popitem()
153+
print(info)
154+
```
155+
156+
### Output
157+
```python
158+
{'a': 1, 'b': 2}
159+
```
160+
161+
### clear()
162+
163+
- Removes all items from the dictionary.
164+
```python
165+
info = {"x": 100, "y": 200}
166+
info.clear()
167+
print(info)
168+
```
169+
170+
### Output
171+
```python
172+
{}
173+
```
174+
175+
---
176+
177+
## Looping Through a Dictionary
178+
```python
179+
# Looping through keys:
180+
car = {"brand": "Tesla", "model": "S", "year": 2025}
181+
for key in car:
182+
print(key)
183+
184+
Looping through values:
185+
for value in car.values():
186+
print(value)
187+
188+
Looping through both keys and values:
189+
for key, value in car.items():
190+
print(key, ":", value)
191+
```
192+
193+
---
194+
195+
## Copying a Dictionary
196+
197+
- You can use copy() to make a duplicate dictionary.
198+
```python
199+
original = {"name": "Dark", "lang": "Python"}
200+
duplicate = original.copy()
201+
print(duplicate)
202+
```
203+
204+
---
205+
206+
## Nested Dictionary
207+
208+
- A dictionary inside another dictionary.
209+
```python
210+
students = {
211+
"student1": {"name": "Alice", "age": 20},
212+
"student2": {"name": "Bob", "age": 22}
213+
}
214+
215+
print(students["student1"]["name"])
216+
# Output: Alice
217+
```
218+
219+
---
220+
221+
## Built-in Functions for Dictionary
222+
```python
223+
len() # Returns total number of key-value pairs
224+
str() # Converts dictionary into a string
225+
type() # Returns the type of the object
226+
```
227+
228+
---
229+
230+
## 📘 *Next step (Day 20):*
231+
232+
I’ll explore **Function**.

Day-21/notes.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Day 21 – Functions and Arguments in Python
2+
3+
## 🔹 What is a Function?
4+
5+
- A function is a block of reusable code that performs a specific task.
6+
It helps make programs more modular, readable, and reduces repetition.
7+
8+
### Syntax:
9+
```python
10+
def function_name(parameters):
11+
# block of code
12+
return value
13+
```
14+
15+
### Example:
16+
```python
17+
def greet():
18+
print("Hello, welcome to Python learning!")
19+
```
20+
21+
#### To call the function:
22+
23+
`greet()`
24+
25+
---
26+
27+
### 🔹 Why Use Functions?
28+
29+
- Increases code reusability.
30+
- Makes code modular and organized.
31+
- Easy to debug and maintain.
32+
- Avoids repetition of logic.
33+
34+
---
35+
36+
## Types of Arguments in Python Functions
37+
38+
- Python supports multiple types of arguments to make functions flexible and powerful.
39+
40+
---
41+
42+
### 1. Positional Arguments
43+
44+
- Arguments passed in the same order as defined in the function.
45+
```python
46+
def student(name, age):
47+
print("Name:", name)
48+
print("Age:", age)
49+
50+
student("Vinaal", 21)
51+
```
52+
53+
### Output:
54+
```python
55+
Name: Vinaal
56+
Age: 21
57+
```
58+
59+
> ⚠️ The order matters — if you swap them, values will mismatch.
60+
61+
---
62+
63+
### 2. Default Arguments
64+
65+
- A parameter can have a default value which is used if no argument is provided.
66+
```python
67+
def greet(name="User"):
68+
print("Hello,", name)
69+
70+
greet("Dark AI")
71+
greet()
72+
```
73+
74+
### Output:
75+
```python
76+
Hello, Dark AI
77+
Hello, User
78+
```
79+
80+
---
81+
82+
### 3. Keyword Arguments
83+
84+
- You can specify arguments by their parameter names, regardless of order.
85+
```python
86+
def display(name, age):
87+
print(f"Name: {name}, Age: {age}")
88+
89+
display(age=22, name="TechnoSport")
90+
```
91+
92+
### Output:
93+
```python
94+
Name: TechnoSport, Age: 22
95+
```
96+
97+
---
98+
99+
### 4. Variable-Length (Arbitrary) Arguments
100+
101+
- When you don’t know how many arguments will be passed, use:
102+
- *args → for non-keyworded variable arguments (tuple)
103+
104+
#### Using *args
105+
```python
106+
def add_numbers(*args):
107+
print("Numbers:", args)
108+
print("Sum:", sum(args))
109+
110+
add_numbers(10, 20, 30)
111+
```
112+
113+
### Output:
114+
```python
115+
Numbers: (10, 20, 30)
116+
Sum: 60
117+
```
118+
119+
---
120+
121+
## Return Statement
122+
123+
- The return keyword sends a result back to the caller.
124+
```python
125+
def multiply(a, b):
126+
return a * b
127+
128+
result = multiply(5, 3)
129+
print("Result:", result)
130+
```
131+
132+
### Output:
133+
```python
134+
Result: 15
135+
```
136+
137+
---
138+
139+
## 📘 *Next step (Day 20):*
140+
141+
I’ll explore **Advanced Functions**.

0 commit comments

Comments
 (0)