Functions and the various types of arguments they can take
This code snippet is about functions, its parameters and the various types of arguments a function can take.
def funct():
print("I am a function that takes no value as input.")
def country(countryname="Pakistan"): #parameter with default value
print("The name of my country is", countryname)
def greet(fname, lname): # fuction takes NMT and NLT 2 parameters
print("Good evening", fname, lname)
def person(*args): # accepts variable number of positional arguments
print(args) # returns result with quotes, commas and brackets
print(*args) # returns result without quotes, commas and brackets
print(len(args)) # returns number of arguments given
print(type(args)) # returns type of data which is tuple
def personData(**kwargs): # accepts variable number of keyword arguments
print(kwargs) # returns result with quotes, commas and brackets
print(*kwargs) # returns only keys (without quotes, commas and brackets)
print(len(kwargs)) # returns number of keyword arguments given
print(type(kwargs)) # returns type of data which is dict here
# calling a function
funct()
print("\n")
# Function with default values, if value supplied default value is overrided
country() # returns default value
country("India") # returns india as country name
print("\n")
# positional arguments vs. keyword arguments
greet("abdur", "rehman") # positional arguments: order matters
greet(lname="rehman", fname="abdur") # keyword arguments: order doesn't matter
print("\n")
# functions that can take variable number of arguments
person("abdur", 35, "pharmacist", "pythonista", "runner", "vegetarian")
print("\n")
personData(name="abdur", age=35, profession="pharmacist", passion="pythonista")
print("\n")
Comments
Post a Comment
Write something to CodeWithAbdur!