Chapter II
Your 1st program
OBJECTIVES
Write and run the first Python structured program
Learn how to use command line arguments
Program structure
Python is a structured programming language. This implies that there is predefined structure of the program execution. When launching python in headless mode, a script will be supplied as input argument. A module’s __name__
is set equal to'__main__'
when read from standard input, a script, or from an interactive prompt.'__main__'
is the name of the scope in which top-level code executes. The following idiom may be used to check whether the script is executed as top level code:
There are a couple of new things in here:
if
is a decision statement allowing you to implement program flow according to boolean logic conditionsimport
is a statement allowing you to use existing packages, either pre-installed with Python distribution, likesys
package or some other custom build packages - your own or othersthe 3rd line is a function call with a value. In fact this is the moment where we actually launch the program.
I would recommend the following recipe for launching the
main
function of a module:
The logic is to launch main
function if this is the main script to be interpreted. The way script is launched is like functional programming - the result of the entire program is the result of main
function.
Inside main
function we recover the input arguments: either the function was called from the main script without any arguments or it was called from another script with arguments. We check the number of arguments and if we do not have the minimum number of required input arguments then we display the usage of the program otherwise we perform the business logic.
This is a generic approach, in fact it is a code template that you may use for every Python script that you want to implement.
Functions
A function is the basic logic structure that performs clear, distinct actions. It may have input parameters and may return a result. It has its own memory space but may interact with global memory space - global variables at the main program level. This is called side effect - please try to minimize side effect code, i.e. code that modifies values of global variables. We will see later that functions and data are packed nicely in classes and this gives as OOP in Python. In this Chapter we will use simple, plain functions.
We already saw main
function and there is a reference to usage
and greetings
functions. Each function has a name and everything is in the name - please give your functions clear, sound names, this will save time and neurons, thank you! Inside the function we see that the code is indented with 4 spaces - this is IMPORTANT, use 4 spaces for code block indentation, otherwise Python will consider the code as part of the main block and the result will be completely different. So free Python programming tip: one place to look for errors in your programs is code indentation!
Important information: if the last statement inside the body of the function is the function itself then we call this a tail recursive call. This is very useful when we want to save memory and implement program logic that allows to reuse the same code to accomplish the task. This concept is very useful when thinking and programming in functional style.
The solution
First let us understand the problem we are asked to solve: write a structured program and use the command line arguments. The program should be able to read the input arguments and use the 1st one as a name in order to greet the corresponding user. A sort of welcome message. Otherwise the program should display a help message that explains to the user how to use correctly the program and also gives an example of correct usage.
Basically this means that we have to use the above code template, to create 2 new functions: usage
and greetings
, both displaying text on the console. greetings
function should take one parameter: a string representing person's name.
We need to add comments and document our code.
The solution is found in Chapter_II
folder args.py
file. Run this file with:
python args.py Dragos
the result should be:
Welcome to the world of Python - Dragos !
What have you learned:
how to create, edit and run a structured Python program
how to manage command line arguments
Homework:
modify the code so that you can process multiple command line arguments. For example you can have:
python args.py Dragos Constantin STOICA
as input parameters. Hint: usemap
function.
Last updated
Was this helpful?