CS171-Lab1

pdf

School

Drexel University *

*We aren’t endorsed by this school

Course

171

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

8

Uploaded by jakelore73

Report
CS 171 Computer Programming I 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: Lab sheet in PDF to Gradescope Questions must be tagged in Gradescope Your lab partner must be added to the submission Only one submission per lab group Submission must be done via Gradescope Student Name(s): Johnson Lin User ID(s) (abc123): jl4449 Possible Points: 80 Your score out of 80 : Lab Grade on 100% scale:
Python Activity 1: Introduction to Python 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 Critical Thinking Questions (30 points): 1. (2pts) Enter the Python program shown above. What does it do? It outputs the phrase, Go! in the shell when compiled. FYI: A "string literal" is a sequence of characters surrounded by double quotation marks (" "). 2. (6pts) Type and execute the following code. What output is produced? Indicate if there is a problem. a. print(“Hello, my name is Pat!”) b. print(Hello, my name is Pat) c. print(“Hello.\nMy name is Pat”)
a. Output the sentence, Hello, my name is Pat! when compiled. (Hello, my name is Pat!) b. It didn’t execute as there is an invalid syntax. The issue is there are no double quotation marks around the string literal. c. Output Hello. on the first line. Then, My name is Pat on the second line. (Hello.) ( My name is Pat) 3. (2pts) What caused the different output format for samples “a” and “c” in question 2? The syntax \n caused the different output format as it breaks the string and jumps to the next line. 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) Output the sum, 7. b. print(2*5) Output the product, 10. c. print(“2+5”) Output the string, 2+5. d. print(“Age:”,20) Output, Age: 20. 5. (8pts) Examine the output for each statement in question 4. a. What is the difference in the output for the statements in “a” and “c” of question 4? Statement “a” outputs the sum, 7. While statement “c” outputs 2+5. b. What caused the difference? The difference was caused by the double quotation marks. c. Which statements contain a string literal ? Statement “c” contains the string literal. 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 is to separate the string literal and the number, so it can print both of them without any issue. It adds a space between the two.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
6. (2pts) Examine the following code and its output. What do the first three lines of the program do? The first three lines of the program are meant to provide information about the program. It allows the reader to know what’s going on inside. 7. (2pts) In the program from question 6, what would happen if you placed a “#” in front of It will make the line into a comment. When you compile the code, it doesn’t execute. 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 \nyour 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) print(56*97)
Critical Thinking Questions (36 points): 1. (2pts) Enter and execute the Python program. What is printed on the screen when the Python program is executed? It printed, What is your name? when executed. Then I inputted my name and it printed out another statement below the first, Your name is Johnson. FYI: input() and print() are functions in Python. 2. (4pts) Examine the first line of a Python program: name = input ( “What is your name? ” ) a. What appears on the screen when this line of code is executed? It outputs the question, What is your name? FYI: The words that appear on the screen and tell the user what to enter are known as a prompt . FYI: name = input ( “What is your name? ” ) The word name in the Python code is a variable – a name given to a memory location used to store data. b. What happens to the data the user entered? The data the user entered would be given to a memory location used to store data. 3. (4pts) Explain the errors that occur when you execute each of the following lines of Python code. a. name? = input(“What is your name?”) The symbol ? next to name, which makes it into an invalid syntax. b. your name = input(“What is your name?”) A variable has to be one word or it can’t store data. c. 1st_name = input(“What is your name?”) The variable is formatted incorrectly, so it can’t store data into it. d. from = input(“Where were you born?”) “from” can’t be used as a variable as it’s also a function.
4. (2pts) Examine the errors that occurred when executing the lines of code in question 3. Then examine the following lines of valid code. name2 = input(“What is your name?”) your_name = input(“What is your name?”) yourName = input(“What is your name?”) List the rules that you need to follow to create a valid variable name. A variable name must start with a letter or an underscore character. A variable can’t start with a number. A variable can’t be spaced out. A variable must contain alphabetical letters, numbers, and underscore characters. A variable can’t be a function. 5. (8pts) Are the following variable names valid ? Are they good names? Why or why not? Variable name Comments about variable name price The variable is valid. Also they are good names because it directly shows what the variable stands for. costoffirstite m The variable is valid, but the name is bad because it is not using the camel case, making it hard to understand. Ic The variable is valid, but the name is bad because I don’t understand what it represents. firstName The variable is valid and the name is good because it has camel case, making it easier to understand. 6. (2pts) Execute the following lines of code. Is the output what you would expect? Why or why not? No because the output would be printing, Your name is and the data stored in Name, not name. Variables are case sensitive. 7. (10pts) Use the following set of Python statements to answer the questions below. a. State the output for each line of code. Your name is Pat. Your name is Pat. Your name isPat. Your age is 20 Invalid, can’t concatenate string with integer.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
b. How are the first two print statements different? Does the difference affect the output? The first two print statements are different because one has single quotation marks, while the other has double quotation marks. The difference doesn’t 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? They don’t produce the same output. The comma separates them and the “+” concatenate them together. e. Explain the purpose of the comma. The purpose of the comma is to separate the values when printing them. e. Why does the last print statement crash the program? What would you do to correct it? The last print statement crashes the program because you can’t concatenate an integer with string. I would make 20 with double quotation marks. 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 outputs the statement, Enter your name: on the screen. After inputting my name, another statement appears, Enter your student ID number: on the screen. When I entered my ID, another statement appeared below it, Enter your course number. Following it, prints my Johnson’s ID is 14487394 onto the next line, and is enrolled in 171. 9. (2pts) What caused the output in the print statement in question 8 to be printed on more than one line? The syntax \n allows 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) 1. (2pts) State a good variable name for an employee’s ID number: A good variable name can be employeeID.
2. (2pts) Write a line of Python code that prompts the user for the name of their favorite ice cream and stores it in a valid variable name. favIcecream = input("Enter your favorite ice cream: ") print("Your favorite ice cream is", favIcecream + ”.”) 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.