Back Python Chrestomathics Ahead |
Part 2 — Basic anatomy
An anatomy — Needs to be undertaken — Of our first program
Do not be afraid — Do not hide what makes you, you — You are welcomed here
Section 2.1: Here we go
Communication — The essence of love and work — And now in Python
- When teaching something brand new, I start with what is necessary. A program is only worthwhile if it communicates with its user. So, we will start there — show you how to print out information.
- It may seem odd to a beginner programmer, but programs are like essays — they are meant to be read by people other than their authors. Successful programs are often adapted for other purposes. To support that process, you should write code that is kind to its readers.
- So, all your programs, besides containing the instructions to be carried out, will document what is going on. And as a matter of pride, will name you as its author and provide contact information.
- Many introductory courses have their first program print the message Hello World. They do so to honor the creators of the C programming language. C is the concestor programming language of Python and many other modern programming languages.
- I like the first program to print some truth I want my students to know. The truth displayed here is the title of a song by The Beatles.
All you need is love
- The program printing that message is love.py. The program has both documentation and a print instruction. Its contents are
""" Purpose: demonstrate user communication
Author: Jim Cohoon
Email id: jpc
"""
# Send message to user
print( "All you need is love" )
- The very last line is the Python instruction that displays the message.
print( "All you need is love" )
The preceding lines are documentation. As mentioned above, they are there because of their importance when writing code.
- In-program documentation are called comments. Python supports two ways of commenting what is going on. The program starts with a multi-line string comment. This comment is also called a header comment.
""" Purpose: demonstrate user communication
Author: Jim Cohoon
Email id: jpc
"""
- Header comments occur at the start of the program file. At a minimum, they document the purpose of the code and provide contact information.
- A string comment starts with triple quotes, all the text that follows, no matter what characters they are, is part of that comment string until a matching triple quote is reached. A string comment can use either three double or single matching quotes as delimiters. Standard Python style guidelines prefer the use of double quotes.
- When Python encounters a
#
that is not part of a string, the rest of the line is ignored as a comment to the reader. The comment
# Send message to user
is an explanatory comment. These comments often occur at the start of a new section of code to explain what is happening next, or within a section of code to provide detail.
- There is a single blank line separating the two comments. Python generally ignores spacing between code elements. The spacing is called white space.
- There is an exception about ignoring white space. Code that is not part of the proceeding lines, must start in column 1. Code that is part of the proceeding lines must be indented. We will talk more about indenting when we consider decision making and repetition.
- To satisfy your now heightened anticipation on how to print, let’s look at the last line in
love.py
.
print( "All you need is love" )
- To simplify programming, Python provides some built-in functions. The built-in functions are already-written units of code that come with your Python application. The print() function is one of the built-in functions. If you click the preceding links do not get weirded out about what is there — it’s written in formal Computer Science-ese. You will become more fluent with it over time.
- To invoke (start up) a function to carry out its task, you supply its name and a pair of matching parentheses. In between the parentheses, you give the information the function needs to carry out its job. Commas are used to separate out that information.
- When talking about Python functions, some of the vocabulary comes from algebra. In particular, the information given to the function are called arguments. Each argument is evaluated, and a copy of the value is passed to the function.
- The
print()
function displays information to the user. Among other things, that information can be numbers or strings of characters.
- To get a string of text displayed with a
print()
statement, the string is surrounded by either a single or double quote. The end of the string occurs when the first occurrence of a matching quote is found. The delimiting quotes are not printed. For compatibility with string syntax in other programming languages, I will tend to use double quotes.
- Unless told otherwise, the
print()
function separates the different arguments being displayed by a single space. If there are no arguments to the function, a blank line is printed.
- Also, unless told otherwise, after printing the requested information, the
print()
function prints a newline character that causes the next output to start on the following line.
- So officially when our
print()
statement is reached,
print( "All you need is love" )
Python first checks that the statement starts in column 1. It then causes the built-in
print()
function to be invoked. The function then displays its string argument"All you need is love"
.
White spaceThe following statements are equivalent to Python for getting our love message displayed. The first one is our preference.
|
Section 2.2: Onward
- Our second program is camouflage_not_needed.py. I like the second program to also display a message how our class is to be run. Observe that the program does not indicate its author. I do not, because it seems too egotistical to constantly flag who I am.
""" Purpose: demonstrate user communication
"""
# Display haiku
print( "Camouflage not needed" )
print()
print( "Do not be afraid" )
print( "Do not hide what makes you, you" )
print( "You are welcomed here" )
- The program displays a haiku. It maybe offbeat, but its message is important — everyone is welcomed just as they are.
Camouflage not needed
Do not be afraid
Do not hide what makes you, you
You are welcomed here
- The takeaway here is that a Python program can consist of multiple statements. The statements are executed from top to bottom, one after the other.
Section 2.3: Just a little more
- Our next program is mix_it_up.py
""" Purpose: display strings, integers, and decimal values
"""
print( 21, "Celsius is", 69.8, "Fahrenheit" )
- The program demonstrates that
print()
statements can display more than value one per use. When multiple values are to be printed, separate the values with commas. The program also demonstrates that both strings and numbers can be displayed. The numbers can be either integer or decimal.
21 Celsius is 69.8 Fahrenheit
- It is a common gotcha to forget to provide separating commas for the values or to forget to supply a starting or closing quote. When you have a bad
print()
statement first check if something is missing.
- Now consider the following program before reading on.
print( "a,", "b, ", 1, 2, 12 )
print( "a a", " b")
- It is left to you to determine why it should output
a, b, 1 2 12
a a b
- Now consider the following program before reading on. It is left to you to determine why each statement has a problem or problems.
Print( "a" 1 "b" )
Print( a, b
Print( 'a", 'b" )
Print( "a", 'b' )
Section 2.4: What’s next
- Firstly, well done. You now know how to print information using Python. You are a programmer. We will next consider how to and store and calculate values.
The Beatles All You Need Is Love, © Capitol Records
Back Ahead |