Chapter I

Start simple think big

OBJECTIVES

  1. Write and run the first Python program

  2. Basic console output

Hello world!

This is a classic example in almost all programming languages. The point is to write a simple program that will allow you to go through the full development cycle:

  • understand the problem you want to solve

  • conceive a program that will solve the problem - build the architecture of your solution

  • write the code

  • execute successfully the code, if errors occur then correct them and execute the code

This is the basic routine that you have to perform as a programmer. Do not forget that analysis and thinking is always before coding. Think a lot before hand and then code efficiently! Actually if you can solve a problem with less code it is better: less code = less errors. Writing code is a non-natural activity, writing text for a machine is not something that most people do so do not be surprised when you precious code will crush, malfunction or cause troubles - this is normal.

We will start with the basic part - a fundamental design pattern called ephemeral feedback or the simplified version called user feedback. The main objective here is to give feedback to the user about the execution of our program. So, the only task that our program has to perform is to give some feedback to the user by displaying a text in the console. Wait a minute, what is a console? If you do not know what a console is, it means that you are a modern human being. You ancestors used keyboard and black screen with green phosphorus in order to program computers - that is a console. For historical reasons and compatibility with old computer programmers - the humans not the machines, this console/terminal is still in use as a software emulation of the old hardware console. This is a window, usually with black background where you have a command prompt and where you type commands - simple text, press enter and the command is executed immediately. This is what is called an interpreter - it will read the commands line by line and execute them line by line, and will stop as soon as there is an error or after the command is executed successfully.

Python is an interpreted language - so the origins of Python are in the pre civilized era. You Python program can be also typed into Python interpreter line by line or in a text file containing the same commands fed as input argument to the Python interpreter. I will skip the line by line typing and interpreting since this is not the standard way to execute programs in Python. Nevertheless the behavior and execution of the program are identical so you are not loosing any magic tips and tricks.

In order to write Python code you need a text editor or a Python friendly IDE. No suggestions or influence, whatever suits you is the perfect one. One warning though, Python uses spaces for block indentation so it is very, very, very important that your Python source code file is correctly formatted, otherwise your program is doing something else. This is where a Python aware IDE makes the difference.

Let's start with our programming routine. Understand the problem we want to solve: write a Python program that outputs something to the console. We need to learn how to edit a Python source code file, save it and execute it. We need to understand how the Python language interacts with the console.

In order to create a Python source code file, open you favorite IDE and create a new file. Save this file as hello.py then type the following lines of code:

print ("Hello world from my 1st Python program!")

Save the source code file. You may execute the program either from the IDE, most Python specialized IDE can launch the execution of Python by a click of a button, or from the command line - this is the actual, real, action performed by IDE, also. I will use the command line execution in this book, because it is universal. So, open a terminal window. Change the working directory to the location where your Python files are stored, Chapter_I and you should find hello.py file saved there. Type:

python3 hello.py

The expected result:

Hello world from my 1st Python program!

Congratulations! You have successfully executed your first Python program. You can do it!

What have you learned:

  • how to create, edit and run a Python program

  • how to output text to the console

Comments

One of the most important sections in your source code are the comments. It is the only human readable section. This will save you years of your life, it will be your anchor, it will make the others love and respect you, it will make you a real professional. Writing comments has two fundamental advantages:

  1. Does not introduce any errors in the code

  2. You can actually understand the code just reading the comments

There are two types of comments: one line comments and multi line comments.

Single line comments start with # sign:

# this is a single line comment

Multi line comments are enclosed by """ , three double quotes:

"""
This is a comment on a couple of lines
Which is a good example on how to use comments
"""

Multi line comments can be used as doc strings for functions, modules, classes which will be processed and will generate documentation for users and programmers using you code as libraries. For more information on how automatic documentation generation works from block comments please visit: https://docs.python.org/3/library/pydoc.html?highlight=comments%20documentation%20generation#module-pydoc

You may inspect the file hello_comments.py for an block comment example and Google style guideline. Starting from now I will use in all source files comments.

Learning the language

Basic data types

Numbers

Strings

Lists

Program logic

functions

if statement

while statement

for statement

Data structures

Stack

Queue

Set

Dictionary

Object Oriented Programming notions

Last updated

Was this helpful?