Section 1

.pdf

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

140 - X625

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

2

Uploaded by DeaconCaterpillar4154 on coursehero.com

Students: Section 1.2 is a part of 1 assignment: 1-3 zyBooks Participation Activities Includes: PA 1.2 Programming using Python Python interpreter The Python interpreter is a computer program that executes code written in the Python programming language. An interactive interpreter is a program that allows the user to execute one line of code at a time. Code is a common word for the textual representation of a program (and hence programming is also called coding ). A line is a row of text. The interactive interpreter displays a prompt (" >>> ") that indicates the interpreter is ready to accept code. The user types a line of Python code and presses the enter key to instruct the interpreter to execute the code. Initially you may think of the interactive interpreter as a powerful calculator. The example program below calculates a salary based on a given hourly wage, the number of hours worked per week, and the number of weeks per year. The speci±cs of the code are described elsewhere in the chapter. PARTICIPATION ACTIVITY 1.2.1: The Python interpreter. PARTICIPATION ACTIVITY 1.2.2: Match the Python terms with their de±nitions. If unable to drag and drop, refresh the page. A program that executes computer code. The text that represents a computer program. Informs the programmer that the interpreter is ready to accept commands. A row of text. Executing a Python program The Python interactive interpreter is useful for simple operations or programs consisting of only a few lines. However, entering code line-by-line into the interpreter quickly becomes unwieldy for any program spanning more than a few lines. Instead, a programmer can write Python code in a ±le, and then provide that ±le to the interpreter. The interpreter begins by executing the ±rst line of code at the top of the ±le, and continues until the end is reached. A statement is a program instruction. A program mostly consists of a series of statements, and each statement usually appears on its own line. Expressions are code that return a value when evaluated; for example, the code wage * hours * weeks is an expression that computes a number. The symbol * is used for multiplication. The names wage, hours, weeks, and salary are variables , which are named references to values stored by the interpreter. A new variable is created by performing an assignment using the = symbol, such as salary = wage * hours * weeks , which creates a new variable called salary. The print() function displays variables or expression values. '#' characters denote comments , which are optional but can be used to explain portions of code to a human reader. Many code editors color certain words, as in the below program, to assist a human reader in understanding various words' roles. PARTICIPATION ACTIVITY 1.2.3: Executing a simple Python program. Python interpreter Name Value >>> wage = 20 wage 20 >>> hours = 40 >>> weeks = 50 weeks 50 | >>> salary = wage * hours * weeks >>> print(salary) 40000 >>> hours = 35 hours 35 >>> salary = wage * hours * weeks 35000 salary >>> print(salary) 35000 >>> 1. After each press of the enter key, the python interpreter executes the line of code. 2. The python interpreter can be used as a calculator and can perform a variety of calculations. 3. Users can change values and execute calculations again. Captions Feedback? Line Prompt Interpreter Code Reset Feedback? Start 2x speed Start 2x speed
Activity summary for assignment: 1-3 zyBooks Participation Activities 98 % 98 % submitted to desire2learn PARTICIPATION ACTIVITY 1.2.4: Python basics. 1) What is the purpose of variables? Store values for later use. Instruct the processor to execute an action. Automatically color text in the editor. 2) The code 20 * 40 is an expression. True False 3) How are most Python programs developed? Writing code in the interactive interpreter. Writing code in ±les. 4) Comments are required in a program. True False zyDE 1.2.1: A ±rst program. The below program simulates a race between two cars, displaying the position of each car at the end of the race. Make sure the output box below the code is visible, then click "run." The car1_top_speed and car1_acceleration variables control the maximum velocity and acceleration of car 1. Modify these variables, and run the program again. Can you make the second car win? You do not need to understand how the code works right now. Instead, just modify the speed and acceleration variables and observe how the output changes. How was this section? | wage = 20 hours = 40 weeks = 50 salary = wage * hours * weeks print ( 'Salary is:' , salary ) hours = 35 salary = wage * hours * weeks print ( 'New salary is:' , salary ) file.py Python interpreter >>> Name Value wage 20 weeks 50 salary 40000 Salary is: hours 35 35000 35000 New salary is: 1. The python interpreter reads a ±le line by line. Variables wage, hours, and weeks are named references that refer to values stored by the interpreter. 2. 20 * 40 * 50 is computed, and then assigned to the variable salary. 3. The print statement prints 'Salary is:' to the screen and displays the value of the variable salary. 4. Values can be overwritten if the same variable name is used. Captions Feedback? Feedback? Load default template... # Welcome to the Python 500 race! Click the run button to begin. # Configurable values. # Try changing car speeds, accelerations, and the simulation speed. car1_top_speed = 60 car2_top_speed = 50 car1_acceleration = 11 car2_acceleration = 10 car1 = [ ' ______\n' , ' |__1_|_\\\n' , ' O----O\n' ] car2 = [ ' ___\n' , ' /__2_\\__\n' , ' O----O\n' ] Run Feedback? Provide section feedback Completion details 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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