Pages Navigation Menu

"It's not about the destination, it's about the journey. "

Learn Python The Hard Way, exercise 18

I found this exercise to be quite comforting; however, at first I was a little confused when presented with all that new code that I had never seen before. In this exercise it teaches you about the “def” (Define) and creating functions (Mini-scripts).

To overcome my confusion, I typed the whole thing out and ran it in Windows Powershell. Then for extra practice I created another file and typed each function one by one and changed those functions to get an idea on what each and every thing does.

Here’s my code:

# FUNCTION 1
def print_two(*args):
	arg1, arg2, arg3 = args
	print "arg1: %r, arg2: %r, arg3: %r" % (arg1, arg2, arg3)

print_two("Hello","Mr","Python")

# FUNCTION 2
def print_two_again(arg1, arg2): 
	print "arg1: %r, arg2: %r" % (arg1, arg2)

print_two_again("Hello2","World!")

# FUNCTION 3
def print_none():
	print "Nothing here."
print_none()

I only modified it slightly, for testing purposes, and then tested each and everyone of them. It’s quite fascinating how it works and it has got me more interested in learning as it’s a really simple way of explaining these various different methods of displaying data in Python.

Instead of using argv or variables to display strings, you can simply create functions which, I guess, organises the code; I suspect.

Leave a Comment

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