Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Keyword Arguments

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Keyword Arguments

      Python allows to pass function arguments in the form of keywords which are also called named arguments. Variables in the function definition are used as keywords. When the function is called, you can explicitly mention the name and its value.

      Example

      # Function definition is here
      def printinfo( name, age ):
      "This prints a passed info into this function"
      print ("Name: ", name)
      print ("Age ", age)
      return
      
      # Now you can call printinfo function
      # by positional arguments
      printinfo ("Naveen", 29)
      
      # by keyword arguments
      printinfo(name="miki", age = 30)
      By default, the function assigns the values to arguments in the order of appearance. In the second function call, we have assigned the value to a specific argument
      It will produce the following output
      Name: Naveen
      Age 29
      Name: miki
      Age 30
      Let us try to understand more about keyword argument with the help of following function definition
      def division(num, den):
      quotient = num/den
      print ("num:{} den:{} quotient:{}".format(num, den, quotient))
      
      division(10,5)
      division(5,10)
      Since the values are assigned as per the position, the output is as follows
      num:10 den:5 quotient:2.0
      num:5 den:10 quotient:0.5
      Instead ofpassing the values with positional arguments, let us call the function with keyword arguments
      division(num=10, den=5)
      division(den=5, num=10)
      It will produce the following output
      num:10 den:5 quotient:2.0
      num:10 den:5 quotient:2.0
      When using keyword arguments, it is not necessary to follow the order of formal arguments in function definition.
      Using keyword arguments is optional. You can use mixed calling. You can pass values to some arguments without keywords, and for others with keyword.
      division(10, den=5)
      However, the positional arguments must be before the keyword arguments while using mixed calling.
      Try to call the division() function with the following statement.
      def division(num, den):
      quotient = num/den
      print ("num:{} den:{} quotient:{}".format(num, den, quotient))
      
      division(num=5, 10)
      As the Positional argument cannot appear after keyword arguments, Python raises the following error message
      division(num=5, 10)
      ^
      SyntaxError: non-keyword arg after keyword arg

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.