Lab - 01 - CS171
Lab - 01 - CS171
Lab 1
Content by Professor Lisa Olivieri of Chestnut Hill College
Modified for Drexel by Professors Mark Boady and Adelaida Medlock
Detailed instructions to the lab assignment are found in the following pages.
Complete all the exercises and type your answers in the space provided.
For the last question of this lab you must submit a screenshot your source code (.py
file) running in Thonny
What to submit:
Possible Points: 80
Learning Objectives
Students will be able to:
Content:
Explain how to display data in Python
Explain how to create a comment in Python
Determine the difference between a string literal and a number
Explain how to input data using Python
Explain the meaning and purpose of a variable
Determine if a variable name is valid
Explain concatenation and the use of “+”
Process:
Create print statements in Python
Create Python code that displays results to calculated addition facts
Discuss problems and programs with all group members
Create input statements in Python
Create Python code that prompts the user for data and stores it in a variable
Create valid and good variable names
Prior Knowledge
Be able to input and execute Python code using the Python IDE
1. (2pts) Enter the Python program shown above. What does it do?
When you use print(“Go!”) in the python program, it prints out the string in one line of text. In this
case the string literal is Go!
FYI: A "string literal" is a sequence of characters surrounded by double quotation marks (" ").
2. (6pts) Type and execute following code. What output is produced? Indicate if there is a problem.
The string gets printed on two different lines. The top line is
“Hello.” and “My name is Pat” is printed on the line underneath
it.
3. (2pts) What caused the different output format for samples “a” and “c” in question 2?
There are different outputs because \n in question c acts as a new line character. It is used to indicate
the end of a line of text. Therefore, the string gets printed on two lines instead of one.
4. (8pts) What do you think the following Python statements output? Enter the statements in the
interactive mode of the Python interpreter to verify your answers.
a. print(2+5)
When entering this line of code, python prints out the number
“7” which is the simple sum of the 2+5
b. print(2*5)
When entering this line of code, python prints out the number
10, which is the answer to 2*5
c. print(“2+5”)
d. print(“Age:”,20)
a. What is the difference in the output for the statements in “a” and “c” of question 4?
The difference is that in Part a, the sum gets printed out which is 10 and in Part b the entire
string gets printed out, “2+5”
b. What caused the difference?
The difference is caused by the addition of the quotations, the commas indicate that the
whatever is inside them is a string and not an integer.
Part C and Part D have string literals due to the addition of quotations inside the parenthesis
of the print function.
d. What does the comma (,) do in the print statement in part “d” of question 4? How does it
affect the spacing of the output?
The comma acts as a space within the print function and spaces out the string from the
integer.
6. (2pts) Examine the following code and its output. What do the first three lines of the program do?
Output
7. (2pts) In the program from question 6, what would happen if you placed a “#” in front of
If you place a hashtag in front of code, it becomes a comment that is not executed. In other words the
hashtag tells the interpreter to ignore the rest of the code on that line.
Application Questions: Use the Python program mode to design and check your work (4 points)
1. (2 points) Create a Python program containing three statements to print the following output. Copy
your program statements in the space below.
print("Congratulations!")
print("You just created")
print("your first Python program")
2. (2pts) Create a Python program containing two statements that prints the output
to the right. Have the program calculate the answers to the two arithmetic
problems. Copy your program statements in the space below.
print("34 + 123","=",34+123)
print("56 * 97", "=", 56*97)
1. (2pts) Enter and execute the Python program. What is printed on the screen when the Python program
is executed?
After typing the first line of code on the python program, it prints on the screen What is
your name? With implementing the name function, it is required for the user to input a
name into the Shell response. For example, if I respond to the question, “What is your
name?” with the response Bob, it will give a response under the line with “Your name is
Bob”
2. (4pts) Examine the first line of Python program: name = input(“What is your name? ”)
When the line of code is executed , the function is printed and asks for an input to
the question
FYI: The words that appear on the screen and tell the user what to enter are known as a prompt.
3. (4pts) Explain the errors that occur when you execute each of the following lines of Python code.
From executing this line of code, there is a SyntaxError in the variable of the
code due to them using a question mark with the variable name
b. your name = input(“What is your name?”)
From executing this line of code, there is an SyntaxError in the variable as there
should be no space between “your name.” The code should either be indicated
as “your_name” or “yourname”
From executing this line of code, there is a SyntaxError in the variable as there is
an invalid decimal literal in the beginning of the variable.
From executing this line of code, there seems to be no SyntaxError in the code
indicating from the response given in the Shell.
4. (2pts) Examine the errors that occurred when executing the lines of code in question 3. Then
examine the following lines of valid code.
List the rules that you need to follow to create a valid variable name.
1. A variable name must start with a letter or the underscore character.
2. A variable name can’t start with any numerical values, spaces, or any other special
characters.
5. (8pts) Are the following variable names valid? Are they good names? Why or why not?
Variable name Comments about variable name
The variable name is indeed valid. I believe this would be a
price good fitting name as the variable name is short, concise,
and easy to understand
The variable name is indeed valid. However, I do not believe
this is a good fitting name as the variable is too long,
costoffirstitem unconcise, hard to read. It should be written out in the
proper way by using the Camel case therefore stating
costOfFirstItem
The variable name is indeed valid. However, I do not
Ic believe this is a good fitting name as the variable has no
meaning to any scenario given
The variable name is indeed valid. I believe this would be a
firstName good fitting name as the variable name is short, concise,
and easy to understand. It also efficiently uses the Camel
Case when stating this variable.
6. (2pts) Execute the following lines of code. Is the output what you would expect? Why or why not?
After executing the following lines of code, it is the output we expected as the variable
name is spelled differently on the first line than what is provided in the second line
after the print code therefore creating an error in the code.
7. (10pts) Use the following set of Python statements to answer the questions below.
b. How are the first two print statements different? Does the difference affect the output?
c. Notice that some statements include a comma (,) between the two literals being printed and some
statements use a “+”. Do they produce the same output?
e. Explain the purpose of the comma.
e. Why does the last print statement crash the program? What would you do to correct it?
FYI: “+” concatenates two strings. The strings can be string literals or a variable containing string literals.
8. (2pts) State what is displayed on the screen when the following program is executed:
It prints three questions and when you answer each one, there is a final statement that shows your
name, student ID number, and course number.
9. (2pts) What caused the output in the print statement in question 8 to be printed on more than one line?
Application Questions:
Use the Python Interpreter to enter your code and check your work (10 points)
3. (6pts) Crazy Sentence Program. Create a program that prompts the user for the name of an
animal, a color, the name of a vehicle, and the name of a city. Then print a sentence that contains
the user input in the following order: color, animal, vehicle, city. Include the additional words in
the sample output as part of your output. Example: Assume the user enters the words: tiger,
green, motorcycle, and Wildwood. The output would be: The green tiger drove the motorcycle
to Wildwood.
Once your program is complete, take a screenshot of the code and output of the program. You
may need multiple screenshots to capture all your code. Paste the screenshot below.
Your code must include comments with the names of all group members and the date the code
was written.