Writing Your First Lines of Python Code

An interactive introduction to writing your first lines of Python code.

Introduction

An acquaintance on Twitter recently reached out to tell me they were getting into programming and asked "Any advice for beginners?" My first response was to start with one programming language, such as JavaScript or Python for their simplicity and popularity. Like any new activity, the initial steps can be the scariest and most difficult to overcome. Writing your first lines of code is no different.

The traditional rite of passage into programming is to write a "Hello, World!" computer program. The code is simple and illustrative of the most basic syntax of a programming language. According to Wikipedia, the phrase "Hello, World!" was influenced by an example program in the seminal 1978 book The C Programming Language, and was inherited from a 1974 Bell Labs internal memo by Brian Kernighan in Programming in C: A Tutorial.

main( ) {
        printf("hello, world\n");
}

The original tutorial source goes on to explain a C program consists of one or more functions and perhaps some external data definitions. main is a function, and in fact all C programs must have a main function.

Execution of the program begins at the first statement within main, which may invoke other functions to perform its job. Some functions are derived from the same program and others from separate libraries of code. printf is a library function which will format and print output on the terminal (unless some other destination is specified). In this case it prints "hello, world".

In other words, the program runs the main function. Inside main, the printf command is called and outputs the "hello, world" text followed by a return line (\n). The "hello, world" program can vary in complexity between different programming languages but is still a useful starting point example for any language.

Live Example

Next, let's expand on the elementary "hello, world" program with an introduction to Python.

The editor below is interactive. After the Python environment finishes loading, run the Python program and try adding more greetings, like the Danish "hej", to experiment.

Loading...

In this code snippet, the function output_greetings defines the execution for the text output akin to the main function used in the C program. The output_greetings function takes a parameter called greetings , which is defined as a variable near the bottom of the program. The greetings variable type is a Python list containing text stings for "Hello" in English, Spanish, French, German, and Chinese. On the last line, the output_greetings function is invoked and the greetings list is passed into the function to be used.

On a side note, the program also introduces the concept of a comment noted by the # symbol in Python. The comment text is ignored during the program execution. Comments add clarification to code akin to a footnote in a book, but otherwise are ignored at program runtime.

Within the output_greetings function there is a for loop used to iterate over the greetings list variable. Each iteration in the loop will successively index into the list one element at a time. In effect, each greeting string is printed individually within the loop.

For a real-world metaphor imagine the five greetings are written on individual pieces of paper and placed in a box. Reach your hand into the box, grab one paper, say the greeting out loud, and repeat (iterate) the process until you've said all five greetings.

Now envision a list of greetings with hundreds of variations for saying "Hello" in different languages. Rather than output each greeting one line of code at a time with separate print statements, a for loop can do the output with only two lines of code. The for loop statement indexes into the greetings variable to acquire one greeting string variable at a time then passes it into the print command for output.

Loops are incredibly powerful for automating repetitive processes and at the core of basically any programming language. Sometimes programming is as simple as iterating over a large list of items and doing something with one item at a time from the list.

Calling or invoking the output_greetings function will execute the loop and print logic once the program runs. Otherwise, if the output_greetings function is not called nothing will be printed out, because the logic within the function is self-contained and must be called from the outside.

That's it. Pretty simple. Well, maybe not at first, but programming becomes intuitive with practice, like learning Spanish, French, or even carpentry.

Writing code is analogous to composing elaborate structures and functionalities using virtual Lego blocks that come to life. Ultimately, the code blocks express a set of instructions when combined together tell a computer what to do. In this case...

Hello
Hola
Bonjour
Guten Tag
Nǐn Hǎo

Published