Module 0.1 - Getting Started: Examples
0.1.1 - Whitespace
Consider the following program:
print ( 'Hello World!' )
Notice the spaces inserted in various places.
Consider this variation:
print('Hello World!')
(There are three spaces before the print
statement)
Running this program produces an error - spaces at the beginning of a line (“indentation”) are how Python logically groups code, and improper indentation can cause errors.
Finally look at:
print('Hello World!')
(Six spaces between Hello
and World!
)
Exercise 0.1.2
Let’s point out a few things: - Some kinds of whitespace, even if ill-advised, are permitted. - Consistent style makes your code more readable. - The beginning of each line of code must be properly indented. This is why there was an error with the line that was indented before print
. - The extra spaces in between Hello
and World!
in the third example are acceptable, if it is your goal to print them. Printing accepts whatever spaces in the text you provide.
0.1.2 - Strings
A string in Python is a sequence of letters, digits, or symbols (such as & or @) surrounded by either:
- A pair of double quotes, as in “Hello world!”
- A pair of single quotes, as in: ‘Hello world!’
Note:
- Whichever kind of quote (single or double) you use to start a string must be used to end the string.
- The ending quote must be on the same line as the starting quote.
- There are special techniques to handle long strings that you want to print over several lines.
- This might raise some questions:
- Is it possible to print on a single line using multiple print statements?
- How does one print a quotation mark?
First, note that we can use single or double quotes for different strings in the same program:
print('George')
print("Washington")
Exercise 0.1.3
A print
statement prints the string within parentheses and then goes to the next line of output, which is why we see Washington
on the next line after George
.
To keep printing on the same line:
print('George', end=' ')
print('Washington')
Exercise 0.1.4
Now we will use a single string containing the instruction to print on several lines:
print('George\nWashington')
Notice the \n
inside of the string George\nWashington
.
Exercise 0.1.5
Strings can include escape sequences
, which begin with the backslash, \
. Escape sequences are used to print “special characters.” A special character can be something like \n
, which represents a new line. A special character can also be \'
, which represents a literal quotation mark rather than telling Python where a string starts or begins.
print('My friend\'s friend')
Another way is to use double quotes to delimit a string that contains single quotes, or vice versa:
print('My friend\'s friend')
print("baked me an apple pie")
print('and I said "thank you."')
How does one print a backslash? By using a double backslash (a backslash escaped by another backslash.)
print('The backslash character, \\, is printed like this.')
Exercise 0.1.6
Another use of the backslash is to make long strings:
- Sometimes we need to type in a very long string
- The following does not work:
print('The woods are lovely, dark and deep
But I have promises to keep
And miles to go before I sleep
And miles to go before I sleep.')
Exercise 0.1.7
To spread a single string over multiple lines, use triple quotes such as:
print('''The woods are lovely, dark and deep
But I have promises to keep
And miles to go before I sleep
And miles to go before I sleep.''')
(You can also use triple double quotes, """
.)
Exercise 0.1.8
Empty strings:
- It is possible to have a string with nothing in it, such as:
print('')
- Notice that there are no letters, digits, or anything in between the two single quotes.
- This is called an empty string.
- Empty strings are useful (we will see later) when we want to combine strings to make a longer string.
0.1.3 - Case Sensitivity
What would happen if we used uppercase P
instead of lowercase p
in print
?
'Hello World!') Print(
Exercise 0.1.9
What if we changed the case inside of a string?
print('helLo wOrLd!')
Exercise 0.1.10
Python is case sensitive, but strings are like data inside of programs, which means they can take on any value we give them.
- The two strings
'Hello World!'
and'helLo wOrLd!'
are fine as two different strings, if that is our intention. - Python only has one
print
function and does not recognizePrint
(with an uppercaseP
).
0.1.4 - A Peek At The Future
We will occasionally jump ahead and present an example of a program that does something advanced, just so you get a feel for what’s coming. With these examples, we’ll only ask you to type up the program and run the program.
And then we’ll point out a few features, just so that you get acquainted with features that you’ll eventually encounter.
import tkinter as tk
= tk.Tk()
window = tk.Canvas(master=window, width=500, height=400)
canvas
canvas.pack()
for i in range(1, 10):
= i * 20
fontSize = 'Times ' + str(fontSize) + ' italic bold'
fontStr = 200 + 10 * i
startx = 20 + i * 20
starty ="Hello", font=fontStr, fill='grey')
canvas.create_text(startx, starty, text
window.mainloop()
Exercise 0.1.11
If something went wrong with the typing, you can instead download hello_gui.py but only after giving the typing a good try. Now let’s point out a few features of the program, and we’ll focus on the middle section:
By showing you bits and pieces of advanced code, you’ll be ready to absorb these concepts when we work through them.
One of the most important things to observe about is that some code is indented:
0.1.5 - A Reminder About Computer Skills
As you complete Module 1 and ready that for submission you might want to review:
- Did you make your
module0.1
folder? Did you ensure that them
inmodule0.1
is lowercase and that there’s no space betweenmodule
and1
? - Are your Python programs from this module in that folder?
- Do you recall how to make a zip? If it’s a bit hazy, please make sure to review from the material about computer skills.
- Finally, make sure you understand the instructions for how to submit your work.
End-Of-Module Problems
Full credit is 100 pts (complete at least three problems). There is no extra credit.
0.1.0 - Comments
A comment is a note in a program that is designed to be read by human beings. It is not interpreted at all by the computer and will not run anything.
#
character and run until the end of the lineTo illustrate:
The above program is, as far as Python is concerned:
Comments can be inserted in various places in a program. Let’s run code with comments and look at the output:
The output is the same as if there were no comments at all.
Exercise 0.1.1
Create a
module0.1
folder (if you haven’t already). Write up the above code incomments.py
.Sometimes one needs a comment to spill over multiple lines, such as:
There is an error on the second line of this code: a
#
is missing.