Arguments in Python



Arguments in Python

Function with Position arguments(Non-Default arguments)
function with parameterized variable, where value is not assigned to any parameterized variable

# different ways of calling function with non-default values
display("Techno Xpress", 28)
display(
28, "Techno Xpress")
display(
age=28, name="Techno Xpresss")

#output
name: Techno Xpress age: 28
name: 28 age: Techno Xpress
name: Techno Xpresss age:
28

  • whatever values we will pass, it will take as position as in call 1 and call 2 in above example
  • we can also pass value by directly assigning it to parameterized variable while calling the function. 

Function with arguments as default values(Default arguments)
function with parameterized variable, where value is assigned to any parameterized variable while defining

#program to understand Default arguments
def display(name = None , age = 28 , marks = 80.72) :
print("\nname: ", name, "\tage: ", age, "\tMarks: ", marks)

display()
display(
"Techno Xpress", 28, 80.72)
display(
25, "Techno Xpress")
display(
marks = 90, age= 26, name= "Er M S Dandyan")


#output

name: None age: 28 Marks: 80.72

name: Techno Xpress age: 28 Marks: 80.72

name: 25 age: Techno Xpress Marks: 80.72

name: Er M S Dandyan age: 26 Marks: 90


  • we can pass values in many ways
  • but remember until we are not assigning value to any parameterized variable while calling the function, values will be assigned based on positions
  • if we are not passing values, it will take default value

Function with Default and Non-Default argument
passing Default and Non-default arguments

#program to understand Default + Non-Default arguments
def display(age = 28 , marks = 80.72, name) :
print("\nname: ", name, "\tage: ", age, "\tMarks: ", marks)


display(
"Techno Xpress")


#output
File "C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 50
def display(age = 28 , marks = 80.72, name) :
^
SyntaxError: non-default argument follows default argument


Python has a rule to use Default and Non-Default parameter at same time.
  • Non-default argument follows default argument

Correct Way


#program to understand Default + Non-Default arguments
def display(name, age = 28 , marks = 80.72) :
print("\nname: ", name, "\tage: ", age, "\tMarks: ", marks)


display(
"Techno Xpress")
display(
"Techno Xpress", 28 , 80.72)
display(
28, "Techno Xpress")


#output
name: Techno Xpress age: 28 Marks: 80.72

name: Techno Xpress age: 28 Marks: 80.72

name: 28 age: Techno Xpress Marks: 80.72


Limitation:

#program to understand Default + Non-Default arguments
def display(name, age = 28 , marks = 80.72) :
print("\nname: ", name, "\tage: ", age, "\tMarks: ", marks)

display(
"Techno Xpress", name ="Er M S Dandyan")

#output
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 5, in <module>
display("Techno Xpress", name ="Er M S Dandyan")
TypeError: display() got multiple values for argument 'name'



by default the first position is for Non-Default argument, and we can not pass multiple values to same variable





Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India