Chapter 2 Flashcards
What are the 5 phases of the program development cycle?
- Design the Program: Creating a design of the program before writing any code
- Write the Code: writing the code in a high-level language
- Correct Syntax Errors: compiler or interpreter will display error message to be fixed
- Test the Program: Once code is in executable form, it is tested for logic errors (mistake that compiles but produces incorrect results (ex mathematics are off)
- Correct Logic Errors: Programmer debugs the logic errors. If program must be redesigned, the process starts over again
What two steps summarize the process of designing a program?
- Understand the task that the program is to perform
- It is important to interview or discuss with the customer what it is that the program is supposed to do.
- Programmer creates a list of software requirements (tasks the program must perform) based on this gathered info - Determine the steps that must be taken to perform the task
- Programmer breaks down the task into an algorithm (list of logical steps that must be completed in order)
- Programmers use pseudocode and flowcharts to help organize these algorithms/steps
What is pseudocode?
- An informal language that has no syntax rules and is not meant to be compiled or executed.
- Pseudocode is used to create models, or “mock-ups,” of programs.
- Allows programmers to not have to worry about syntax errors during mockup
What is a flowchart?
- A diagram that graphically depicts the steps that take place in a program.
- There are three different symbols:
- Ovals: top and bottom of flowchart (start/end)
- Parallelograms: Input and output symbols
- Rectangles: processing symbols, like mathematical calculations
Computer programs typically perform the three-step process:
- Input is received. (any data that the program receives while it is running)
- Some process is performed on the input. (ex mathematical calculation)
- Output is produced. (this is the result of the program that is outputted)
What is a function? What are some function examples in python?
- A piece of prewritten code that performs an operation.
- Ex: print(‘hello world’) function displays the output onto the screen
What does calling a function mean?
When programmers execute a function, they refer to it as calling a function
What is an argument?
Data that you want to be displayed on the screen (items in the parenthesis)
What is a string?
A sequence of characters that is used as data in a program
-Ex: ‘Name’
‘address’
‘profession’
What is a string literal?
When a string appears in the actual code of a program
-String literals are contained in single quotation marks
How do you use apostraphes or quotes with the print function?
- Use double quotation marks
- Ex: print(“I’m Jake”)
How do you display “ in the display?
-Use triple quotations to display “
-Ex: print(“"”I said “Hello”. “””)
-Triple quotes can also be used to surround multiline strings, something for which single and double quotes cannot be used
-Ex:
print(“"”One
Two
Three”””)
What are comments? How is it displayed in Python?
- Comments are short notes placed in different parts of a program, explaining how those parts of the program work
- Comments are displayed with # character
- Ex: # This is a comment
What is an end-line comment?
-A comment that appears at the end of a line of code
-Ex:
1 print(‘Kate Austen’) # Display the name.
What is a variable?
- A name that represents a value in the computer’s memory.
- Use an assignment statement to create a variable and make it reference a piece of data.
- Ex: age=25
- Variable is the name of a variable and expression is a value, or any piece of code that results in a value.
- When passing a variable into a print function, do not use quotation marks
What is an input function?
-Reads input that the user inputs from the keyboard
-Ex: variable=input(prompt)
name = input(‘What is your name?’)
1 # Get the user’s first name.
2 first_name = input(‘Enter your first name: ‘)
3
4 # Get the user’s last name.
5 last_name = input(‘Enter your last name: ‘)
6
7 # Print a greeting to the user.
How would you print the first_name and last_name inputs?
print(‘Hello’, first_name, last_name)
The input function always returns the user’s input as a string, even if the user enters numeric data. What function can you use to convert a string to a numeric type?
-int(item) : You pass an argument to the int() function and it returns the argument’s value converted to an int.
-float(item): You pass an argument to the float() function and it returns the argument’s value converted to a float.
-Best line of code for this:
hours = int(input(‘How many hours did you work? ‘))
What is an exception?
- An unexpected error that occurs while a program is running, causing the program to halt if the error is not properly dealt with.
- Ex; int and float function does not contain a valid numeric value
What are math operators and give some examples of them in Python?
Tools for performing mathematical calculations
-Ex: Addition +, Subtraction -, Multiplication *, Division /, Exponents **, Integer division //
What is an operand?
Values to the left and the right of operators
-Ex: 12+2 (12 and 2 are operands)
How to enter a percent value in Python?
Multiply by the decimal version
-Ex: 20% discount is 0.2 times the original price of the item. Then doing the original price-discount amount=New price of item
Give an example of the use of / operator vs // operator:
5/2=2.5
5//2=2 (whole integer)
-When the result is positive, it is truncated, which means that its fractional part is thrown away.
-When the result is negative, it is rounded away from zero to the nearest integer.
(-5)//2=-3
What does % do?
Remainder operator: Instead of returning the quotient, it returns the remainder
- leftover = 17 % 3=2
- When an operation is performed on two ___ values, the result will be an int.
- When an operation is performed on two _____ values, the result will be a float.
- When an operation is performed on an int and a float, the ____ value will be temporarily converted to a ____ and the result of the operation will be a float. (An expression that uses operands of different data types is called a mixed-type expression.)
- int
- float
- int, float
Explain how an int value gets converted to a float with this expressoin:
my_number = 5 * 2.0
When this statement executes, the value 5 will be converted to a float (5.0) then multiplied by 2.0. The result, 10.0, will be assigned to my_number.
fvalue = −2.9
ivalue = int(fvalue)
What is the value of ivalue?
-2