DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 30 - Types of arguments in Python

Types of Arguments:
Positional Arguments:
The values are assigned to the parameters in the order in which they are passed.

def display(first_name, last_name):
    print("Welcome", first_name, last_name)
display("Lakshmi", "Pritha")
Enter fullscreen mode Exit fullscreen mode
Welcome Lakshmi Pritha
Enter fullscreen mode Exit fullscreen mode
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
224
Enter fullscreen mode Exit fullscreen mode

Variable length arguments:

variable length arguments also called as arbitrary arguments.
Same method/function with different number of arguments or with different types of arguments.
Sometimes we don’t know how many arguments will be passed to a function. Python provides ways to handle this with *

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
277
99
0
Enter fullscreen mode Exit fullscreen mode

keyword arguments:

keyword arguments by specifying the parameter name followed by an equal sign (=) and the value you want to assign to it.

def display(*marks,name):
    print('Welcome', name)
    print(marks)


display(90,80,name="Lakshmi")
display(80,name="pritha")
Enter fullscreen mode Exit fullscreen mode
Welcome Lakshmi
(90, 80)
Welcome pritha
(80,)
Enter fullscreen mode Exit fullscreen mode

Default Arguments:

Default arguments are parameters that have default values. If the argument is not passed when calling the function, the default value is used.

def login(username, password="admin"):
    print(username, password)


login("abcd", "abcd")
login("pqrs")
Enter fullscreen mode Exit fullscreen mode
abcd abcd
pqrs admin
Enter fullscreen mode Exit fullscreen mode

Keyword variable length arguments:

keyword variable-length arguments allow you to pass a variable number of keyword arguments to a function.

def login(**args):
    print(args)
login(name='raja')

Enter fullscreen mode Exit fullscreen mode
{'name': 'raja'}
Enter fullscreen mode Exit fullscreen mode
def login(**args):
    print(args)
login(name='raja', age=25, city='madurai')
Enter fullscreen mode Exit fullscreen mode
{'name': 'raja', 'age': 25, 'city': 'madurai'}
Enter fullscreen mode Exit fullscreen mode
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
Outside modify [10, 20, 30]
Inside modify, [10, 20, 30, 100]
Outside modify2 [10, 20, 30, 100]
Enter fullscreen mode Exit fullscreen mode

Keyword only arguments:

Keyword-only arguments must be specified by name when calling the function.
The * symbol is used in the function signature to indicate that subsequent arguments are keyword-only.

def add(*, no1, no2):
    return no1+no2

print(add(10,20)) # It returns error
print(add(no1=100,no2=200)) #output:300

Enter fullscreen mode Exit fullscreen mode

Function returning dictionary:

In Python, a function returning a dictionary simply means that the function creates and returns a dictionary object.

The zip() function combines the two lists element-wise, and dict() converts the resulting pairs into a 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)

Enter fullscreen mode Exit fullscreen mode
{'virat': 100, 'rohit': 105}
Enter fullscreen mode Exit fullscreen mode

Mutable default arguments:

When you use a list or dictionary as a default argument, it’s initialized only once. If the argument is modified during the function call, the changes persist across future calls.

def add(no, l=[]):
    l.append(no)
    return l

print(add(10))
print(add(20))

Enter fullscreen mode Exit fullscreen mode
[10]
[10, 20]
Enter fullscreen mode Exit fullscreen mode
def add(no, l=None):
    l.append(no)
    return l

print(add(10))
print(add(20))
Enter fullscreen mode Exit fullscreen mode
AttributeError: 'NoneType' object has no attribute 'append'

Enter fullscreen mode Exit fullscreen mode
def add(no, l=None):
    l = []
    l.append(no)
    return l

print(add(10))
print(add(20))

Enter fullscreen mode Exit fullscreen mode
[10]
[20]
Enter fullscreen mode Exit fullscreen mode

Types of variables:

Global variable:

Defined Outside Functions: Global variables are usually declared outside of all functions, at the top level of the code.

no = 10
def f1():
    print(no)

def f2():
    print(no)

f1()
f2()
Enter fullscreen mode Exit fullscreen mode
10
10
Enter fullscreen mode Exit fullscreen mode

Local variable:

Local variables are declared inside functions or blocks of code.

no = 10
def f1():
    no2 = 100
    print(no)
    print(no2)

f1()
Enter fullscreen mode Exit fullscreen mode

10
100
Enter fullscreen mode Exit fullscreen mode

If local and global variable can have same value means the precedence given to the local variable.

amount = 10000
def shopping():
    amount = 1000
    print(amount)

shopping()
Enter fullscreen mode Exit fullscreen mode
1000
Enter fullscreen mode Exit fullscreen mode

The global keyword allows you to modify the global variable amount inside the function.

amount = 10000
def shopping():
    global amount
    print("Total Amount for Trip is",amount)
    amount = 1000
    print("Shopping Amount",amount)

shopping()

Enter fullscreen mode Exit fullscreen mode
Total Amount for Trip is 10000
Shopping Amount 1000
Enter fullscreen mode Exit fullscreen mode

Inner functions:

Inner functions are functions defined inside other functions.
They can access variables from their outer function.

def open_fridge():
    capacity = 165
    print("opening fridge", capacity)
    def take_water():
        capacity = 1
        print("Drink Water", capacity)
    take_water()

open_fridge()

Enter fullscreen mode Exit fullscreen mode
opening fridge 165
Drink Water 1
Enter fullscreen mode Exit fullscreen mode
def open_fridge():
    capacity = 165
    print("opening fridge", capacity)
    def take_water():
        print("Drink Water", capacity)
    take_water()

open_fridge()

Enter fullscreen mode Exit fullscreen mode
opening fridge 165
Drink Water 165
Enter fullscreen mode Exit fullscreen mode
def open_tank():
    tank = 2
    print(f'Inside open_tank {tank}')
    def fill_petrol():
        nonlocal tank # Refers to the variable 'tank' from the enclosing scope
        tank = tank + 3
        print(f'Inside fill_petrol {tank}')
    fill_petrol()

open_tank()
Enter fullscreen mode Exit fullscreen mode
Inside open_tank 2
Inside fill_petrol 5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)