Unit 3 - Lesson 3

26.1.1. What are default arguments?

def simplecalc(a,b=100 ):
	
#fill in the missing code..	
	print("addition:", a+b)
	print("subtraction:", a-b)
	print("multiplication:", a*b)

num1 = int(input("num1: "))
num2 = int(input("num2: "))
simplecalc(a = num1)

simplecalc(num1,num2)

26.1.2. Writing a default value arguments example

def calculateTax(salary,percent=20):
	taxAmount = salary*percent/100
	print(taxAmount)
#fill in the missing code..

sal = int(input("salary: "))
per = float(input("tax percentage: "))
calculateTax(sal)
calculateTax(sal,per)

26.1.3. Default arguments , some important points

(a) Function arguments can have default values in Python.
(c) Any number of arguments in a function can have a default values.
(d) non-default arguments cannot follow default arguments.

26.1.4. Write a program with positional parameters and keyword parameters and default parameters.

def fun1(name = 'Padma',age = 12):
	'''Body of the function'''
	print (name,"is",age, "years old.")


name = input()
age = int(input())

# call the function by passing arguments (name,age)
fun1(name,age)
# call the function by passing age as 16 and user given name.
fun1(name,16)
# call the function by only passing age and Name is default.
fun1(age=age)
# call the function by only passing name and age is default.
fun1(name)
# call the function without passing any arguments
fun1()

26.2.1. Arbitrary Number of Arguments

(a) If the correct number of arguments that will be passed to a function at the time of execution is not known , we can use function with arbitrary arguments.
(b) Arbitrary arguments. is specified by using an asterisk (*) in the function definition before the parameter name.

26.2.2. Writing a function that takes arbitrary length arguments

def mySum(*args):
	return(sum(args))
#fill in the missing code...

a = int(input())
b = int(input())
c = int(input())
d = int(input())

print(mySum(a,b,c,d))
print(mySum(a,b,c))
print(mySum(a,b))

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.