Your First Program

Let’s wrap this up by combining what we learned into a nice little program:

>>> 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)

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.

When typing in this 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 following code:

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")

At this point, using the interactive Python shell starts to work against us. Luckily, we can also store our Python programs in files, as you’ll soon learn.

Congratulations. If you followed along, you now have a basic understanding of programming with Python. I recommend you to keep experimenting inside the REPL. 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!