Theory of Intelligence
PYTHON PROGRAMMING•Control Flow And Functions•Functions•2.2 Parameters, *args & **kwargs

2.2.3 *args & **kwargs

Subtopics: Positional & Keyword Arguments, Default Values and *args & **kwargs.

2.2.3 *args & **kwargs

Introduction

args and kwargs are used to pass arguments to a function. args is used to pass a variable number of positional arguments to a function, and kwargs is used to pass a variable number of keyword arguments to a function.

args

args is used to pass a variable number of positional arguments to a function. It is a tuple.

kwargs

kwargs is used to pass a variable number of keyword arguments to a function. It is a dictionary.

Example 1

Sketch and analyze y=−x2−2x+3y = -x^2 - 2x + 3.

Solution

yy-intercept: y=3y = 3 when x=0x = 0.

xx-intercepts: solve −x2−2x+3=0-x^2 - 2x + 3 = 0, or x2+2x−3=0x^2 + 2x - 3 = 0:

(x+3)(x−1)=0  ⟹  x=−3, x=1(x + 3)(x - 1) = 0 \implies x = -3,\ x = 1

Vertex:

h=−b2a=−−22(−1)=−1,k=−(−1)2−2(−1)+3=4h = -\dfrac{b}{2a} = -\dfrac{-2}{2(-1)} = -1, \quad k = -(-1)^2 - 2(-1) + 3 = 4

So the vertex (−1,4)(-1, 4) is a maximum point. The function is increasing on (−∞,−1)(-\infty, -1) and decreasing on (−1,∞)(-1, \infty).

Finding Maximum and Minimum Values by Completing the Square

Completing the square rewrites f(x)=ax2+bx+cf(x) = ax^2 + bx + c in vertex form. It is a dictionary.

Example 3

Find the maximum or minimum value of f(x)=x2−2x−3f(x) = x^2 - 2x - 3.

Solution

Here a=1a = 1, b=−2b = -2, c=−3c = -3:

h=−−22(1)=1,k=−3−(−2)24(1)=−4h = -\dfrac{-2}{2(1)} = 1, \quad k = -3 - \dfrac{(-2)^2}{4(1)} = -4

Since a=1>0a = 1 > 0, the minimum value of ff at x=1x = 1 is −4-4.

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!