Notes on Python Arguments

Jul 24, 2024

Python Functions - Arguments

Introduction

  • Lecture presented by 720 on Python functions and arguments.

Types of Arguments

  1. Formal Arguments: Variables defined in a function.
  2. Actual Arguments: Values passed to the function during a call.

Example: Adding Two Numbers

  • Function definition:
    def sum(a, b):
        c = a + b
        print(c)
    
  • To call the function: sum(5, 6) - Must pass two values, otherwise an error occurs.

Four Types of Actual Arguments

  1. Positional Arguments

    • Arguments assigned based on the position in the function call.
    • Example:
      def person(name, age):
          print(name, age)
      person('Naveen', 28)
      
    • Order matters; if switched, results can be incorrect.
  2. Keyword Arguments

    • Specify the argument names in the function call, eliminating order issues.
    • Example:
      person(age=28, name='Naveen')
      
  3. Default Arguments

    • Assign default values if no argument is passed.
    • Example:
      def person(name, age=18):
          print(name, age)
      person('Naveen')  # Outputs 'Naveen 18'
      
  4. Variable Length Arguments

    • Allows passing a varying number of arguments.
    • Use *args to accept multiple values.
    • Example:
      def add(*args):
          total = 0
          for num in args:
              total += num
          print(total)
      add(5, 6, 7, 8)  # Outputs '26'
      

Summary

  • Discussed various types of function arguments: positional, keyword, default, and variable length.
  • Understanding how to properly utilize these arguments is crucial for effective function usage in Python.

Conclusion

  • Engage with the content by asking questions in the comments.
  • Reminder to practice these types of arguments for better mastery of Python functions.