Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python

Python | Types of parameters



Python types of parameters: Here, we are going to learn about the types of the parameters python.
Submitted by Pankaj Singh, on October 11, 2018

There are following types of parameters in python:

  1. Required parameters
  2. Default parameters
  3. Keyword/named parameters
  4. Variable length parameters

1) Required parameters

If we define a function in python with parameters, so while calling that function – it is must send those parameters because they are Required parameters.

# Required parameter
def show(id,name):
    print("Your id is :",id,"and your name is :",name)

show(12,"deepak")
# show() #error
# show(12) #error

Output

Your id is : 12 and your name is : deepak

2) Default parameters

If we define the parameters as default parameters, there will not any error occurred even you do not pass the parameter or pass the less parameters.

# Default parameters
def show(id="<no id>",name="<no name>"):
    print("Your id is :",id,"and your name is :",name)

show(12,"deepak")
show()
show(12)

Output

Your id is : 12 and your name is : deepak
Your id is : <no id> and your name is : <no name>
Your id is : 12 and your name is : <no name>

3) Keyword/named parameters

Python has dynamic types – so if we send parameters in the wrong sequence, it will accept the values due to dynamic typing. But, this data is not correct so to prevent this, we use keyword/named parameter.

# keyword/named parameters
def show(id="<no id>",name="<no name>"):
    print("Your id is :",id,"and your name is :",name)

# defualt/correct sequance 	
show(12,"deepak")
# sequence with the keywords
# there is no need to rememeber the parameters sequences
# provide the value with the name/argument name
show(name="priya",id=34)

Output

Your id is : 12 and your name is : deepak
Your id is : 34 and your name is : priya

4) Variable length parameters

By using *args, we can pass any number of arguments to the function in python.

# Variable length parameters 
def sum(*data):
    s=0
    for item in data:
       s+=item
    print("Sum :",s)

sum()
sum(12)
sum(12,4)
sum(12,4,6)
sum(1,2,3,4,5,6,7,8)
sum(12,45,67,78,90,56)

Output

Sum : 0
Sum : 12
Sum : 16
Sum : 22
Sum : 36
Sum : 348





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.