0% found this document useful (0 votes)
6 views

Function Part2

python functions

Uploaded by

vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Function Part2

python functions

Uploaded by

vijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Keyword and Positional Argument in Python

1. Keyword-Only Arguments
Keyword-only arguments mean whenever we pass the arguments (or value) by their parameter names at the time of calling the function in Python in which if you change
the position of arguments then there will be no change in the output.

def nameAge(name, age):


print("Hi, I am", name)
print("My age is ", age)

nameAge(name="Prince", age=20)

nameAge(age=20, name="Prince")

Output
Hi, I am Prince
My age is 20
Hi, I am Prince
My age is 20

2. Positional-Only Arguments
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)

# You will get correct output because argument is given in order


print("Case-1:")
nameAge("Prince", 20)
# You will get incorrect output because argument is not in order
print("\nCase-2:")
nameAge(20, "Prince")

Output
Case-1:
Hi, I am Prince
My age is 20

Case-2:
Hi, I am 20
My age is Prince

Example 2
def minus(a, b):
return a - b

a, b = 20, 10
result1 = minus(a, b)
print("Used Positional arguments:", result1)

# you will get incorrect output because


# expected was (a-b) but you will be getting (b-a)
# because of swapped position of value a and b

result2 = minus(b, a)
print("Used Positional arguments:", result2)

Output
Used Positional arguments: 10
Used Positional arguments: -10

How to find the number of arguments in a Python function?


def no_of_argu(*args):

# using len() method in args to count


return(len(args))

a = 1
b = 3

# arguments passed
n = no_of_argu(1, 2, 4, a)

# result printed
print(" The number of arguments are: ", n)

Output :
The number of arguments passed are: 4
Example 2:
Python3
def no_of_argu(*args):

# using len() method in args to count


return(len(args))

print(no_of_argu(2, 5, 4))
print(no_of_argu(4, 5, 6, 5, 4, 4))
print(no_of_argu(3, 2, 32, 4, 4, 52, 1))
print(no_of_argu(1))

Output :
3
6
7
1

Default arguments in Python


Syntax:
def function_name(param1, param2=default_value2, param3=default_value3)

ef student(firstname, lastname ='Mark', standard ='Fifth'):

print(firstname, lastname, 'studies in', standard, 'Standard')

Example #1: Calling functions without keyword arguments

Python
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard, 'Standard')

# 1 positional argument
student('John')

# 3 positional arguments
student('John', 'Gates', 'Seventh')

# 2 positional arguments
student('John', 'Gates')
student('John', 'Seventh')

Output:
John Mark studies in Fifth Standard
John Gates studies in Seventh Standard
John Gates studies in Fifth Standard
John Seventh studies in Fifth Standard

Passing function as an argument in Python


# Python program to illustrate functions
# can be treated as objects
def shout(text):
return text.upper()

print(shout('Hello'))

yell = shout

print(yell('Hello'))

Output:
HELLO
HELLO

Assign Function to a Variable in Python


Syntax:
def func():
{
..
}

var=func

var()
var()

Example:
Python
def a():
print("GFG")

# assigning function to a variable


var=a
# calling the variable
var()
Output:
GFG

You might also like