What Is Programming?
Example: Programming Languages and a Ticket Total
A programming language is a formal system for expressing instructions with specified rules and meanings. Its syntax is the set of rules governing how symbols and statements may be arranged. You need both a permitted arrangement and instructions that express your intended calculation.
A school theatre sells eight tickets at 7 dollars each and charges one booking fee of 3 dollars for the entire order. A student writes the following source code in Python, but one symbol is missing.
tickets = 8
price = 7
fee = 3
total = tickets price + fee
print(total)For this example, = stores the value on its right under the name on its left. The symbol * means multiplication, and multiplication occurs before addition. The instruction print(total) displays the stored value of total. All monetary values in the fragment represent dollars.
a) Repair the line defining total. Explain why Python cannot use the original line as written.
The intended ticket cost is the number of tickets multiplied by the price of each ticket. Python requires an explicit multiplication operator between these two names; placing them next to each other does not express multiplication.
The corrected line is:
total = tickets * price + feeThe original line breaks Python's syntax rules because the required operator between tickets and price is missing. Supporting software cannot carry out that line as a valid Python assignment.
b) After this repair, what number does the program display, and what does it represent?
First calculate the ticket subtotal. Multiplication is evaluated before the booking fee is added:
The intermediate ticket subtotal is ________. Add the single booking fee to obtain the order total:
The assignment stores 59 in total, and print(total) displays 59. This represents an order total of 59 dollars; the program does not add a currency label to the displayed number.
c) The student instead changes the repaired line to total = tickets * (price + fee). Find the new displayed number and decide whether this valid expression follows the stated pricing rule.
Parentheses require the program to add price and fee first:
The grouped amount is ________. The program then multiplies that amount by the number of tickets:
The program displays 80, representing 80 dollars. This expression is valid Python, but it applies the booking fee to every ticket. The stated rule charges that fee once for the entire order, so this expression does not implement the intended price.
Valid syntax does not guarantee the intended result. Check both whether the language accepts the instructions and whether their meaning matches the task.
A student creates and revises instructions for a school announcements app. Later, a computer carries out those instructions to display the announcements.
Use the words programming and execution once each to complete the descriptions.
Creating and revising the app's instructions is . The computer carrying out the instructions that already exist is .
A beginner writes instructions, checks their arrangement, and runs them to display a message. Match each programming term to its description.
A formal system for expressing instructions with specified rules and meanings
Rules governing how symbols and statements may be arranged
Information produced when instructions are executed
Program text written in a particular language
Source code
Program output
Programming language
Syntax