Python Functions: Keyword Arguments, Default Arguments, and Recursion, Slides of Object Oriented Programming

The concept of functions in Python with a focus on keyword arguments, default arguments, and recursion. Keyword arguments are specified by parameter name, while positional arguments are assigned based on their position in the argument list. Default arguments provide optional parameters with default values. Recursion is a method of problem solving where a problem is broken down into similar sub-problems until they can be directly solved. examples and exercises.

Typology: Slides

2019/2020

Uploaded on 08/07/2020

muskan278
muskan278 🇮🇳

4

(1)

5 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Functions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Python Functions: Keyword Arguments, Default Arguments, and Recursion and more Slides Object Oriented Programming in PDF only on Docsity!

Functions

Keyword Arguments in Python

  • (^) The functions we have looked at so far were called with a fixed number of positional arguments. A positional argument is an argument that is assigned to a particular parameter based on its position in the argument list, as illustrated below.

Keyword Arguments in Python Contd..

  • (^) A keyword argument is an argument that is specified by parameter name, rather than as a positional argument as shown below

Default Arguments in Python

  • (^) A default argument is an argument that can be optionally provided,
  • (^) In this case, the third argument in calls to function mortgage_rate is optional. If omitted, parameter term will default to the value 20 (years) as shown. If, on the other hand, a third argument is provided, the value passed replaces the default parameter value.

Defaults

>>> def f(a, b=2, c=3): print(a, b, c)

a required, b and c optional

>>> f(1) # Use defaults 1 2 3 >>> f(a=1) 1 2 3 >>> f(1, 4) # Override defaults 1 4 3 >>> f(1, 4, 5) 1 4 5

Defaults

>>> f(1, c=6) # Choose defaults 1 2 6 Combining keywords and defaults def func(spam, eggs, toast=0, ham=0):

First 2 required

print((spam, eggs, toast, ham))

Arbitrary Arguments Examples

  • (^) Use of ‘*’
  • (^) collects unmatched positional arguments into a tuple: >>> def f(*args): print(args)

Arbitrary Arguments Examples

  • (^) >>> f() ()
  • (^) >>> f(1) (1,) >>> f(1, 2, 3, 4) (1, 2, 3, 4)

Arbitrary Arguments Examples

>>> def f(a, *pargs, **kargs): print(a, pargs, kargs) >>> f(1, 2, 3, x=1, y=2) 1 (2, 3) {'y': 2, 'x': 1}

Calls: Unpacking arguments

>>> def func(a, b, c, d): print(a, b, c, d) >>> args = (1, 2) >>>args += (3, 4) >>> func(*args)

Same as func(1, 2, 3, 4)

1 2 3 4

Different patterns in Algorithm

Recursive algorithms

  • (^) In recursive problem solving , a problem is repeatedly broken down into similar sub problems, until the sub problems can be directly solved without further breakdown.

Recursive algorithms

  • (^) Recursive algorithms ■ The functions computed by the algorithms The functions computed by the algorithms are expressed in terms of itself ■ The functions computed by the algorithms Example Task: Find the Factorial of a positive integer