Crash Course in Python – Part II

It is Friday. Many stressful things are happening in different fronts in the past few months. Most of the things are out of my control. It seems like I need to find a way to relax. Hopefully things will turn out well. I fully understand that stress is an internal thing and I am quite good at controlling it. I used to say that stress is the salt of life; but too much salt is not good for you.I continued experimenting with the examples in chapter 2 from the Data Science from Scratch book by Joel Grus. Please keep in mind that I modify the examples as I tried them out.

In the following example we will define and use a function.

# **** function definition ****
def doubleMe(x):
	# *** ... or alternate function definition ***
	print("doubleMe <<< x:", x)
	return 2 * x

The output of the previous script follows:

$ python function.py
main <<< x: 5
doubleMe <<< x: 5
main <<< y: 10
$ 

Please note that the code was entered into the function.py script. The script was then invoked on a console.

The following code illustrates a function used as an argument to another function and a function being assigned to a variable and then the variable used to execute the function.

# **** a function ****
def func(x):
	print("func <<< x:", x)
	return x * x;

# **** another function ****
def funcFunc(f):
	return f(2)

# **** call a function with a function as an argument ****
y = funcFunc(func)
print("main <<< y:", y)

# **** assign a function to a varible ****
aFunc = func
z = aFunc(3)
print("main <<< z:", z)

The output of the previous script follows:

$ python functions.py
func <<< x: 2
main <<< y: 4
func <<< x: 3
main <<< z: 9
$ 

Let’s check out the following code:

# **** expects a function as an argument ****
def funcFunc(f):
	return f(2)

# **** using a lambda ****
q = 1
z = lambda q: q + 1
x = z(1)
print("main <<< x:", x)

# **** lambda as an argument to a function ****
y = funcFunc(lambda x: x + 7)
print("main <<< y:", y)

We start by defining a function that calls a function with 2 as an argument. In the main we define a lambda (which is a function), assign it to a variable and then call it with a value of 1.

The last set of statements define a lambda and call a function that expects a function. The returned value is assigned to a variable and the value is displayed.

The output for a run is as follows:

$ python lambdas.py
main <<< x: 2
main <<< y: 9
$ 

Function parameters may have default values in case a value is not specified as illustrated by the following code:

# **** function with default parameters ****
def func(s1 = "Hello", s2 = "unknown person"):
	return s1 + " " + s2

# **** ****
s1 = "hello"
s2 = "world"
print("main <<< " + func(s1, s2))

# **** ****
print("main <<< " + func())

# **** ****
print("main <<< " + func(s1))

# **** ****
print("main <<< " + func(s2))

The idea is that if the function is called with a missing parameter the default would be used. A run of the associated script is shown:

$ python default_param.py
main <<< hello world
main <<< Hello unknown person
main <<< hello unknown person
main <<< world unknown person
$ 

The following code has a few more lines illustrating the use of additional cases of missing parameters.

# **** function with default parameters ****
def func(s1 = "Hello", s2 = "unknown person"):
	return s1 + " " + s2

# **** another function with default parameters ****
def func2(a = 0, b = 0):
	return a + b;

# **** ****
s1 = "hello"
s2 = "world"
print("main <<< " + func(s1, s2))

# **** ****
print("main <<< " + func())

# **** ****
print("main <<< " + func(s1))

# **** ****
print("main <<< " + func(s2))

# **** ****
print()
print("main <<< ", func2(1, 2))

# **** ****
print("main <<< ", func2(b=2))

# **** ****
print("main <<< ", func2(a=1))

# **** ****
print("main <<< ", func2(a=1, b=2))

# **** ****
print("main <<< ", func2(b=2, a=1))

The run of the modified code follows:

$ python default_param.py
main <<< hello world
main <<< Hello unknown person
main <<< hello unknown person
main <<< world unknown person

main <<<  3
main <<<  2
main <<<  1
main <<<  3
main <<<  3
$ 

Perhaps this post was not that exciting. I decided to put it together for completeness. The following post will include more complex objects.

As usual, if you have comments or questions please do not hesitate and leave me a note. Will respond as soon as possible.

Enjoy;

John

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.