parameters

n the above example, name and age are positional parameters. When calling the greet function, the value "Alice" is passed for name and 25 is passed for age

 parameters and it's types  

In Python, parameters refer to the variables that are defined in a function or method declaration. They represent the input values that are passed to a function when it is called. Parameters allow you to define the behavior and functionality of the function based on the values that are provided during its invocation.

In Python, there are several types of parameters that can be used:

  • Positional Parameters:
  • Default Parameters
  • Keyword Parameters
  • Variable-Length Parameters:
Positional Parameters: These are the most common type of parameters and are defined by their position in the function declaration. When calling the function, the values are passed in the same order as the parameter declaration. For example:

       
       def greet(name, age):
    print("Hello", name, "you are", age, "years old")

greet("Alice", 25)

OUTPUT:
         Hello Alice you are 25 years old
2. Default Parameters: These parameters have a predefined default value that is used if no argument is provided for that parameter during the function call. Default parameters are useful when you want to provide a default behavior but still allow it to be overridden if desired. For example:
  
  def greet(name, age=30):
    print("Hello", name, "you are", age, "years old")

greet("Alice")  # age defaults to 30
greet("Bob", 35)  # age overridden to 35
  
 OUTPUT:
          Hello Alice you are 30 years old
Hello Bob you are 35 years old

In the above example, age is a default parameter with a default value of 30. If no age is provided during the function call, it will default to 30. However, you can also pass a different value, as shown in the second function call.
Keyword Parameters: Unlike positional parameters, keyword parameters are identified by their 
names rather than their positions. They are specified explicitly using the parameter name 
followed by the corresponding value during the function call. This allows you to provide 
arguments in any order. For example:
  

def greet(name, age):
    print("Hello", name, "you are", age, "years old")

greet(age=25, name="Alice")

OUTPUT:
              Hello Alice you are 25 years old
In the above example, the function greet is called with keyword parameters age=25 and name="Alice". The order of the arguments doesn't matter because they are explicitly associated with the parameter names.
Variable-Length Parameters: Python provides two special syntaxes to handle variable-length
 parameters:
 *args and **kwargs.
args: It allows a function to accept a variable number of non-keyword arguments. The parameter name args is just a convention; you can use any valid variable name preceded by an asterisk () to represent the variable-length parameter. The arguments passed are collected into a tuple. For example:
      
      def add_numbers(*args):
    result = 0
    for num in args:
        result += num
    return result

print(add_numbers(1, 2, 3))  # Output: 6
print(add_numbers(4, 5, 6, 7))  # Output: 22
kwargs: It allows a function to accept a variable number of keyword arguments. The parameter name kwargs is just a convention; you can use any valid variable name preceded by double asterisks () to represent the variable-length parameter. The arguments passed are collected into a dictionary. For example:


  def print_info(**kwargs):
    for key, value in kwargs.items():
        print(key, ":", value)

print_info(name="Alice", age=25, country="USA")

OUTPUT:
           name : Alice
           age : 25
           country : USA
           
    • In the above example, the print_info function accepts multiple keyword arguments, which are collected into a dictionary kwargs. The function then prints each key-value pair in the dictionary.

These are the commonly used types of parameters in Python functions. You can combine these parameter types based on your requirements to define functions with flexible and customizable behavior.

Comments

Popular posts from this blog

ILLUSTRATIVE PROGRAMS IN PYTHON

Fibonacci Series

The Fibonacci series