Chapter 2 Flashcards

1
Q

What are the 5 phases of the program development cycle?

A
  1. Design the Program: Creating a design of the program before writing any code
  2. Write the Code: writing the code in a high-level language
  3. Correct Syntax Errors: compiler or interpreter will display error message to be fixed
  4. 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)
  5. Correct Logic Errors: Programmer debugs the logic errors. If program must be redesigned, the process starts over again
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What two steps summarize the process of designing a program?

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

What is pseudocode?

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

What is a flowchart?

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

Computer programs typically perform the three-step process:

A
  1. Input is received. (any data that the program receives while it is running)
  2. Some process is performed on the input. (ex mathematical calculation)
  3. Output is produced. (this is the result of the program that is outputted)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a function? What are some function examples in python?

A
  • A piece of prewritten code that performs an operation.

- Ex: print(‘hello world’) function displays the output onto the screen

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

What does calling a function mean?

A

When programmers execute a function, they refer to it as calling a function

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

What is an argument?

A

Data that you want to be displayed on the screen (items in the parenthesis)

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

What is a string?

A

A sequence of characters that is used as data in a program
-Ex: ‘Name’
‘address’
‘profession’

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

What is a string literal?

A

When a string appears in the actual code of a program

-String literals are contained in single quotation marks

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

How do you use apostraphes or quotes with the print function?

A
  • Use double quotation marks

- Ex: print(“I’m Jake”)

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

How do you display “ in the display?

A

-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”””)

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

What are comments? How is it displayed in Python?

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

What is an end-line comment?

A

-A comment that appears at the end of a line of code
-Ex:
1 print(‘Kate Austen’) # Display the name.

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

What is a variable?

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

What is an input function?

A

-Reads input that the user inputs from the keyboard
-Ex: variable=input(prompt)
name = input(‘What is your name?’)

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

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?

A

print(‘Hello’, first_name, last_name)

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

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?

A

-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? ‘))

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

What is an exception?

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

What are math operators and give some examples of them in Python?

A

Tools for performing mathematical calculations

-Ex: Addition +, Subtraction -, Multiplication *, Division /, Exponents **, Integer division //

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

What is an operand?

A

Values to the left and the right of operators

-Ex: 12+2 (12 and 2 are operands)

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

How to enter a percent value in Python?

A

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

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

Give an example of the use of / operator vs // operator:

A

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

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

What does % do?

A

Remainder operator: Instead of returning the quotient, it returns the remainder
- leftover = 17 % 3=2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
  1. When an operation is performed on two ___ values, the result will be an int.
  2. When an operation is performed on two _____ values, the result will be a float.
  3. 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.)
A
  1. int
  2. float
  3. int, float
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Explain how an int value gets converted to a float with this expressoin:
my_number = 5 * 2.0

A

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.

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

fvalue = −2.9
ivalue = int(fvalue)

What is the value of ivalue?

A

-2

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

ivalue = 2
fvalue = float(ivalue)

What is the value of fvalue?

A

2.0

29
Q

What is the line continuation character and how is it used?

A
  • Allows you to break a statement into multiple lines
    -Use a backslash () to do this
    Ex:
    result = var1 * 2 + var2 * 3 + \
    var3 * 4 + var4 * 5
30
Q

What is a concantenation?

A

To append one string to the end of another string.
-In Python, this is done with + operator
Ex:
&raquo_space;> message = ‘Hello ‘ + ‘world’ [Enter]
&raquo_space;> print(message) [Enter]
Hello world
&raquo_space;>

31
Q

> > > my_str = ‘one’ ‘two’ ‘three’
print(my_str)
-What will this code print?

A

onetwothree

32
Q

What does argument end=’ ‘ do?

A

Used for when you do not want the print function to start a new line of output when it finishes displaying its output
p

33
Q

print(‘One’, end=’ ‘)
​print(‘Two’, end=’ ‘)
​print(‘Three’)
What does this code do?

A

OneTwoThree

34
Q

What does sep=’ ‘ do?

A
Eliminates space between items
Ex:
 >>> print('One', 'Two', 'Three', sep='') [Enter]
 OneTwoThree
 >>>
35
Q

> > > print(‘One’, ‘Two’, ‘Three’, sep=’*’) [Enter]What does this output?

A

OneTwoThree

36
Q

What is an escape character?

A
-A special character that is preceded with a backslash (\), appearing inside a string literal
Ex: print('One\nTwo\nThree')
Result: 
One
Two
Three
37
Q
What do the following do?
/n
/t
/'
/"
//
A
/n: Output advanced to next line
/t: Output skips over to next horizontal tab position
/': Single quote mark printed
/": double quote mark printed
//: backslash character printed

Note: / and // can be used as quotation marks when using print function
/ can be used to print a backslash

38
Q

What is a formatted string literal-

A

-An f-string is a string literal that is enclosed in quotation marks and prefixed with the letter f.
-Ex:
»> print(f’Hello world’) [Enter]
Hello world
-Can be used to contain placeholders for variables and other expressions
-Ex:
»> name = ‘Johnny’ [Enter]
&raquo_space;> print(f’Hello {name}.’) [Enter]
Hello Johnny.

-Note: You can concatenate multiple f strings together with +

39
Q

What is the output of the following program:

|&raquo_space;> print(f’The value is {10 + 2}.’) [Enter]

A

The value is 12.

40
Q

What is a format specifier?

A

-Can be included inside of a placeholder in an f-string to format the placeholder.
-Ex: Rounding a value to a specific decimal places
-Written as follows:
{placeholder:format-specifier}

41
Q

Provide an example of a format specifier:

A

{monthly_payment:.2f}
-The .2f is a precision designator that indicates the number should be rounded to two decimal places. The letter f is a type designator, and it indicates that the value should be displayed with a fixed number of digits after the decimal point.
-

42
Q
What do the following format specifiers do?
\:2f
\:,
\:%
\:e
\:d
\:10 (or any number)
\:<
\:>
\:^
A

:2f Rounds to the nearest amount of decimal places
:, Adds commas to numbers such as 1,300,200
:% Formats floating point number as percentage
:e Displays number in scientific notation
:d indicates value should be displayed as a decimal integer
:10 displays minimum number of spaces to display the value
:< Left-aligns the value
:> Right-aligns the value
:^ Center-aligns the value

43
Q

What order should you write format specifiers in?

A

[alignment][width][,][.precision][type]

print(f’{number:^10,.2f}’)

44
Q

What is a magic number?

A

An unexplained value that appears in the program’s code

45
Q

What is a named constant?

A

-a name that represents a special value
Ex: INTEREST_RATE = 0.069
-Makes program easier to understand and makes modifying program easier

46
Q

What is the first step to using Python’s turtle?

A

import turtle

47
Q

What does this do?
»> import turtle
&raquo_space;> turtle.showturtle()

A

Imports turtle and makes it visible

48
Q
What does this do?
 >>> import turtle
 >>> turtle.forward(200)
 >>> turtle.right(90)
 >>> turtle.forward(200)
 >>>
A
  • Moves turtle forward 200 pixels
  • Turns turtle right 90 degrees
  • Moves turtle forward 200 pixels
49
Q

What does this do?

turtle.setheading(angle)

A

Sets the turtle’s heading to a specific angle

50
Q

What does this do?

turtle.heading()

A

Displays turtle’s current heading

51
Q

What does the turtle.penup() and turtle.pendown() commands do?

A

Raises and lowers turtle pen so you can move him without drawing a line

52
Q

What does this do?

turtle.circle(radius)

A

Draws a circle of a certain radius

53
Q

turtle.dot()

A

Turtle draws a simple dot

54
Q

turtle.pensize(width)

A

Changes the turtle’s pensize

55
Q

turtle.pencolor(color)

A

Changes turtle’s pen colors

56
Q

turtle.reset()

A

all drawings that currently appear in the graphics window, resets the drawing color to black, and resets the turtle to its original position in the center of the screen.

57
Q

turtle.clear()

A

erases all drawings that currently appear in the graphics window. It does not change the turtle’s position, the drawing color, or the graphics window’s background color.

58
Q

turtle.clearscreen()

A

erases all drawings that currently appear in the graphics window, resets the drawing color to black, reset the graphics window’s background color to white, and resets the turtle to its original position in the center of the graphics window.

59
Q

turtle.setup(width, height)

A

Specifies size of graphics window

60
Q

turtle.goto(x, y)

A

Makes turtle go to certain point on coordinate pane, almost like a unit circle plane.

61
Q

turtle.pos()

A

Displays turtle’s current position

62
Q

turtle.speed(speed)

A

Sets turtles speed from range of 0-10

63
Q

turtle. hideturtle()

turtle. showturtle()

A

Hides or shows turtle graphic but still draws what you prompted it to

64
Q

turtle.write(text)

A

Displays text in graphics window

65
Q

turtle. begin_fill()

turtle. end_fill()

A
  • Turtle fills a shape with a color
  • When turtle end fill is executed, the shape fills with the color
  • Note: If you fill a shape that is not enclosed, the shape will be filled as if you had drawn a line connecting the starting point with the ending point.
66
Q

turtle.numinput

A
  • get numeric input from the user and assign it to a variable
  • displays a small graphical window known as a dialog box. The dialog box provides an area for the user to type input, as well as an OK button and a Cancel button.
  • variable = turtle.numinput(title, prompt)
  • Title is whatever the user will see (such as “enter a radius”, then prompt is the value the user enters and is stored as a variable
67
Q

variable = turtle.numinput(title, prompt, default=x, minval=y, maxval=z)

-Give an example of how this might be used

A

num = turtle.numinput(‘Input Needed’, ‘Enter a value in the range 1-10’, default=5, minval=1, maxval=10)

68
Q

turtle.textinput

A

-Gets string input from a user

variable = turtle.textinput(title, prompt)

69
Q

turtle.done()

A

If graphics window disappears after turtle is done drawing, add this command to ensure it doesn’t disappear
-If using IDLE, this is not necessary