Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions day2/solutions/task2/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Python: Understanding *args and **kwargs

This mini project explains how to use `*args` and `**kwargs` in Python functions. These features help you write flexible, reusable, and dynamic functions that can handle varying numbers of inputs.

---

## 📌 What is `*args`?

- `*args` allows a function to accept any number of **positional arguments**.
- These arguments are stored as a **tuple** inside the function.
- Useful when you’re not sure how many values will be passed.

### ✅ Example of `*args`

```python
def add_numbers(*args):
total = sum(args)
print("Sum:", total)

add_numbers(10, 20, 30)
```

---

## 📌 What is `**kwargs`?

- `**kwargs` allows a function to accept any number of **keyword arguments**.
- These arguments are stored as a **dictionary** inside the function.
- Useful for handling optional or dynamic key-value inputs.

### ✅ Example of `**kwargs`

```python
def print_user_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")

print_user_info(name="Alice", age=25, city="London")
```
29 changes: 29 additions & 0 deletions day2/solutions/task2/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Task=2

def student_result(**kwargs):
print(kwargs) # Inside the function, kwargs is a dictionary
failed_marks=37
total=0
max_marks=0
max_user=''
failed_user=[]
for name in kwargs:
total=total+kwargs[name]

#for max user
if kwargs[name]>max_marks:
max_marks=kwargs[name]
max_user=name

#for failed user
if kwargs[name]<failed_marks:
failed_user.append(name)

#print(total)
average_marks=total/len(kwargs)
print('Average Marks is:-',average_marks)
print(f'{max_user}:{max_marks}')
print(failed_user)


student_result(gaurav=72,sundeep=56,raj=27,shivam=29)
31 changes: 31 additions & 0 deletions day2/solutions/task3/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 🧾 User Info Printer

This Python script traverses a nested dictionary structure and prints all user-related information, including each user's details and a list of their friends.

---

## 🧠 Logic Breakdown

1. Loop through each user in the list `a_dict`.
2. For each user (which is a dictionary), iterate through all key-value pairs using `.items()`.
3. If the value is a list (like the `"frieds"` key), pass it to the `fried_func()` to handle nested friend data.
4. Otherwise, print the key-value pair directly.
5. The `fried_func()` is a helper function that:
- Loops through each friend dictionary.
- Prints each friend’s information with a tab for better formatting.

---

## 🧰 Function & Method Explanations

### 🔁 `dict.items()`

- In Python, the .items() method is a built-in dictionary method that returns a view object. This view object contains all the key-value pairs of the dictionary as tuples, where each tuple is in the format (key, value).

#### Example:
```python
{"name": "gara"}.items() # ➝ [('name', 'gara')]
```
### 🧪 `isinstance()`

- isinstance(object, type) is used to check the type of a value at runtime.
17 changes: 17 additions & 0 deletions day2/solutions/task3/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Task-3


def fried_func(fried):
for i in fried:
for k,v in i.items():
print(f'\t{k}:-{v}')

for i in a_dict:
#print(i)
#print (i.items())
for key,value in i.items():
if isinstance(value,list):
print(f'{key}:- ')
fried_func(value)
else:
print(f'{key} :- {value}')