Using Keyword and Positional Arguments

Practice Labs Module
Time
6 minutes
Difficulty
Intermediate

Welcome to the Using Keyword and Positional Arguments Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills

Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Overview

Introduction

Welcome to the Using Keyword and Positional Arguments Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills

Learning Outcomes

In this module, you will complete the following exercise:

  • Exercise 1 - Defining Functions with Keyword and Positional Arguments

After completing this lab, you will be able to:

  • Define functions with keyword and positional arguments in Python programs

Exam Objectives

The following exam objectives are covered in this lab:

  • 4.1 Construct and analyze code segments that include function definitions

Lab Duration

It will take approximately 20 minutes to complete this lab.

Exercise 1 - Defining Functions with Keyword and Positional Arguments

Have you ever noticed how the values that you pass to functions get assigned to the correct arguments? For example, when calling the built-in format() function in the following statement, how does the compiler know which value to place at the location specified by the curly braces?

print(“The sum of the two numbers, {} + {} is: {}”.format (5, 10, 15))

Well, it is the position of these arguments which decides the values that will replace the curly brackets in the string to be printed.

The same applies to user functions.

For example, assume you define a user function to write a personalized message to the user as follows:

def welcome(name, msg):

print (“Hello {}, {}”.format(name, msg))

When calling this function, you need to make sure to supply the name followed by the message.

welcome(“Melisa”, “Good morning!”)

If the reverse is entered, the program will simply print the message in place of a name and vice versa.

welcome(“Good morning!”, “Melisa”)

Here is the output in the two cases described above:

Hello Melisa, Good morning!

Hello Good morning!, Melisa

This suggests that the order of arguments is extremely important in these cases. Therefore, these arguments are called positional arguments.

Another way to call functions without really worrying about the position of arguments is by using** keyword arguments** or named arguments.

Using the same example, you can pass the arguments as keyword arguments such as:

welcome(name=“Melisa”, msg=“Good morning!”)

In this case, since the values are directly assigned, the order of the argument is of no importance.

This means that you could also call the function as follows without changing the output.

welcome(msg=“Good morning!”, name=“Melisa”)

The output will be:

Hello Melisa, Good morning!

In this exercise, you will perform the following tasks:

  • Task 1 - Write a program to define a simple function that accepts a user's name, city, and state as keyword arguments and stores the same in a dynamically generated CSV file. The program will print the data stored in the CSV file.
  • Task 2 - Write a program to define a simple function that accepts user's name, city, and state as positional arguments and stores the same in a dynamically generated CSV file. The program will print the data stored in the CSV file.

Learning Outcomes

After completing this exercise, you will be able to:

  • Define functions with keyword and positional arguments in Python programs
Learning Partner
Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.