CHAPTER 1 Flashcards
DEFINE a computer program
What are Basic instruction types? Define them
A computer program consists of instructions executing one at a time. Basic instruction types are:
Input: A program gets data, perhaps from a file, keyboard, touchscreen, network, etc.
Process: A program performs computations on that data, such as adding two values like x + y.
Output: A program puts that data somewhere, such as to a file, screen, or network.
Define variables
variables to refer to data, like x, y, and z below. The name is due to a variable’s value “varying” as a program assigns a variable like x with new values.
Define Computational thinking
Define algorithm.
Computational thinking, or creating a sequence of instructions to solve a problem, will become increasingly important for work and everyday life. A sequence of instructions that solves a problem is called an algorithm.
Define Python interpreter
Interactive interpeter
Code
Prompt
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.
Define a statement Expressions assignment The print() function '#'
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.
Define String lateral
Text enclosed in quotes is known as a string literal. Text in string literals may have letters, numbers, spaces, or symbols like @ or #.
ext enclosed in quotes is known as a string literal. Text in string literals may have letters, numbers, spaces, or symbols like @ or #.
Input # Including end=' ' keeps output on same line print('Hello there.', end=' ') print('My name is...', end=' ') print('Carl?')
Output:
Hello there. My name is… Carl?
Outputting a variable’s value
The value of a variable can be printed out via: print(variable_name) (without quotes).
The value of a variable can be printed out via: print(variable_name) (without quotes).
Input
wage = 20
print(‘Wage is’, end=’ ‘
print(wage) # print variable’s value
print(‘Goodbye.’)
Output
Wage is 20
Goodbye
Outputting multiple items with one statement
Programmers commonly try to use a single print statement for each line of output by combining the printing of text, variable values, and new lines. A programmer can simply separate the items with commas, and each item in the output will be separated by a space. Combining string literals, variables, and new lines can improve program readability, because the program’s code corresponds more closely to the program’s printed output.
Printing multiple items using a single print statement.
wage = 20
print(‘Wage:’, wage) # Comma separates multiple items
print(‘Goodbye.’)
Wage: 20
Goodbye.
Newline characters
Output can be moved to the next line by printing “\n”, known as a newline character. Ex: print(‘1\n2\n3’) prints “1” on the first line, “2” on the second line, and “3” on the third line of output. “\n” consists of two characters, \ and n, but together are considered by the Python interpreter as a single character.
Printing using newline characters. print('1\n2\n3') 1 2 3
printing without text. print('123') print() print('abc') 123
abc
Basic input
Many useful programs allow a user to enter values, such as typing a number, a name, etc.
Reading input is achieved using the input() function. The statement best_friend = input() will read text entered by the user and the best_friend variable is assigned with the entered text. The function input() causes the interpreter to wait until the user has entered some text and has pushed the return key.
Input print('Enter name of best friend:', end=' ') best_friend = input() print('My best friend is', best_friend)
Output
Enter name of best friend:
Marty McFly
My best friend is Marty McFly
Converting input types
The string ‘123’ (with quotes) is fundamentally different from the integer 123 (without quotes). The ‘123’ string is a sequence of the characters ‘1’, ‘2’, and ‘3’ arranged in a certain order, whereas 123 represents the integer value one-hundred twenty-three. Strings and integers are each an example of a type; a type determines how a value can behave. For example, integers can be divided by 2, but not strings (what sense would “Hello” / 2 make?). Types are discussed in detail later on.
Reading from input always results in a string type. However, often a programmer wants to read in an integer, and then use that number in a calculation. If a string contains only numbers, like ‘123’, then the int() function can be used to convert that string to the integer 123.
Figure 1.3.7: Using int() to convert strings to integers.
my_string = ‘123’
my_int = int(‘123’)
print(my_string)
print(my_int)
123
123
A programmer can combine input() and int() to read in a string from the user and then convert that string to an integer for use in a calculation.
Figure 1.3.8: Converting user input to integers.
Input print('Enter wage:', end=' ') wage = int(input())
new_wage = wage + 10
print(‘New wage:’, new_wage)
Output
Enter wage: 8
New wage: 18
Input prompt
Adding a string inside the parentheses of input() displays a prompt to the user before waiting for input and is a useful shortcut to adding an additional print statement line.
Figure 1.3.9: Basic input example. Input hours = 40 weeks = 50 hourly_wage = int(input('Enter hourly wage: '))
print(‘Salary is’, hourly_wage * hours * weeks)
Output Enter hourly wage: 12 Salary is 24000 ... Enter hourly wage: 20 Salary is 40000
Define syntax error
syntax error, is to violate a programming language’s rules on how symbols can be combined to create a program. An example is putting multiple prints on the same line.
The interpreter will generate a message when encountering a syntax error. The error message will report the number of the offending line,