Chapter 3, part 2 Flashcards

1
Q

Give examples of an if statement in pseudocode

A
  • If student’s grade is greater than or equal to 60, display ‘Passed’
  • If the condition is true, ‘Passed” is displayed. Then, the next pseudocode statement in order is ‘performed’
  • If the condition is false, nothing is displayed, and the next pseudocode statement is ‘performed’
  • Indentation emphasizes that ‘Passed’ is displayed only if the condition is true

Python code requires indentation. Here it emphasizes that ‘Passed’ is displayed only if the condition is true

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

What is suite indentation?

A

Indenting a suite is required; otherwise an IndentationError syntax error occurs
Must indent a total of 4 spaces when writing print after the if statement

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

T/F

If you indent a suite’s statements, you will not get an IndentationError

A

False. All the statements in a suite must have the same indentation. Otherwise an IndentationError occurs

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

What should you be mindful of when using the = sign?

A

Using the equality operator == instead of the assignment symbol = in an assignment statement can lead to subtle problems
== is used when assessing if something is true or false. Can be used in the same manner as is except is compares identities(if something is identical) while == compares if things are equal (doesnt have to be identical but has to have the same components)
= is used to assign a meaning to something or define it

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

What does adding an if… else statement do that an if statement doesn’t?

A
  • Performs different suites, based on whether a condition is true or false
  • Pseudocode:
    If student’s grade is greater than or equal to 60
    …..Display ‘Passed’
    Else
    …..Display ‘Failed’

Ignore the periods and pretend it’s indented

We indent both If and Else suites, and by the same amount

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

What are conditional statements?

A

When an if…else statment assign different values to a variable, based on a condition, as in:
if grade >= 60:
…..result = ‘Passed’
else:
…..resule = ‘Failed’
We can then print or evaluate that variable:
result
..’Passed’

Ignore the periods and pretend they are indented

The parenthesis are not required but they make it clear that the statement assigns the conditional expression’s value to result
Can write statements like this using a concise conditional expression

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

What would it look like on interactive mode? Use same example of:
if grade >= 60:
…..result = ‘Passed’
else:
…..result = ‘Failed’

Not shown but the second lines should have indentations

Ignore the periods and pretend it’s indented

A

‘Passed’ if grade >=60 else ‘Failed’

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

What would it look like to have multiple statements in a suite?

A

In [13]: grade = 49
In [14] if grade >=60:
………………print(‘Passed’)
………….else:
………………print(‘Failed’)
………………print(‘You must take this course again’)
Out[14] Failed

Pretend periods are indentations

In this case, grade is less than 60, so both statements in the else’s suite execute. If you do not indent the second print, then it’s not in the else’s suite. So, that statement always executes, creating strange incorrect output

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

What would it look like if we did not indent the second print on a multiple statement suite?

A

In [13]: grade = 100
In [14] if grade >=60:
………………print(‘Passed’)
………….else:
………………print(‘Failed’)
………….print(‘You must take this course again’)
Out[14] Passed
…………….You must take this course again

Ignore the periods and pretend they are indentations

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

What are if….elif……else statements?

A

They test for many cases and only the action for the first true condition executes
In [13]: grade = 77
In [14] if grade >= 90:
………………print(‘A’)
………….elif grade >= 80:
………………print(‘B’)
………….elif grade >= 70:
………………print(‘C’)
………….elif grade >= 60:
………………print(‘D’)
………….else:
………………print(‘F’)
Out[14] C

else is optional here, without else, if no conditions are true, the program does not execute any of the statement’s suites

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

What is a nonfatal logic error?

A

An incorrectly indented code segment in snippet:
In [13]: grade = 100
In [14] if grade >=60:
………………print(‘Passed’)
………….else:
………………print(‘Failed’)
………….print(‘You must take this course again’)
Out[14] Passed
…………….You must take this course again
The code executes but it produces incorrect results

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

What is a fatal logic error?

A

An exception occurs such as a ZeroDibisionError from an attempt to divide by 0, so Python displays a traceback, then terminates the script. A fatal error in interactive mode terminates only the current snippet. Then IPython waits for your next input.

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

T/F

A fatal logic error causes a script to produce incorrect results, then continue executing

A

False. A fatal logic error causes a script to terminate

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

What is a while statement?

A

Allows you to repeat one or more actions while a condition remains true. Such a statement often is called loop

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

Give an example of a while statement

A

In [1]: product = 3
In [2]: while product <= 50:
…….. product = product * 3
In [3]: product
Out[3]: 81

This loop goes like this:
3 is less than 50, so we do 3 x 3 = 9
9 is less than 50, so 9 x 3 = 27
27 is less than 50, so 27 x 3 = 81 and now the loop stops because Python again tests the condition, which is finally false because product is now 81. The repetition now terminates.

If this while statement were part of a larger script, execution would continue with the next statement in sequence after the while.

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

What is an infinite loop logic error?

A

When a while statement never meets its false condition and the loop remains in reptition mode. The program appears to “hang”

17
Q

T/F

A while statement performs its suite while some conditions remains true

A

True

18
Q

What does the for statement used for?

A

It allows you to repear an action or several actions. It performs its action(s) for each item in a sequence of items.

19
Q

Give an example of a repeating action using a for statement

A
  • Upon entering the for loop, Python assigns the ‘P’ in ‘Programming’ to the target variable between keywords for and in
  • After executing the suite, Python assigns to character the next item in the sequnce (that is the ‘r’ in ‘Programming’), then executes the suite again
  • Continues while there are more items in the sequence
  • Using the target variable in the suite is common but not required
20
Q

What is the difference between using the 2 keywords end and sep?

As we know, this is what happens when you use end

A

When using sep (short for separator) to specify the string that appears between the items that print displays
Here the for statement will result vertically by default unless we add the end keyword that terminates the string onto one line

21
Q

How would you remove this string’s spaces?

A

Use an empty string with no characters between its quotes

22
Q

Define

Iterable

The sequence to the right of the for statement’s in keyword must be an iterable

A

An object from which the for statement can take one item at a time until no more items remain

The for statement uses the iterator “behind the scenes” to get each consecutive item until there are no more to process. Think of it like a bookmark, it always knows where it is in the sequence.

23
Q

What is one of the most common iterables in a for statement?

A

A list, which is a comma-separated collection of items enclosed in square brackets ([ and ])

24
Q

Define

Mutable

A

The order of the items in a list matters and that is list modifiable (mutable)

25
Q

Define

range function

A

A built-in function which creates an iterable object that represents a sequence of consecutive integer calues starting from 0 and countinuing up to but not including, the argument value stated. Ex: where function call range(10)

In [6]: for counter in range(10):
………………print(counter, end=’ ‘)
Out[6]: 0 1 2 3 4 5 6 7 8 9

Creates an iterable object that represents a sequence of consecutive integer values starting from 0 and continuing up to, but not including, the argument value

26
Q

What is an off-by-one error?

A

A logic error that occurs when you assume that range’s argument value is included in the generated sequence

27
Q

Function _________________ generates a sequene of integers

A

range

28
Q

Write a range function ad a for statement to calculate the total of integers from 0 through 1,000,000

A

In[1]: total = 0
In[2]: for number in range (1000001):
……………total = total + number
In[3]: total
Out[3]: 500000500000