Python Tutorial - Positional Parameters

Python Exercise Lists

What are positional parameters in Python?

Positional parameters, sometimes called mandatory parameters, mean that the actual parameters must be passed to the function in the correct order. In other words, the number and position of the actual parameters passed in when calling the function must be the same as when defining the function

The actual and formal parameters in Python must be the same.

When calling a function, the number of actual parameters specified must be the same as the number of formal parameters (passing more or less is not sufficient), otherwise the Python interpreter will throw a TypeError exception and prompt for the missing required positional parameters.

Example:

def girl(width, height):
    return 2 * (width + height)
#When calling the function, 2 parameters must be passed, otherwise an error will be thrown
print(girl(3))
           

The output is:

---------------------------------------------------------------------------
TypeError              Traceback (most recent call last)
[ipython-input-60-e234f2482b4f] in [module]
2 return 2 * (width + height)
3 #When calling the function, 2 parameters must be passed, otherwise an error will be thrown
----> 4 print (girl(3))
TypeError: girl() missing 1 required positional argument: 'height'

It can be seen that the type of exception thrown is TypeError, which specifically refers to the girl() function to burn a necessary height parameter.

Similarly, multi-pass parameters also throw exceptions:

def girl (width, height):
    return 2 * (width + height)
#When calling the function, 2 parameters must be passed, otherwise an error will be thrown
print (girl(3,2,4))
           

The output is:

---------------------------------------------------------------------------
TypeError      Traceback (most recent call last)
[ipython-input-61-b846438cdbe5] in [module]
2 return 2 * (width + height)
3 #When calling the function, 2 parameters must be passed, otherwise an error will be thrown
----> 4 print (girl(3,2,4))
TypeError: girl() takes 2 positional arguments but 3 were given

From the TypeErroe exception information, you can know that the girth () function only required 2 parameters, but passed in 3 parameters.

Actual and formal parameter positions must match in Python

When calling a function, the position of the actual parameter passed in must correspond to the position of the formal parameter, otherwise the following 2 results will be produced:

1. Throws a TypeError Exception

When the actual parameter type and the formal parameter type are inconsistent, and the two types cannot be properly converted between functions, a TypeError exception is thrown.

Example:

def area (height, width):
    return height * width / 2
print (area ("Python Language Website", 3))
           

The output is:

---------------------------------------------------------------------------
TypeError     Traceback (most recent call last)
[ipython-input-65-39911434f48f] in [module]
1 def area(height, width):
2 return height * width / 2
----> 3 print (area ("Python Language Website", 3))
[ipython-input-65-39911434f48f] in area(height, width)
1 def area(height, width):
----> 2 return height * width / 2
3 print (area ("Python Language Website", 3))
TypeError: unsupported operand type(s) for /: 'str' and 'int'

The exception message shown above is because the string type and integer value are divided.

2. The Result is not as Expected

When calling a function, if the positions of the specified actual and formal parameters are inconsistent, but their data types are the same, the program will not throw an exception, but it will cause the running result to be different from expected.

For example, design a function to find the area of a trapezoid, and use this function to find the area of a trapezoid with an upper base of 4cm, a lower base of 3cm, and a height of 5cm. But if the incoming positions of the high and low parameters are interacted, the calculation result will cause an error:

def area (upper_base, lower_bottom, height):
    return (upper_base + lower_bottom) * height / 2
print ("The correct result is:", area (4,3,5))
print ("Error result is:", area (4,5,3))
           

The output is:

The correct result is: 17.5
Error result is: 13.5

Therefore, when calling the function, be sure to determine the good position, otherwise it is likely to produce such errors in the example, which is not easy to find.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises