CHAPTER 1 Flashcards

1
Q

DEFINE a computer program

What are Basic instruction types? Define them

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define variables

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define Computational thinking

Define algorithm.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define Python interpreter
Interactive interpeter
Code
Prompt

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Define a statement 
Expressions 
assignment
The print() function 
'#'
A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define String lateral

A

Text enclosed in quotes is known as a string literal. Text in string literals may have letters, numbers, spaces, or symbols like @ or #.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

ext enclosed in quotes is known as a string literal. Text in string literals may have letters, numbers, spaces, or symbols like @ or #.

A
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?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Outputting a variable’s value

The value of a variable can be printed out via: print(variable_name) (without quotes).

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

Printing multiple items using a single print statement.
wage = 20

print(‘Wage:’, wage) # Comma separates multiple items
print(‘Goodbye.’)
Wage: 20
Goodbye.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A
Printing using newline characters.
print('1\n2\n3')
 1
2
3
printing without text.
print('123')
print()
print('abc')
 123

abc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define syntax error

A

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,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Find the syntax errors. Assume variable num_dogs exists.

1) print(num_dogs).
2) print(“Dogs: “ num_dogs)
3) print(‘Woof!”)
4) print(Woof!)

A
  1. Error. There should not be a period at the end
  2. Error Missing a comma
  3. Error Mismatched quotes
  4. Error. Needs quotes
17
Q

Define runtime error,

crash of the program.

A

runtime error, wherein a program’s syntax is correct but the program attempts an impossible operation, such as dividing by zero or multiplying strings together (like ‘Hello’ * ‘ABC’).

A runtime error halts the execution of the program. Abrupt and unintended termination of a program is often called a crash of the program.

18
Q
Common error types.
SyntaxError	
IndentationError	
ValueError	
NameError	
TypeError
A

The program contains invalid code that cannot be understood.

The lines of the program are not properly indented.

An invalid value is used – can occur if giving letters to int().

The program tries to use a variable that does not exist.

An operation uses incorrect types – can occur if adding an integer to a string.

19
Q

Define Logic errors

Bug

A

Some errors may be subtle enough to silently misbehave, instead of causing a runtime error and a crash. An example might be if a programmer accidentally typed “2 * 4” rather than “2 * 40” – the program would load correctly, but would not behave as intended. Such an error is known as a logic error, because the program is logically flawed. A logic error is often called a bug.