Types of Arguments:
1) Positional Arguments
2) Variable Length Arguments
3) Keyword Arguments
4) Default Arguments
5) Keyword Only Arguments
6) Positional Only Arguments
7) Mutable default Arguments
Refer: https://builtin.com/software-engineering-perspectives/arguments-in-python
1) Positional Arguments:
–> During a function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments.
–> Keyword arguments should follow positional arguments only.
Example:1
#Positional Argumentsdef display(first_name, last_name):print("Welcome", first_name, last_name)display("Guru", "Prasanna")#Positional Arguments def display(first_name, last_name): print("Welcome", first_name, last_name) display("Guru", "Prasanna")#Positional Arguments def display(first_name, last_name): print("Welcome", first_name, last_name) display("Guru", "Prasanna")
Enter fullscreen mode Exit fullscreen mode
Output:
Welcome Guru Prasanna
Example:2
def find_total(l):total = 0for num in l:total+=numprint(total)marks = [90,56, 78]find_total(marks)def find_total(l): total = 0 for num in l: total+=num print(total) marks = [90,56, 78] find_total(marks)def find_total(l): total = 0 for num in l: total+=num print(total) marks = [90,56, 78] find_total(marks)
Enter fullscreen mode Exit fullscreen mode
Output:
224
2) Variable Length Arguments:
–> Variable-length arguments are also known as arbitrary arguments.
–> Same method/function with different number of arguments or with different types of arguments.
–> If we don’t know the number of arguments needed for the function in advance, we can use arbitrary arguments.
–> Arbitrary arguments come in two types:
- Arbitrary positional arguments- Arbitrary keyword arguments- Arbitrary positional arguments - Arbitrary keyword arguments- Arbitrary positional arguments - Arbitrary keyword arguments
Enter fullscreen mode Exit fullscreen mode
- Arbitrary positional arguments: An asterisk (*) is placed before a parameter in function definition which can hold non-keyword variable-length arguments.These arguments will be wrapped up in a tuple.
Example:
def find_total(*l):total = 0for num in l:total+=numprint(total)mark1 = 100mark2 = 90mark3 = 87find_total(mark1, mark2,mark3)find_total(45,54)find_total()def find_total(*l): total = 0 for num in l: total+=num print(total) mark1 = 100 mark2 = 90 mark3 = 87 find_total(mark1, mark2,mark3) find_total(45,54) find_total()def find_total(*l): total = 0 for num in l: total+=num print(total) mark1 = 100 mark2 = 90 mark3 = 87 find_total(mark1, mark2,mark3) find_total(45,54) find_total()
Enter fullscreen mode Exit fullscreen mode
Output:
277990277 99 0277 99 0
Enter fullscreen mode Exit fullscreen mode
- Arbitrary keyword arguments:A double asterisk (**) is placed before a parameter in a function which can hold keyword variable-length arguments.
Example:
def login(**args):print(args)login(name='raja', age=25, city='madurai')def login(**args): print(args) login(name='raja', age=25, city='madurai')def login(**args): print(args) login(name='raja', age=25, city='madurai')
Enter fullscreen mode Exit fullscreen mode
Output:
{'name': 'raja', 'age': 25, 'city': 'madurai'}{'name': 'raja', 'age': 25, 'city': 'madurai'}{'name': 'raja', 'age': 25, 'city': 'madurai'}
Enter fullscreen mode Exit fullscreen mode
3) Keyword Arguments:
–> All parameters are given as keyword arguments.
–> Keyword arguments should be given while calling the function, not in the function definition.
Example:
def display(*marks,name):print('Welcome', name)print(marks)display(90,80,name="Lakshmi")display(80,name="guru")def display(*marks,name): print('Welcome', name) print(marks) display(90,80,name="Lakshmi") display(80,name="guru")def display(*marks,name): print('Welcome', name) print(marks) display(90,80,name="Lakshmi") display(80,name="guru")
Enter fullscreen mode Exit fullscreen mode
Output:
Welcome Lakshmi(90, 80)Welcome guru(80,)Welcome Lakshmi (90, 80) Welcome guru (80,)Welcome Lakshmi (90, 80) Welcome guru (80,)
Enter fullscreen mode Exit fullscreen mode
4) Default Arguments:
- Default arguments are values that are provided while defining functions.
- The assignment operator = is used to assign a default value to the argument.
- Default arguments become optional during the function calls.
- If we provide a value to the default arguments during function calls, it overrides the default value.
- The function can have any number of default arguments.
- Default arguments should follow non-default arguments.
Example:
def login(username, password="admin"):print(username, password)login("abcd", "abcd")login("pqrs")def login(username, password="admin"): print(username, password) login("abcd", "abcd") login("pqrs")def login(username, password="admin"): print(username, password) login("abcd", "abcd") login("pqrs")
Enter fullscreen mode Exit fullscreen mode
Output:
abcd abcdpqrs adminabcd abcd pqrs adminabcd abcd pqrs admin
Enter fullscreen mode Exit fullscreen mode
Default vs. Keyword vs. Positional Arguments
Important Points to Remember for Default, Keyword and Positional Arguments
5) Keyword Only Arguments:
–> To mark parameters as keyword-only, place an * in the arguments list just before the first keyword-only parameter.
Example:
def add(*, no1, no2):return no1+no2print(add(10,20)) #TypeErrorprint(add(no1=100,no2=200))def add(*, no1, no2): return no1+no2 print(add(10,20)) #TypeError print(add(no1=100,no2=200))def add(*, no1, no2): return no1+no2 print(add(10,20)) #TypeError print(add(no1=100,no2=200))
Enter fullscreen mode Exit fullscreen mode
Output:
TypeError300TypeError 300TypeError 300
Enter fullscreen mode Exit fullscreen mode
6) Positional Only Arguments:
–> Positional-only parameters are placed before a / (forward-slash) in the function definition.
–> The / is used to logically separate the positional-only parameters from the rest of the parameters.
–> Parameters following the / may be positional-or-keyword or keyword-only.
def add(a,b,/,c,d):return a+b+c+dprint (add(3,4,c=1,d=2))print (add(3,4,2,d=1))def add(a,b,/,c,d): return a+b+c+d print (add(3,4,c=1,d=2)) print (add(3,4,2,d=1))def add(a,b,/,c,d): return a+b+c+d print (add(3,4,c=1,d=2)) print (add(3,4,2,d=1))
Enter fullscreen mode Exit fullscreen mode
Output:
101010 1010 10
Enter fullscreen mode Exit fullscreen mode
7) Mutable default Arguments
#Mutable Default Arguments:def add(no, l=[]):l.append(no)return lprint(add(10))print(add(20))#Mutable Default Arguments: def add(no, l=[]): l.append(no) return l print(add(10)) print(add(20))#Mutable Default Arguments: def add(no, l=[]): l.append(no) return l print(add(10)) print(add(20))
Enter fullscreen mode Exit fullscreen mode
Output:
[10][10, 20][10] [10, 20][10] [10, 20]
Enter fullscreen mode Exit fullscreen mode
Functions returning dictionary:
def display(player_names, scores):return dict(zip(player_names, scores))player_names = ['virat', 'rohit']scores = [100,105]result = display(player_names, scores)print(result)def display(player_names, scores): return dict(zip(player_names, scores)) player_names = ['virat', 'rohit'] scores = [100,105] result = display(player_names, scores) print(result)def display(player_names, scores): return dict(zip(player_names, scores)) player_names = ['virat', 'rohit'] scores = [100,105] result = display(player_names, scores) print(result)
Enter fullscreen mode Exit fullscreen mode
Output:
{'virat': 100, 'rohit': 105}{'virat': 100, 'rohit': 105}{'virat': 100, 'rohit': 105}
Enter fullscreen mode Exit fullscreen mode
Lists are mutable:
def modify(l):l.append(100)print(f'Inside modify, {l}')l = [10,20,30]print(f'Outside modify {l}')modify(l)print(f'Outside modify2 {l}')def modify(l): l.append(100) print(f'Inside modify, {l}') l = [10,20,30] print(f'Outside modify {l}') modify(l) print(f'Outside modify2 {l}')def modify(l): l.append(100) print(f'Inside modify, {l}') l = [10,20,30] print(f'Outside modify {l}') modify(l) print(f'Outside modify2 {l}')
Enter fullscreen mode Exit fullscreen mode
Output:
Outside modify [10, 20, 30]Inside modify, [10, 20, 30, 100]Outside modify2 [10, 20, 30, 100]Outside modify [10, 20, 30] Inside modify, [10, 20, 30, 100] Outside modify2 [10, 20, 30, 100]Outside modify [10, 20, 30] Inside modify, [10, 20, 30, 100] Outside modify2 [10, 20, 30, 100]
Enter fullscreen mode Exit fullscreen mode
Explanation:
- Before calling
modify(l)
, the list is[10, 20, 30]
. - Inside
modify(l)
, 100 is added to the list. - After the function call, the original list is now
[10, 20, 30, 100]
because lists are mutable and passed by reference.
Types of variables:
Local Variables: A variable declared inside a function and accessible only within that function.
Example:
def shopping():print(amount)amount = 1000print(amount)shopping()def shopping(): print(amount) amount = 1000 print(amount) shopping()def shopping(): print(amount) amount = 1000 print(amount) shopping()
Enter fullscreen mode Exit fullscreen mode
Output:
1000
Global Variables: A variable declared outside a function and accessible throughout the program.Use global keyword to modify the variable.
Example:1
amount = 1000def shopping():print(amount)shopping()amount = 1000 def shopping(): print(amount) shopping()amount = 1000 def shopping(): print(amount) shopping()
Enter fullscreen mode Exit fullscreen mode
Output:
1000
Example:2
amount = 10000 #Global variabledef shopping():global amountprint("Total Amount for Trip is",amount)amount = 1000 #Local variableprint("Shopping Amount",amount)shopping()amount = 10000 #Global variable def shopping(): global amount print("Total Amount for Trip is",amount) amount = 1000 #Local variable print("Shopping Amount",amount) shopping()amount = 10000 #Global variable def shopping(): global amount print("Total Amount for Trip is",amount) amount = 1000 #Local variable print("Shopping Amount",amount) shopping()
Enter fullscreen mode Exit fullscreen mode
Output:
Total Amount for Trip is 10000Shopping Amount 1000Total Amount for Trip is 10000 Shopping Amount 1000Total Amount for Trip is 10000 Shopping Amount 1000
Enter fullscreen mode Exit fullscreen mode
Note:
–> Local and global variables can have same names.
–> Local variable will be precedence.
Inner Function:An inner function (or nested function) is a function defined inside another function.
Nonlocal Variables (Used Inside Nested Functions): A variable declared inside a nested function but referring to a variable in an enclosing (outer) function.Use nonlocal keyword to modify the variable.
Example:
def open_tank():tank = 2print(f'Inside open_tank {tank}')def fill_petrol():nonlocal tanktank = tank + 3print(f'Inside fill_petrol {tank}')fill_petrol()open_tank()def open_tank(): tank = 2 print(f'Inside open_tank {tank}') def fill_petrol(): nonlocal tank tank = tank + 3 print(f'Inside fill_petrol {tank}') fill_petrol() open_tank()def open_tank(): tank = 2 print(f'Inside open_tank {tank}') def fill_petrol(): nonlocal tank tank = tank + 3 print(f'Inside fill_petrol {tank}') fill_petrol() open_tank()
Enter fullscreen mode Exit fullscreen mode
Output:
Inside open_tank 2Inside fill_petrol 5Inside open_tank 2 Inside fill_petrol 5Inside open_tank 2 Inside fill_petrol 5
Enter fullscreen mode Exit fullscreen mode
暂无评论内容