At the start of the
installation instructions,
you created a directory (folder) named
python
to hold your Python scripts.
The simplest way to create a Python script (program) is to launch
IDLE,
pull down
File
→ New File
and write your script in the new Untitled window.
When you’re ready to save the script, pull down
File → Save
Give the script a name that ends with
.py
.
But don’t give the script the same name
as one of the Python
modules
that you might
import
.
For example, don’t name your script
math.py
,
because there is a Python module named
math
.
Then save the script in your
python
folder.
Do not write the line numbers when you type your Python script. The line numbers are not part of the script. They’re displayed by GitHub for your convenience. To see (and download) the script without the line numbers, press the Raw button in the upper right corner of the script.
A
'
single-quoted'
or
"
double-quoted"
string has to fit on a single line.
But a string in
three
pairs of quotes
(either single or double)
can occupy several lines.
A string of documentation at the start of a Python script is called a docstring. It will usually be so long that it has to be triple-quoted. See Documentation Strings. We won’t attempt to print the docstring until we turn this Python file into a module, much later in the course. But if you just can’t wait, insert the line
if __name__ == '__main__':immediately before the first
print
statement,
and indent each of the following statements with
four
spaces.
Then launch the macOS Terminal app,
go to the directory that holds your Python program
flag.py
,
and say the following.
-w
stands for “write”,
-b
stands for “browser”.
pydoc3 -w flag wrote flag.html ls -l flag.html -rw-r--r--@ 1 myname mygroup 623 May 15 12:15 flag.html pydoc3 -bIn the browser window that appears, click on
flag
.
Then remove the
if
and the indentation.
We
saw
that the function
round
accepts a numeric argument:
round(19.95)
The function
print
accepts either a numeric argument or a string argument:
print(19.95) print("* * * * * * =====================")
It looks like each
print
in our
script is outputting 33 characters.
For example,
our first
print
outputs 6 asterisks, 6 spaces, and 21 equal signs:
print("* * * * * * =====================")
But this
print
actually outputs 34 characters,
because it also outputs the invisible
newline character
at the end of the line.
That’s why the output of the second
print
appears below the output of the first
print
.
The
print
in Python 3
needs parentheses because it is a function.
The
print
in Python 2 did not need parentheses because it was a statement,
like
import
.
See
Common
Stumbling Blocks.
print
is implemented
in
the language C.
We
saw
that the
function
log
belongs to the module (family)
math
:
import math math.log(32, 2)
The function
exit
belongs to the module
sys
.
import sys sys.exit(0)
On macOS, Microsoft Windows, and Linux,
a program at the end of its life can give a little invisible number
to the operating system
to indicate whether the program succeeded or failed at its primary mission.
This number is called the program’s
exit status.
(In Microsoft Windows, it’s sometimes called the
errorlevel
.)
On all three operating systems,
the number 0
indicates successful termination.
(There’s another
exit
,
one that does not belong to
sys
.)
In IDLE,
pull down
Run
→ Run Module
or press fn-F5 on macOS.
IDLE will insist that you save the script before you run it.
Save the script in your
python
folder.
Source must be Saved.
OK to Save?
OK
The nine lines of standard output produced by the script will appear in IDLE’s Python 3.8.0 Shell window. You can then copy and the paste this output somewhere else.
* * * * * * ===================== * * * * * ===================== * * * * * * ===================== * * * * * ===================== * * * * * * ===================== ================================= ================================= ======== E pluribus unum ======== =================================
print("""* * * * * * ===================== * * * * * ===================== * * * * * * ===================== * * * * * ===================== * * * * * * ===================== ================================= ================================= ======== E pluribus unum ======== =================================""")
print( """* * * * * * ===================== * * * * * ===================== * * * * * * ===================== * * * * * ===================== * * * * * * ===================== ================================= ================================= ======== E pluribus unum ======== =================================""")
print("""\ * * * * * * ===================== * * * * * ===================== * * * * * * ===================== * * * * * ===================== * * * * * * ===================== ================================= ================================= =========E pluribus unum========= =================================""")
print
with no argument outputs a line consisting only of one newline character.
In other words,
it skips a line.
print("Your most humble and obedient servant,") print() print("G. Washington")
Your most humble and obedient servant, G. Washington
#Could produce the same output with one triple-quoted string. print("""\ Your most humble and obedient servant, G. Washington""")
round
function,
separating them with a comma:
round(100_000.00 * 1.04 ** 20, 2)Let’s give several arguments to the
print
function,
separating the arguments with commas.
Put a space after each comma because we put a space after each comma in English.
(The space after each comma will have no effect on the output.)
The arguments will be printed side-by-side on the same line,
separated by one space.
print("Smith", "John", "100 Sunnyside Drive", "Pleasantville", "NY", "10707")
Smith John 100 Sunnyside Drive Pleasantville NY 10707
print
has a
keyword
argument
named
sep
(for “separator”).
By default, the value of this argument is
" "
(one space).
print("Smith", "John", "100 Sunnyside Drive", "Pleasantville", "NY", "10707") print("Smith", "John", "100 Sunnyside Drive", "Pleasantville", "NY", "10707", sep = ":") print("Smith", "John", "100 Sunnyside Drive", "Pleasantville", "NY", "10707", sep = ",") print("Smith", "John", "100 Sunnyside Drive", "Pleasantville", "NY", "10707", sep = "--")
Smith John 100 Sunnyside Drive Pleasantville NY 10707 Smith:John:100 Sunnyside Drive:Pleasantville:NY:10707 Smith,John,100 Sunnyside Drive,Pleasantville,NY,10707 Smith--John--100 Sunnyside Drive--Pleasantville--NY--10707
print
appears on a new line.
print("You end up with $") print(round(100_000.00 * 1.04 ** 20, 2)) print("after 20 years.")
You end up with $ 219112.31 after 20 years.
print
has another
keyword
argument
named
end
.
The value of this argument defaults to
"\n"
(one newline character; see
Escape
Sequence).
That’s why the output of each
print
appears on a new line.
To make the output of each
print
appear on the same line,
we can suppress the newline.
This is useful when we have to build up a line of output
with several separate calls to
print
.
print("You end up with $", end = "") print(round(100_000.00 * 1.04 ** 20, 2), end = "") print(" after 20 years.")
You end up with $219112.31 after 20 years.
#Could produce the same output with one statement using a formatted string literal. print(f"You end up with ${round(100_000.00 * 1.04 ** 20, 2)} after 20 years.")
import sys
print("This is output.") #Prints in blue in IDLE. print("This is output.", file = sys.stdout) #Does the same thing. print("This is an error message.", file = sys.stderr) #Prints in red in IDLE.