Kwargs
Kwargs
# Example call
print_kwargs(name="Alice", age=25, country="USA")
Output:
makefile
Copy code
name: Alice
age: 25
country: USA
# Example call
1
check_key(age=30, city="London")
Output:
Copy code
Name not provided.
# Example call
student_info(name="Bob")
Output:
yaml
Copy code
Student Name: Bob
Student Age: Not specified
2
# Example call
describe_pet("Dog", name="Buddy", age=3, breed="Golden Retriever")
Output:
yaml
Copy code
Pet type: Dog
name: Buddy
age: 3
breed: Golden Retriever
# Example call
count_kwargs(name="Sarah", age=22, city="Paris")
Output:
javascript
Copy code
Number of keyword arguments: 3
3
# Example call
my_dict = {"a": 1, "b": 2}
updated = update_dict(my_dict, b=3, c=4)
print(updated)
Output:
css
Copy code
{'a': 1, 'b': 3, 'c': 4}
# Example call
result = create_dict(name="Emma", age=29, city="New York")
print(result)
Output:
arduino
Copy code
{'name': 'Emma', 'age': 29, 'city': 'New York'}
4
# Example call
filter_even_numbers(a=1, b=2, c=3, d=4)
Output:
makefile
Copy code
b: 2
d: 4
# Example call
greet("Alice", age=25, city="Berlin", hobby="Reading")
Output:
makefile
Copy code
Hello, Alice!
age: 25
city: Berlin
hobby: Reading
5
Copy code
def calculate(operation, **kwargs):
result = 0 if operation == "add" else 1
for key, value in kwargs.items():
if operation == "add":
result += value
elif operation == "multiply":
result *= value
print(f"Result: {result}")
# Example call
calculate("add", a=10, b=20, c=30)
calculate("multiply", x=2, y=3, z=4)
Output:
makefile
Copy code
Result: 60
Result: 24
These examples cover a variety of basic use cases for **kwargs to help you
understand its flexibility and power!
6
Inside the function, Python collects these arguments into a dictionary.
Syntax
python
Copy code
def function_name(**kwargs):
# kwargs is a dictionary
for key, value in kwargs.items():
print(f"{key}: {value}")
How it Works:
1. The function display_info uses **kwargs.
2. Inside the function, kwargs is a dictionary containing the passed keyword
arguments (name, age, country).
3. The for loop iterates through the dictionary to print each key-value pair.
7
Output:
yaml
Copy code
Keyword arguments received:
name: Alice
age: 30
country: USA
8
9
What are Return Values in Python
Functions?
In Python, a return value is the output of a function. It is the value that a
function sends back to the caller (where the function was called).
The return keyword is used to specify the value that a function
should return.
10
A function can return:
o A single value
o Multiple values
o No value (implicitly returns None if no return is used)
Syntax
python
Copy code
def function_name(parameters):
# Code block
return value
11
2. Returning Multiple Values
python
Copy code
def get_name_and_age():
name = "Alice"
age = 25
return name, age
12
None
Key Points:
1. A function stops executing as soon as it encounters a return
statement.
2. If no return is specified, the function implicitly returns None.
3. You can return any data type (integers, strings, lists, dictionaries,
etc.).
13
What is the pass Statement in
Python?
The pass statement in Python is a placeholder. It does nothing and is
used when a statement is syntactically required but no action is needed.
It is often used in the following cases:
1. To define an empty function, class, or loop as a placeholder while
building the code.
14
2. To avoid syntax errors when the body of a function or block is yet to
be implemented.
Key Characteristics:
It is a no-operation (NOP) statement.
It is a way to write valid Python code without functionality in certain
blocks.
Syntax
python
Copy code
def function_name():
pass # This does nothing for now
Code Examples
1. Using pass in a Function
python
Copy code
def my_function():
pass # Placeholder for future implementation
15
pass # Placeholder for loop body
if x > 5:
pass # Placeholder for 'if' block
else:
print("x is 5 or less")
16
print("Code executed successfully!")
Output:
css
Copy code
Code executed successfully!
17