Your First Program

If you followed the Python tutorial from the start, you’ve learned a lot by now. We’ve covered key topics, like booleans and conditional programming, strings, and functions. What we haven’t done yet, is create an actual program. So let’s wrap this up by combining what we learned into a nice little program.

Entering the code in the REPL

Let me share the program first. Please analyze it thoroughly before you continue reading:

def say_hi(name):
    if name == '':
        print("You didn't enter your name!")
    else:
        print("Hi there...")
    
    for letter in name:
        print(letter)

say_hi("erik")

When typing in more than a few lines of code, you have to be very careful with indentation. If you keep getting errors because of formatting or indentation, you can also try to completely copy and paste the code. In the REPL, it should end up looking like the following:

>>> def say_hi(name):
...     if name == '':
...         print("You didn't enter your name!")
...     else:
...         print("Hi there...")
...         for letter in name:
...             print(letter)
... 
>>> name = input()
 < enter your name at this point >
>>> say_hi(name)

Analyzing the program

Let’s go over the code step by step.

Asking for input with Python

I managed to cram in one more new thing, the built-in function input(). It does exactly what you expect it to do: ask for input and assign that input to a variable.

A function with one argument

The say_hi(name) function takes one argument, the name, and prints the name to the screen. The function does not return anything. It doesn’t need to, since it does all the printing work itself.

An if .. else block

Our function only greets us if the name is not an empty string. Why’s that?

When someone just hits enter when asked for input, the input() function returns an empty string. You can check for yourself in the REPL:

>>> input()
''

Just hit enter when asked for input, and you’ll see that the call to input() results in an empty string. So to not make a fool of ourselves, we won’t greet someone showing such rude behavior.

A for loop

Finally, with the for loop, we print each letter of the entered name on a new line, just because we can.

An assignment

Since we defined a function, say_hi(name), we can reuse this function. You can repeatedly ask for a name and repeatedly call say_hi. Here’s a little assignment:

Create an infinite loop that keeps asking for names,
and that keeps greeting us using the entered name.

Hint: use the say_hi function from above.
>>> while True:
...     name = input()
...     say_hi(name)
...
Erik
Hi there...
E
r
i
k
John
Hi there...
J
o
h
n

And now what?

At this point, using the interactive Python shell starts to work against us. Chances are you’ve been fiddling a lot to get things working, mainly because of indentation issues. Luckily, we can also store our Python programs in files, as you’ll learn in the next section: creating Python programs. But before we do so, we’ll first dive into Python comments.

For now: congratulations. If you followed along, you should have a basic understanding of programming with Python. I recommend you to keep experimenting inside the REPL. You may need to re-read some or all of the sections. That’s OK and perfectly normal.

The most important piece of advice I’d like to give at this point is that you don’t learn programming by reading alone, just like you don’t become a doctor just by reading. You’ll have to get your hands dirty and practice.

If you feel ready, continue with the next chapter!

About the author

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python!