-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27_args_kwargs.py
More file actions
70 lines (63 loc) · 2.77 KB
/
Copy path27_args_kwargs.py
File metadata and controls
70 lines (63 loc) · 2.77 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#======================================================================================
# *Args & **Kwargs
#======================================================================================
#--------------------------------------------------------------------------------------
# 1) *args allows a function to accept any number of positional arguments
#--------------------------------------------------------------------------------------
# Important Points:
#------------------
# args is a tuple.
# Can receive 0, 1 or many positional arguments.
# The name args is conventional; any name works
#-------------------
# When to use *args:
#-------------------
# When you pass similar values
# ex:(1,2,3,4,5)
# ex:('alex', 'alice', 'nora', 'omar', 'leo')
#--------------------------------------------------------------------------------------
def total(*args):
print(type(args))
print(args)
print(sum(args))
total(10,20,30,40,)
total(1,2,3)
print()
#--------------------------------------------------------------------------------------
# 2) **kwargs allows a function to accept any number of keyword arguments
#--------------------------------------------------------------------------------------
# Important Points:
#------------------
# kwargs is a dictionary.
# Stores data as key-value pairs.
# Can receive any number of keyword arguments.
# The name kwargs is conventional; any name works
#----------------------
# When to use **kwargs
#----------------------
# When you pass different types of values
# ex:('alex', 25, 'alex@gmail.com', 10.11.2024')
#--------------------------------------------------------------------------------------
def create_user(**kwargs):
print(type(kwargs))
print(kwargs)
create_user(name = 'Dhoni', role='Batter/Wicket Keeper', country='India')
create_user(name='Virat Kholi', role='Batter', country="India")
# Memory Trick
# *args --> Collects multiple positional arguments into a tuple.
# **kwargs --> Collects multiple keyword arguments into a dictionary
# Difference between *args and **kwargs
#--------------------------------------------------------------------------------------
# Feature | *args | **kwargs
#--------------------------------------------------------------------------------------
# Accepts | Positional arguments | Keyword arguments
# Data Type | Tuple | Dictionary
# Symbol | * | **
# Example | add(1,2,3) | show(name='john', age=23)
#--------------------------------------------------------------------------------------
print()
# Using Both Together
def demo(*args, **kwargs):
print(args)
print(kwargs)
demo('a', 'b', 5, 10, name='John', age=30, country='USA')