Week 1-5 Flashcards

1
Q

What is computer programming?

A

The process of explaining how to solve a problem in a way that a computer can understand. Which can be referred to as an “algorithm”.

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

What is an algorithm?

A

An algorithm is a precise and unambiguous ordered list of instructions that eventually ends with a correct solution.

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

What is the proper Problem Solving Strategy?

A

Define (IPO Chart), Design (Pseudo Code & Flow Chart), Test, Implement (Programming Language), Test.

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

We cannot solve problems we don’t understand. Understanding a problem begins with defining it. How do we define a problem?

A

To define a problem we need to identify 3 parts: the input, the process to follow, and the output.

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

Define the Input stage of an IPO.

A

input: information we know that helps solve the problem.

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

Define the processing stage of an IPO.

A

Processing: the algorithm that takes us from input to output.

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

Define the output stage of an IPO.

A

output: the result we want to solve for.

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

What is the purpose of an IPO chart?

A

An IPO chart helps organize our thinking so we don’t get confused - this stage can be considered brainstorming.

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

Does the IPO chart use detailed information?

A

No, the IPO chart helps define a general solution to a problem, you don’t include the specific date from the problem. That way, you can use the same IPO chart regardless how many times the input values change.

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

What is pseudocode?

A

Once we know what information is available, and what is required as output we can design an algorithm to solve the problem. We do this in a language which is less open to interpretation than english but is also less formal than a programming language. We call this language pseudocode.

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

What are some verbs used in the input stage of pseudocode?

A

Read, Get, Obtain… etc

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

What are some verbs used in the processing stage of pseudocode?

A

Compute, Calculate, Set…etc

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

What are some verbs used in the output stage of pseudocode?

A

Print, Display, Show….etc

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

Why do we build flow charts?

A

It is often helpful to visualize a design with a flow chart.

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

How is a flow chart designed?

A

We use circles to identify the start and end of the algorithm, and rectangles to contain the pseudocode instructions.

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

What are the orders of a design?

A
1 - IPO Chart
2 - Pseudocode & Assumptions
3 - Flow Chart
4 - TRACE or Truth Table
5 - Code & Test
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How can we develop confidence that our design solves the problem?

A

We must test the design carefully: first with any sample values and then with any other possible values.

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

When designing tests we need to be very clear on what we are trying to accomplish. What are the 3 main things we test for?

A
  • test that our code works as expected.
  • test that our code works reasonably when bad values are put in.
  • test that we have explored all the paths through our code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a variable?

A
  • the part of a program that can “remember” values.
  • boxes in memory that can hold 1 thing at a time
    ex. length = 3, word = “hello”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is an expression?

A
  • any piece of code that provides a value.

ex. 3, 3 + 2, x > 10, “Hi”, len( “hi” )

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

What is a statement?

A
  • the smallest complete action of a program.
  • comparable to a sentence.
    ex. print( “hello” )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does an input expression look like?

A

input( “What is your name? “ )

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

What is a literal?

A

A value that is explicitly defined in your source code is called a literal.

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

What is the primary difference between a literal and a variable?

A

The variable may change over the course of the program’s execution. A literal will remain the same.

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

What would a literal statement look like?

A

myNumber = 15

myNumber is a variable and 15 is a literal.

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

What does an assignment statement look like?

A

variableName = expression.

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

what does “=” mean?

A

this means “save to” or “assign to”, which is used to attach an expression to a variable. it does not mean equals to**

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

in python what is this statement saying;

x - 2 = 3

A
  • load the value of x from memory.
  • subtract 2 from that value.
  • save the number 3 into the number 3 into the number you just calculated.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

how to you say equals to in python?

A

==

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

how does python go through the execution process when it runs a program?

A

It is important to note that the program runs one line at a time from start to finish. There are no automatic actions, everything is specified.

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

What are the rules of python variable names?

A
  • must begin with a letter or underscore.
  • may contain numbers.
  • are case sensitive.
  • is not one of Python’s reserved keywords.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is the most common method of giving an output to the screen?

A

The print statement;

print( “hello world” )

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

What are strings?

A

A string is a data type, defined as a sequence of characters contained between a pair of quotation marks. Note that characters can have letters, number or symbols, but they are always read as text.

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

what are the two style types for variable naming convention?

A

my_name

myName

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

What are the 4 Data Types?

A

Boolean Values
Integer Numbers
Real Numbers
Strings

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

What is a boolean data type?

A

boolean - true or false

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

what is an integer data type?

A

int - numbers without decimals

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

what is a real number data type?

A

float - numbers with decimals

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

what is a string data type?

A

str - characters enclosed in quotes

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

Can you mix data types?

A

Absolutely not

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

What does precedence mean?

A

Order of operations

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

What are the arithmetic operations in python from highest ranking to lowest.

A

1: () brackets trump all
2: ** exponents
3: - negation
4: * multiplication
4: / division
4: // integer division
4: %modulo (remainder)
5: + addition
5: - subtraction

** NOTE that there are many on the 4th and 2 on the 5th line of importance.. They have equal importance and are read from left to right **

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

What is the symbol to represent exponents in python?

A

**

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

What is the symbol to represent multiplication in python?

A

*

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

What is the symbol to represent division in python?

A

/

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

What is the symbol to represent integer division in python?

A

//

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

What is the symbol to represent modulo (remainders) in python?

A

%

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

What is the symbol to represent addition in python?

A

+

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

What is the symbol to represent substraction in python?

A

-

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

What are the Relational operations in python.

A
>    Greater than
>=  Greater than or equals
<    Less than
<=  Less than or equals
==  Equal to
!=   Not equal to 
**ALWAYS TRUE OR FALSE**
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

Where do you find the ranking of all python symbols?

A

ASCII Chart

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

is “a > A > 3” true or false?

A

This is true

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

For boolean operators, what does the “and” truth table look like?

A
a     |     b     |     a and b
  true   |   true   |       true 
  true   |  false   |      false
 false   |  true    |      false
 false   |  false   |      false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

for boolean operators, what does the “or” truth table look like?

A
a     |     b     |     a and b
  true   |   true   |       true 
  true   |  false   |       true
 false   |  true    |       true
 false   |  false   |      false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

for boolean operators, what does the “not” truth table look like?

A

a | not
false | true
true | false

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

What is Demorgan’s Law?

A

not (a and b) = not(a) or not (b)

not (a or b) = not(a) and not (b)

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

What is the symbol to represent “greater than” in python?

A

>

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

What is the symbol to represent “greater or equals” in python?

A

> =

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

What is the symbol to represent “less than” in python?

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

What is the symbol to represent “less or equals” in python?

A

<=

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

What is the symbol to represent “equal to” in python?

A

==

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

What is the symbol to represent “not equal to” in python?

A

!=

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

What are the string operations in python.

A
\+       Concatenation
*        Repitition
[n]     Substring (slice)
[m:n] Substring (slice)
in      Membership
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is the symbol to represent “concatenation” and how does it work?

A

+

“hi” + “there” == “hithere”

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

What is the symbol to represent “repetition” and how does it work?

A

*

“hi” * 2 == “hihi”

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

What is the symbol to represent “single substring slice” and how does it work?

A

[n]

“hello” [1] Gives “e”

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

What is the symbol to represent “multi substring slice” and how does it work?

A

[m:n]

“hello” [1:4] Gives “ello”

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

What are the Grouping Priorities in python from highest ranking to lowest.

A
1 - String (slice, concatenation)
2 - Arithmetic and String repetition
3 - Relational &amp; String membership
4 - Logical
5 - Assignment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

Whats the main rule when dealing with exponents in python?

A

ALWAYS USE BRACKETS

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

What are operators?

A

operators are symbols that represent functions, the data they work with are called operands.

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

in the expression 3 + 5, what are the operands and what is the operator?

A

3 and 5 are operands, + is the operator.

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

What is string concatenation?

A

you use the plus sign (+) to join strings together.

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

What do you need to watch out for when dealing with integer division?

A

Caution: integer division can act unexpectedly if one of the operands is negative - it will always round AWAY from 0.
eg. -12//5 is -3

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

What is the precedence (order of operations) for boolean operators?

A

not, and, or… respectively

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

Can you use 0 and 1 to mean true and false?

A

ABSOLUTELY NOT

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

what is the general structure of the IF statement?

A

if boolean-expressions:
code block
(if the boolean expression evaluates to true then the statements in the indented code block should be run.)

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

What does an if-else statement look like?

A
if boolean-expression:
        code block
else:
        code block
(this gives us a basic "this or that" decision making pattern)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

what does an if-elif-else statement look like?

A
if boolean-expression:
      code block
elif boolean-expression:
       code block
else:
       code block
(you may have several elif statements with their own condition. the else statement can be left out if your logic doesn't require it.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

What would the pseudo code look like for an if-elif-else statement?

A
IF number is greater than 0 Then
   SHOW greater than 0 message
ELIF number is equal to 0 THEN
   SHOW equal to 0 message
ELSE
   SHOW less than 0 message
ENDIF
80
Q

What would a nested IF statement look like?

A

if myNumber > 0:
print( “My Number is bigger than 0:)
if myNumber % 2 == 0:
print( “My number is even too!” )
else:
print( “My number is less than or equal to 0” )

81
Q

What do you need to make sure you do when nesting an IF statement?

A

When nesting make sure you indent one extra level.

82
Q

What is the clearer method to use when dealing with IF statements, a Flow Chart, or Pseudo Code?

A

Flow charts are more clear than pseudo code when dealing with IF statements

83
Q

What is the function of the “Circle” in a flow chart, and what are it’s rules?

A

The circle is used to represent the start & finish of the flow chart.
Start: 0 arrows in, 1 arrow out
Finish: 1 or more arrows in, 0 arrows out

84
Q

What is the function of the “Rectangle” in a flow chart, and what are it’s rules?

A

The rectangle is used to represent a code block in the flow chart.
code block: 1 or more arrows in, 1 arrow out

85
Q

What is the function of the “Diamond” in a flow chart, and what are it’s rules?

A

The diamond is used to represent a boolean expression.

boolean expression: 1 or more arrows in, 2 arrows out

86
Q

What is a code block?

A

Code blocks are programming paragraphs, multiple statements put together.

87
Q

What is a bug?

A

a bug is when the program told the computer told to do something it couldn’t do

88
Q

What is the worst type of bug?

A

the worst type of bug is one that doesn’t crash, just give incorrect information. At least when a bug causes a crash you notice that there is a problem.

89
Q

What are the steps in fixing a bug?

A

First you find the bug by tracking down where you gave the wrong information, then you need to figure out what the right instructions are, and then you need to update those instructions without introducing more bugs

90
Q

What are the two reasons a program gets a software update?

A

It is either getting new features added or bugs fixed

91
Q

In python we call () ?

A

Parenthesis

92
Q

In python we call [] ?

A

Brackets

93
Q

In python we call {} ?

A

Braces

94
Q

What is the only program on the computer that’s allowed direct access to the hardware?

A

The operating system

95
Q

What type of program is needed to run your python program on the operating system?

A

A python interpreter

96
Q

What would the expression 53 // 24 display on the next line?

A

2

(53 divided by 24 = 2 with a remainder of 5) the “//” operator only displays the integer.

97
Q

What would the expression 53 % 24 display on the next line?

A

5

(53 divided by 24 = 2 with a remainder of 5) the “%” operator only displays the remainder.

98
Q

Can a floating point number be operands for “//” or “%” ?

A

Yes but the result is rounded down to the nearest whole number.
eg.
»> 3.3 // 1
3.0

99
Q

What is a binary operator?

A
A binary operator is an operator that has two operands.
eg.
>>> -5
-5
>>> --5
5
>>> ---5
-5
100
Q

What is an operator called that can be applied to more than one type of value?

A

An overloaded operator

101
Q

Is the computer good at dealing with decimal points?

A

No, absolutely not. It’s terrible and can cause many many problems in your code. Make sure you test and test and test to make sure it works as it should.

102
Q

Explain whats happening.

>>> difference = 20
>>> double = 2 * difference
>>>double
40
>>> difference = 5
>>> double
40
A

This code demonstrates that assigning to a variable does not change any other variable.

103
Q
What would d be:
>>> d = 2
>>> d *= 3 + 4
>>> d
???
A

the answer is 14.
(the right hand side does its process then the answer is multiplied by d because of the “*=” augmented assignment operator.)

104
Q

are these two examples equivalent?

>>> number = 10
>>> number *= number
-------------------------------
>>> number = 10
>>> number = number * number
A

Yes they are, both would give an answer of 100, because the “*=” is used to replace the first repetition of the LHS variable that occurs on the right hand side.

105
Q

What are the Augmented Assignment Operators in python?

A
\+=   :   ( " x = x + 2 " equals " x += 2 " )
-=   :   ( " x = x - 2 " equals " x -= 2 " )
*=   :   ( " x = x * 2 " equals " x *= 2 " )
/=   :   ( " x = x / 2 " equals " x /= 2 " )
//=   :   ( " x = x // 2 " equals " x //= 2 " )
%=   :   ( " x = x % 2 " equals " x %= 2 " )
**=   :   ( " x = x ** 2 " equals " x **= 2 " )
106
Q

What are the two types of errors in python?

A

Syntax & Semantic

107
Q

What is a Syntax Error?

A

Syntax errors happen when you type something that isn’t valid Python code.

108
Q

What is a Semantic Error?

A

Semantic errors happen when you tell Python to do something that it just can’t do, like try to divide a number by zero or try to use a variable that doesn’t exist.

109
Q

The recommended Python style is to limit lines to ____ characters?

A

80

110
Q

What are the two ways to properly split up a statement into more than one line?

A

1: Make sure your line break occurs inside parentheses.
2: Use the line-continuation character, which is a backslash, .
* * note that the backslash “" is the line-continuation character, not the forward slash “/” which is the division symbol. **

111
Q

What is the symbol used to add a comment?

A

the hashtag #

112
Q

does the comment symbol need to be the first character on the line?

A

no, it can come after your statement.
eg.
»> (212 - 32) * 5 / 9 # Convert 212 degrees….
100.00

113
Q

What is the “abs” function in python?

A

the abs function produces the absolute value of a number.
eg.
»> abs( -9 )
9

114
Q

what is an argument?

A

An argument is an expression that appears between the parentheses of a function call.
eg. in “abs( -9 )”, the argument is “-9”

115
Q

what does the “round” function do in python?

A

the round function rounds a decimal or float number to a whole number or real number.
eg.
»> round(4.3)
4

116
Q

what does the “pow” function do in python?

A
the pow function takes two numbers, and turns it into "first number" to the power of "second number".
eg.
>>> pow(2,4)
16
*2 to the power of 4 is 16*
117
Q

what does the “int” function do in python?

A

the int function converts whatever the argument is, into an integer, truncating any decimal.
eg.
»> int(-4.3)
-4

118
Q

what is the “float” function do in python?

A

the float function converts the argument into a decimal number.
eg.
»> float(21)
21.0

119
Q

how do you find out what a certain function does?

A

use the built in function help.
eg.
»> help(abs)
Help on built-in function abs in module builtins:

abs(…)
abs(number) -> number

Return the absolute value of the argument.

120
Q

how do you find the exact memory address of an object?

A
you use the id function.
eg.
>>> shoeSize = 8.5
>>> id( shoeSize )
42985674
121
Q

what is aliasing?

A
aliasing is when two variable refer to the exact same object.
eg.
>>> i = 3
>>> j = 3
>>> k = 4 - 1
>>> id( i )
4299456
>>> id( j )
4299456
>>> id( k )
4299456
122
Q

How do you create a new function in python?

A

you use def to define, & return for the formula.
eg.
»> def convertToCelsius(fahrenheit):
return (fahrenheit - 32) * 5 / 9

> > > convertToCelsius(80)
26.66666666666

123
Q

can you call upon a local variable that is created within a function?

A

No, Local variables get created each time that fuction is called, and erased when the function returns. Because they only exist when the function is being executed, they can’t be used outside of the function. Trying to do so causes an error.

124
Q

What does creating a multi statement function look like?

A
>>> def quadratic( a, b, c, x ):
           first = a * x ** 2
           second = b * x
           third = c
           return first + second + third

> > > quadratic( 2, 3, 4, 0.5 )
6.0

125
Q

What is the “variables scope”?

A

The are of a program that a variable can be used in is called the variable scope. The scope of a local variable is from the line in which it is defined up until the end of the function.

126
Q

How many arguments do you need in order for a function to work?

A

The function must have the same amount of arguments as the parameters given when it was defined.
eg.
»> quadratic( 1, 2, 3 )
Traceback (most recent call last):
File “”, line 1, in
TypeError: quadratic() takes exactly 4 arguments (3 given)

127
Q

What does EOL stand for?

A

End of line

128
Q

What is the shortest string you can have?

A

an empty string

129
Q

what does the “len” string operation do?

A

it returns the number of characters between the opening and closing quotes.

130
Q

how do you use a double quote inside of a string without python thinking they are stopping and then starting another string?

A

use single quotes for the full string and double quotes inside. or use the escape sequence "

131
Q

will using two single quotes inside of a string end the string and start a new one?

A

not if the string is using double quotes to open and close it. or use the escape sequence '

132
Q

What are the escape sequences python can use to represent special symbols?

A
\' : single quote
\' : double quote
\\ : backslash
\t : tab
\n : newline
\r : carriage return
133
Q

How do you span multiple lines in python?

A
To span multiple lines you put ''' or """ (3 single or double quotes) around the string instead.
eg.
>>> '''one
two
three'''
'one\ntwo\nthree'
134
Q

Define a WHILE loop.

A

A WHILE loop runs as long as the condition is true.

135
Q

Why is the WHILE loop important?

A

The WHILE loop is a fundamental structure; we define other types of loops by comparing them to the WHILE loop.

136
Q

What are the 4 parts of a WHILE loop?

A

The parts of the while loop are the initial state, the loop condition, the loop body and the post state.

137
Q

What does the PSEUDOCODE look like for a loop?

A
GET a number from the user
SET loop counter to 1
SET the message to blank
LOOP
         IF loop counter <= input number THEN
              CONCATENATE message and loop counter
              INCREMENT loop counter
         OTHERWISE
              EXIT LOOP
         END IF
END LOOP
SHOW message to user
138
Q

What are the most common errors in designing a loop?

A

The most common error is that it does not run the number of times you want it to. It will run too many times, not enough, never, or forever.
- 1 time too many or too few is often from using <= instead of instead of >=, or vice versa.
- Never running is often from an incorrect initial condition.
Infinite running is often from forgetting to guarantee that the loop condition eventually becomes false.
* Always check before coding*

139
Q

As programs become more sophisticated, loop conditions can also become complex. Conditions may need to be checked in several parts of the code to determine which logic to execute, but repeating the code creates challenges to code maintenance. What are the two techniques used to manage this problem?

A

Two techniques we can use to manage this problem are SENTINEL VALUES and BOOLEAN FLAGS.

140
Q

What are Sentinel Values?

A

Sentinel Values are values which cause the loop to exit and are often hinted at to the user in an input prompt.
(e.g) choice = “”
while choice != “Q” :
choice = input( “Enter a value (or Q to quit): “ )

141
Q

What are Boolean Flags?

A

Boolean flags are variables which hold the result of a more complicated expression and should be used to clarify code or to reduce calculations.
(e.g)
decision = True
while decision:
number = int( input( “A number? “ ) )
decision = number * 3 < 20 and number % 5 == 0
if decision:
print( “Go Again!” )
Another useful example is when you want to write “while not decision” but don’t want to change your formula.

142
Q

What is a FOR loop?

A

The FOR loop’s way of operating with strings is to say “for each character in the string do something with it.”

143
Q

What are the 4 parts to every loop?

A

The parts of the while loop are the initial state, the loop condition, the loop body and the post state.

144
Q

What does writing pseudocode for a loop involve?

A

It involves defining the different parts of the loop;

initial state, loop condition, loop body and post state.

145
Q

What does writing pseudocode for a loop look like?

A
GET a number from the user
SET loop counter to 1
SET the message to blank
LOOP
    IF loop counter <= input number THEN
           CONCATENATE message and loop counter
           INCREMENT loop counter
    OTHERWISE
           EXIT LOOP
    END IF
END LOOP
SHOW message to user
146
Q

What is the symbol for NOT equal to?

A

!=

147
Q

What is the FOR loop especially useful for?

A

The FOR loop is especially useful for working with strings and lists.

148
Q

What does the general form of a FOR loop look like?

A

The general form is:

for variable in StringOrList:
code-block

149
Q

Is it possible to modify the original variable in a FOR loop?

A

Although it is possible to modify the original variable the for loop is generally considered “read only”.

150
Q

Give an example of a FOR loop

A

Count how many times the letter e appears in a string.

myString = "This is my very nice string."
eCount = 0
for character in myString:
    if ( character == "e" ) :
        eCount = eCount + 1
print( "There are" + str( eCount ) + \
          "letter e's in your string." )
151
Q

can you style the output of a print function in python

?

A

Function “print” does not allow any styling of the output: no colors, no italics, no boldface. All output is plain text.

152
Q

What would an example of using some escape sequences look like?

A

> > > print( “one\ttwo\nthree\tfour” )
one two
three four
the \t symbol indicates the tab character and is used to lay values out in columns. The \n symbol indicates a new line in multiline string.

153
Q

What is used to separate values in order to print a list?

A

a comma. (e.g.)
»>print( 1, 2, 3 )
1 2 3

154
Q

What happens when the print function is called with no arguments?

A

The print ends the current line, advancing to the next one. (e.g)
»>print()

> > >

155
Q

Can the print function print values of different types in the same function call?

A

Yes. (e.g)
»>print( 1, “two”, “three”, 4.0 )
1 two three 4.0

156
Q

What does the separator parameter work and what does it look like when used?

A

The separator parameter lets you choose how you want lists to be separated, it is set to a space by default. (e.g)
»> print( “a”, “b”, “c” ) #separator is a space by default.
a b c
»> print( “a”, “b”, “c”, sep=”, “ )
a, b, c

157
Q

How do you stop the program or shell from starting a new line every time?

A

You use the keyword argument end=”” to tell python to end with an empty string instead of a new line. (e.g)
»> print(“a”, “b”, “c”, sep= “, “, end=”” )
a, b, c»>

158
Q

What expression type does the input function automatically use when getting an input from the user?

A

the input function returns whatever the user enters as a string, even if it looks like a number.

159
Q

What are control flow statements?

A

These statements involve a python type that is used to represent truth and falsehood to control the way the computer executes programs.

160
Q

How many value and operators do control flow statements have?

A

this type has only two values and three operators.

161
Q

the type “bool” has how many possible values?

A

2, true or false

162
Q

What are the three basic Boolean operators?

A

AND, OR, and NOT. NOT has the highest precedence, followed by AND and then OR

163
Q

How many values can the NOT operator be applied to at once?

A

NOT is a unary operator: it is applied to just one value, like the negation in the expression -(3 + 2).

164
Q

What does a NOT operator look like when used?

A

> > > not True
false
not false
True

165
Q

How does the AND operator work?

A
AND is a binary operator; the expression left and right produces True if both left and right are True, and it produces False otherwise.
>>> True and True
True
>>> False and False
False
>>> True and False
False
>>> False and False
False
166
Q

How does the OR operator work?

A
OR is also a binary operator. It produces True if either operand is True, and it produces False only if both are False.
>>> True or True
True
>>> False or False
False
>>> True or False
True
>>> False or True
167
Q

What does “IFF” stand for and when is it used?

A

IFF stands for “if and only if” or “exactly when” the conditions are met will True be returned. It is implied that when those conditions aren’t met the function will return False.

168
Q

What are the three rules for combining arithmetic, boolean and relational operators?

A
  • Arithmetic operators have higher precedence than relational operators. “+ and / are evaluated before < or >”
  • Relational operators have higher precedence than Boolean. “< or > are evaluated before AND ,OR and NOT”
  • All relational operators have the same precedence.
169
Q

Do you use brackets when combining arithmetic, boolean and relational operators?

A

You don’t have to, but it’s usually a good idea to put the parentheses in since it helps the eye find the subexpressions and clearly communicates the order to anyone reading your code.

170
Q

How do you check wether a value lies in a certain range?

A

You combine the comparisons with AND. (e.g)
»> x = 3
»> ( 1 < x ) and ( x <= 5 )
True
** BUT this happens so often that python lets you chain the comparison (e.g)
»> x = 3
»> 1 < x <= 5
True
**
only chain comparisons when dealing with similar operators not < and != ***

171
Q

How does python treat numbers when dealing with Boolean operators?

A

Python treats 0 and 0.0 as false and all other numbers as true.

172
Q

How does python treat strings when dealing with Boolean operators?

A

Python treats an empty string as False and all other strings as true.

173
Q

What is a short circuit evaluation?

A

When Python evaluates an expression containing AND or OR, it does so from left to right. As soon as it knows enough to stop evaluating, it stops, even if some operands haven’t been looked at yet. This is called short-circuit evaluation.

174
Q

What happens in an OR expression if the first operand is True?

A

In an OR expression, if the first operand is True, we know that the expression is True. Python knows this as well, so it doesn’t even evaluate the second operand.

175
Q

What happens in an AND expression if the first operand is False?

A

In an AND expression, if the first operand is False, we know that the expression is False. Python knows this as well, so it doesn’t even evaluate the second operand.

176
Q

Is it possible to compare strings?

A

It’s possible to compare strings just as you would compare numbers. The characters in strings are represented by integers: a capital A, for example is represented by 65, while a space is 32, and a lowercase z is 172. So “_” < “A” < “z”

177
Q

What is the operator that checks wether one string appears inside another one? give an example.

A
The " in" operator. (e.g)
>>> "Jan" in "01 Jan 1838"
True
>>> "Feb" in "01 Jan 1838"
False
*The "in" operator produces True exactly when the first string appears in the second string, this is case sensitive.*
178
Q

What is the standard indentation of python?

A

4 spaces

179
Q

Does indentation matter in if statements?

A

Yes, it will not run properly, and python will let us know with an error message.

180
Q

What is the difference between multiple IF statements and an IF statement with an ELIF?

A

The difference between the two is that ELIF is checked only when the IF condition above it evaluated to False, where the every if statement back to back would get checked even though only one can be true.

181
Q

What would be an example of when to use two if statements instead of if/elif?

A

If the body of the first IF changes the value of a variable used in the second condition. (e.g)
»> if ph < 7.0:
ph = 8.0

> > > if ph > 7.0:
print( ph, “is acidic.” )

  1. 0 is acidic.
    * now if that was an if/elif expression then the second condition would have been skipped even though the variable change from the first statement would have made the second true.*
182
Q

As a rule of thumb, when should you use if/elif?

A
If two conditions are related, by using multiple elif clauses. (e.g)
>>> compound = input( "Enter the compound: " )
Enter the compound: CH4
>>> if compound == "H20":
            print( "Water" )
       elif compound == "NH3":
            print( "Ammonia" )
        elif compound == "CH4":
            print( "Methane" )

Methane
»>

183
Q

When is an ELSE clause used?

A

If none of the conditions in a chain of if/elif statements are satisfied and we want our program to print something we use an ELSE clause to print “this” if no conditions are met.

184
Q

What type of python statement can go into an IF statement’s block?

A

Any type of python statement even other IF statements.

185
Q

What is it called when you put an IF statement inside of another IF statement?

A

A Nested IF statement.

186
Q

What value is stored in x?

|&raquo_space;> x = 15 > 5

A

x = True

187
Q

Give an example of what nested if’s look like vs non nested.

A
Nested:
young = age < 45
slim = bmi , 22.0
if young:
     if slim:
         risk = "low:
      else:
         risk = "medium"
else:
     if slim:
          risk = "medium"
     else: risk = "high"
Not Nested:
young = age < 45
slim = bmi < 22.0
if young and slim:
     risk = "low"
elif young and not slim:
     risk = "medium"
elif not young and slim:
     risk = "medium"
elif not young and not slim:
     risk = "high"
188
Q

When are FOR loops only useful?

A

FOR loops are useful only if you know how many iterations of the loop you need. If you do not know how many loop iterations to execute then you will use a while loop.

189
Q

What are the main differences between a WHILE loop and a FOR loop?

A

the while loop is “keep looping while the expression evaluates true, only stop when the expression is false” making the length vary depending of many different things, while the for loop is “Loop for X amount of times then end loop”. Use the FOR loop when you know how many loop intervals you need and WHILE loops when you do not.

190
Q

How do you make an interactive loop?

A
You use the input function in a loop to make it interactive.
(e.g) text = ""
while text != "quit":
       text = input ( "Enter a word (or 'quit' to exit):  " )
       if text == "quit":
           print( "...now exiting" )
       elif text == "H20":
           print( "water" )
       elif text == "NH3":
           print( "Ammonia" )
       else:
           print( "unknown" )

> > > Enter a word (or ‘quit’ to exit): NH3
Ammonia
Enter a word (or ‘quit’ to exit): H20
Water
Enter a word (or ‘quit’ to exit): quit
…now exiting

191
Q

Do FOR and WHILE loops execute all the statements in their body on each iteration?

A

FOR and WHILE loops execute all the statements in their body on each iteration, however sometimes it is handy to be able to break that rule. Python provides two ways of controlling the iteration of a loop: Break, & Continue.

192
Q

What is a break, and how is it used?

A
A Break terminated the execution of a loop immediately.
(e.g) text = ""
while text != "quit":
       text = input ( "Enter a word (or 'quit' to exit):  " )
       if text == "quit":
           print( "...now exiting" )
           break
       elif text == "H20":
           print( "water" )
       elif text == "NH3":
           print( "Ammonia" )
       else:
           print( "unknown" )

** only use if letting the loop run its course would cause an issue and terminating the loop at that exact time makes the most sense. **

193
Q

Will a break terminate multiple loops if nested?

A

No, a break only terminates the innermost loop in which its contained, this means that in a nested loop, a break statement inside the inner loop will terminate only the inner loop not both loops.

194
Q

What is a continue statement?

A

A continue statement causes Python to skip immediately ahead to the next iteration of a loop. All statements in the loop body that appear after it are skipped.

195
Q

what does int/int return?

A

a float

196
Q

what does int//int return?

A

an int truncated