Get Your Feet Wet with Python
A gentle introduction to writing software
Hi. You. Yes, you.
If you’re here because you want to start learning how to use code to do awesome things, you’re in the right place. If you’re here because you want to learn Python specifically, you may want to go elsewhere for a while and check back periodically.
In my last article I talked about moving from understanding what code is to how it can actually be written and leveraged in the real world. Let’s delve into real world applications together now, using Python as a framework.
The first program a developer might write in a new language is “Hello, World!” This program is minimal but good at proving that your computer is set up correctly to run the code you’re writing. Google “install Python on [operating system]” before reading on to get up and running. We’ll start by using Python from the command line/terminal, where it can usually be started by typing “python” at the prompt.
“Hello, World!” does nothing more than print the words “Hello, World!” to the screen. In Python, this is exceedingly simple.
>>> print(‘Hello, World!’)
Well, that was easy. Let’s talk about the (few) pieces of this. print is a function, and is built into Python. Functions are things that take inputs and produce outputs, typically in a repeatable way. In this case, print takes in one argument — the message to print — and results in the message being output to the screen.
The message is passed into print inside parentheses, which is similar for every function in Python. In this case the message is a string of characters, commonly called a “string” in programming. Strings in Python are denoted with quotes. Everything inside the quotes is the message that will be printed.
Is this all making sense? Functions are pivotal in the world of software. They allow us to write reusable code that behaves in a predictable way, abstracting much functionality in the effort to make our code more readable. Let’s look at a use case and write a slightly more complicated function to make our lives easier.
Suppose we want to know the square of a bunch of numbers plus 2. The simplest way to do this in Python would be to print each calculation individually:
print((1 * 1) + 2)
print((2 * 2) + 2)
print((3 * 3) + 2)
This is all well and good, but if we decide later that we want the squares plus 3 instead, we’ll have to change that value on every line. That’s a huge headache! Functions to the rescue. We can write a function square_plus_n that takes in a number to square and optionally takes in a number n to add to the square result:
def square_plus_n(number_to_square, n=2):
return (number_to_square * number_to_square) + n
Now if we want to change our default added number, we can just change n=2 to n=(some number) in the function definition. There are a couple of new pieces to talk about in our function. Let’s review.
“def” is a keyword in Python, short for “define,” that means “here is a function definition.” Then comes the name of the function, square_plus_n.
Next are our arguments. square_plus_n takes an argument called number_to_square and a second argument n. Since n has a default value specified, n is optional . If unspecified when the function is called, n will default to whatever default you choose.
The colon means that the following indented lines are the body of the function. Our function body here is a single line, which uses the “return” keyword to return the result of our function to wherever it was called. The result that’s being returned is our calculation of the square plus a number.
Now that we have our function defined, we can call it multiple times. Notice that we’re only supplying one argument each time, meaning that the second argument, n, is defaulting to 2. We could specify a different default:
This would result in a calculation of (3 * 3) + 5 = 14.
Here we’ve learned what functions are good for and talked through an example of how to define a function that returns an output. Then we called our function and printed the output to the screen. You’re pretty close to writing some really useful stuff! If you have questions or suggestions, make sure to reply below. Meanwhile, start writing your own functions!