COSC 1336 | PyThon

ACC

Using the Command Line Script Mode

Foreword

In the previous chapter we saw how the interpreter can be run in interactive mode. In this chapter, we will consider a few examples of running the interpreter in script mode. To get started, we will write the programs in Notepad++ and then save the file with a .py extension. When ready to run, we will open a command window and start the Python interpreter which we will use to run our programs.

 

Our First Python Program File

Let's begin by creating a location to store our programs. Create the directory C:\cosc1336-programs as shown.

 

Open Notepad++ and enter the code listed in the example. Save the file as ex-1.py (for exercise 1). Save it to the C:\cosc1336-programs directory. Notice that we assign a string to a variable on line 4 and print the message on line 5. Starting with Python v3, format() like shown on line 14 is recommended for output when working with strings.

 

To run the program, open a command prompt and type python /cosc1336-programs/ex1.py <ENTER>. See the output below and verify that it agrees with the ex-1.py code listing.

 

Be Careful with Compact Syntax

Python introduces a compact way of declaring and assigning variables that may appear unusual to developers experienced with other languages. Note the abbreviated syntax on lines 14 and 17 in ex-2.py. On the surface it may seem simple enough. However, there is a twist of which you should be aware. It is covered below.

Now code ex-2.py in Notepad++ or your text editor. Lines 1, 2, and 3 begin with a hashtag which means they are comments and are ignored by the Python interpreter. However, line 1 has a special meaning when running on Unix-based operating systems like Linux and OSX. The combination of '#!' is referred to as 'she-bang'. That line enables the file to be run by simply typing 'ex-2.py' instead of 'python ex-2.py'. Some developers even use it on Windows systems as a way to quickly identify the file as Python and for the benefit of the line being in place when/if it is ported (moved/copied) to a machine running a Unix-based operating system.

Lines 7-10 and 15-17 represent loops which will be covered more completely in an upcoming chapter. However, let's look at the basic operation at this point. The first while loop will be executed while the condition of 'b < 10' is true. Each time through the loop, b is printed and then the value of a is set to b and b is set to a+b. Notice in the output that b is set to 1, 2, 4, 8.

 

Before running the program, change the directory to the location where your exercise files are stored. I typed 'cd cosc1336-programs' to change to that directory. Once in the directory, only the filename is required after 'python' instead of the full path of the file. See the highlighted lines in the output.

Like in the first while loop, the second while loop will be executed while 'b < 10'. On lines 14 and 17 Python allows a compact syntax to assign values to a and b. On line 14 a is assigned the value of 0 and b is assigned 1. Each time through the loop a is set equal to b and b is set equal to a+b. These operations appear identical to those listed on lines 9 and 10. However, there is a big difference as shown in the output. In the second loop, on line 17, b is assigned the value of a+b before a is assigned the value of b. Look at first two outputs of b from line 16. The values are 1 and 1. The reason the second output is 1 is that a+b on line 17 has the value of 0+1 which equals 1.

 

To follow the results of the preceding program a bit more clearly make the changes shown below. Choose File | Save As... with ex-2.py and save it as ex-3.py. The print statements now display the values of both a and b each time through the loop.

 

Notice in the output that both a and b change values more quickly in the first loop than the second. That is due to the fact that on line 17, the assignment of b=a+b is made before the assignment of a=b. Again, the assignment b=a+b happens before a gets the updated value. The compact syntax processes from right to left. Be sure you understand this example.

 

Print Alternatives

Let's write one more program using Notepad++ and the command line interpreter. Code the ex-4.py as shown below. There is a new concept on lines 5-8. The built-in function 'input()' is being used to obtain input from the user. The input from the user is stored in appropriately named variables. Lines 11-14 so the use of replacement fields (the {}) and the format() built-in function.

Lines 18-21 use a combination of commas and the + sign to produce output. Notice how an undesired space (produced by the comma) precedes the ! in the output of line 18. Also note how lines 19 and 21 use the + sign as a concatenation operator to tie the output together.

See the extra spaces on line 25 and how they are processed. Only one of the first four extra spaces is printed and the extra spaces after the final comma are ignored (the one space is produced by the comma). Line 26 suppresses the separator and therefore the ! is adjacent to the name.

Lines 27-29 demonstrate suppression of both the separator and end of line characters. Notice the effects in the output. Also, notice how "Requirement #'s" are printed in the output to identify the requirements that are being met.

 

Observe the output.

 

Remember to code the examples and ensure you see and understand the output as displayed.