The objectives of this lab are to become familiar with the interactive and script modes in Python, and to learn the basics of input, processing, and output in Python.
You will submit a text document with your answers to the problems posed in the lab.
Editor
and
iPython console
. Your screen should look much less
complex now.iPython console
. (This is the
right side of the Spyder window.)
You might try something like the following commands:
print(3 + 4)
print("Hello class")
print("Hello" + "class")
print("Hello", "class")
Stop and Think: (These questions are to think about; they do not need to be submitted.) What happened when you typed in these statements? Could you type them all at once and then get your output, or did you type them one at a time, getting a result after each one? Was that what you expected?
Editor
, and then go to
File->New File. This will create a new file in your
Editor
. iPython console
.
Stop and Think: What happened this time? Did you get any output? Was this what you expected?
Lab1.py
, and then choose the folder in
which you'd like to save your work. It would be a good idea to create a
COMP108 folder to keep all of your work for this class in one
place.Editor
pane. If you get a
question about the Run settings, you can choose to execute in the
current iPython
console.
Stop and Think: What happened now? Did you get any output? If so, where did your output appear?
Problem 1: (This is the first
of the problems to be typed up for submission.) Write a few
sentences to describe the similarities and differences between using the
iPython console
and the Editor
in Spyder.
iPython console
or
the Editor
.
print "Hello" + "class"
, the
resulting output had no space between the two words. Experiment with
this statement so that the output contains a space between the two
words.
print
statements above using
single quotes around the text instead of double quotes. You should
still get the same output; do you?print """I'm reading "Hamlet" tonight."""
Type in this
example and experiment with what happens when you replace the single
quote with a double quote, the double quote with a triple quote and
the triple quote with a single quote. .Problem 2: Write down a statement
that displays the following text: COMP108's textbook is
"A Primer on Scientific Progamming with Python".
(You may want to try typing
your line of code in the iPython console
to be sure it
works!)
Single line comments in Python begin with the #
character. For good
programming style, every script you write should contain comments at the
beginning that give your name, the date, and a description of the
purpose of the script. For example, the script containing the print
statements above might look like the following:
# Pam Cutter
# September 12, 2017
# Modified on September 14, 2021 to update for Python 3
# This script contains a few print statements
# to explore different aspects of printing
print(3 + 4)
print("Hello class")
print("Hello" + "class")
Comments that span more than one line may be placed inside a pair of """. The previous example would look like the following, using the multi-line comment:
"""
Pam Cutter
September 12, 2017
This script contains a few print statements
to explore different aspects of printing
"""
print(3 + 4)
print("Hello class")
print("Hello" + "class")
Editor
.
Problem 3: Write a statement that prints out Hello
yourname. Today is dayOfTheWeek.
where yourname
gets its value from your variable and
dayOfTheWeek
is another variable that you
create and initialize.
input
function. (The input
function in Python 3 works the same as the
raw_input
function in Python 2.7.)
name
and then print out the value of that
variable.
Problem 4: Write
down the statements that would be needed to get a name from the user and then print
out "Hello" with that name. For instance, if the user entered
Pam
, the output would be Hello Pam
.
Problem 5: In your own words,
describe why the float
and int
functions
are included with some input statements but not others. When do
you need to apply the eval
function?
iPython console
or
as print statements in your script to test your predictions.Stop and Think: How did the actual results of these statements compare with your expected results? Did they agree? If so, great job - you understand these operators! If not, make sure you understand what you missed. Feel free to discuss with the instructor or a TA if necessary.
iPython console
or as
print statements in your script.
Stop and Think: How did your actual results compare with your predicted results? Discuss with an instructor or TA if you do not understand any differences.
Problem 6: Write statements to complete the following program:
Calculate tip, tax, and total of a meal purchased at a restuarant. The program should ask the user to enter the total charge for the food, and then calculate the amount of an 18% tip and 6% sales tax. Display each of these amounts and the total. Variation: In addition to asking the user to enter the total charge for the food, ask the user for the percentage they would like to tip and the sales tax percentage.
Turn in your answers to the problems in these exercises and the script you saved. (Submit these 2 documents on Kit. Be sure your name is on both of them.)