-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path77.py
More file actions
39 lines (30 loc) · 1.06 KB
/
Copy path77.py
File metadata and controls
39 lines (30 loc) · 1.06 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
#1. Positional Parameters
def greet(name, age):
print(f"Hello, {name}! You are {age} years old.")
greet("Alice", 30) # Output: Hello, Alice! You are 30 years old.
#2. Default Parameters
def greet(name, age=25):
print(f"Hello, {name}! You are {age} years old.")
greet("Alice") # Output: Hello, Alice! You are 25 years old.
greet("Bob", 30) # Output: Hello, Bob! You are 30 years old.
#3. Keyword Parameters
def greet(name, age):
print(f"Hello, {name}! You are {age} years old.")
greet(name="Alice", age=30) # Output: Hello, Alice! You are 30 years old.
greet(age=30, name="Alice") # Output: Hello, Alice! You are 30 years old.
#4. Variable-Length Parameters
#Using *args
def sum_numbers(*args):
return sum(args)
print(sum_numbers(1, 2, 3)) # Output: 6
print(sum_numbers(4, 5, 6, 7, 8)) # Output: 30
#Using **kwargs
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Alice", age=30, city="New York")
# Output:
# name: Alice
# age: 30
# city: New York
print("Program by Udit Madaan")