Chapter 3 Flashcards

1
Q

What is the control structure?

A

a logical design that controls the order in which a set of statements execute.

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

What is a sequence structure?

A
a set of statements that execute in the order in which they appear
Ex: 
name = input('What is your name? ')
 age = int(input('What is your age? '))
 print('Here is the data you entered:')
 print('Name:', name)
 print('Age:', age)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a decision structure?

A

a specific action is performed only if a certain condition exists.
-If the condition does not exist, the action is not performed

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

What is a single alternative decision structure?

A

There is only one alternative path of execution if the one condition is true

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

How is an alternative decision structure written out in Python?

A

Using an if statement:

if condition:
  statement
  statement
  etc.

-Note that the indentation is required

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

What is a boolean expression?

A

Expression taht is tested by the if statement to determine true/false

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

What are the different relational operators used with if statements in Python?

A
>	Greater than
<	Less than
>=	Greater than or equal to
<=	Less than or equal to
==	Equal to
!=	Not equal to
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If you type a Boolean expression at the _____ prompt, the interpreter will evaluate the expression and display its value as either True or False.

A

> > >

Ex: 
1  >>> x = 1 [Enter]
 2  >>> y = 0 [Enter]
 3  >>> y < x [Enter]
 4  True
 5  >>> x < y [Enter]
 6  False
 7  >>>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

if sales > 50000:
  bonus = 500.0
What does the above statement mean?

A

If the expression sales > 50000 is true, the variable bonus is assigned 500.0. If the expression is false, however, the assignment statement is skipped.

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

What is a dual alternative decision structure and what line of code is used for it?

A

Two possible paths of execution.

  • one path is taken if a condition is true, and the other path is taken if the condition is false.
  • Use an if-else statement to represent this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
if condition:
     statement
     statement
     etc.
 else:
     statement
     statement
     etc.

Explain what this means:

A

If condition is true, the block of indented code executes and then the code jumps to the next statement following the if-else.
-If false, the block of indented quotes below else executes and the code jumps to the next statement following the if-else.

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

if temperature < 40:
  print(“A little cold, isn’t it?”)
 else:
  print(“Nice weather we’re having.”)

Explain what this does?

A

If temperature is less than 40, it will print the indented line below. Otherwise, it will print the indented line below else

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

How does indentation work with if-else statement

A
  • Make sure the if clause and the else clause are aligned.
  • The if clause and the else clause are each followed by a block of statements. Make sure the statements in the blocks are consistently indented.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does ASCII work with if and if-else statement comparisons?

A

The strings are translated into their corresponding ASCII values.
Ex: “M” is the number 77 in ASCII.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
How is the following compared in ASCII?
name1 = 'Mary'
 name2 = 'Mark'
 if name1 > name2:
     print('Mary is greater than Mark')
 else:
     print('Mary is not greater than Mark')
A

The ‘M’ in ‘Mary’ is compared with the ‘M’ in ‘Mark’. Since these are the same, the next characters are compared.

The ‘a’ in ‘Mary’ is compared with the ‘a’ in ‘Mark’. Since these are the same, the next characters are compared.

The ‘r’ in ‘Mary’ is compared with the ‘r’ in ‘Mark’. Since these are the same, the next characters are compared.

The ‘y’ in ‘Mary’ is compared with the ‘k’ in ‘Mark’. Since these are not the same, the two strings are not equal. The character ‘y’ has a higher ASCII code (121) than ‘k’ (107), so it is determined that the string ‘Mary’ is greater than the string ‘Mark’.

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

What would the following program output?

1 # This program compares strings with the < operator.
  2 # Get two names from the user.
  3 name1 = input(‘Enter a name (last name first): ‘)
  4 name2 = input(‘Enter another name (last name first): ‘)
  5
  6 # Display the names in alphabetical order.
  7 print(‘Here are the names, listed alphabetically.’)
  8
  9 if name1 < name2:
 10 print(name1)
 11 print(name2)
 12 else:
 13 print(name2)
 14 print(name1)

A

Enter a name (last name first): Jones, Richard [Enter]
 Enter another name (last name first) Costa, Joan [Enter]
 Here are the names, listed alphabetically:
 Costa, Joan
 Jones, Richard

17
Q

What is the if-elif-else statement?

A

Makes nested decision structures easier to write

You can still use if-else statements, but the indentation can be confusing.

Ex:
When the statement executes, condition_1 is tested. If condition_1 is true, the block of statements that immediately follow is executed, up to the elif clause. The rest of the structure is ignored. If condition_1 is false, however, the program jumps to the very next elif clause and tests condition_2. If it is true, the block of statements that immediately follow is executed, up to the next elif clause. The rest of the structure is then ignored. This process continues until a condition is found to be true, or no more elif clauses are left. If no condition is true, the block of statements following the else clause is executed.

18
Q

What is a logical operator? Describe the 3 different logical operators

A

Used to create complex Boolean expressions:

and: The and operator connects two Boolean expressions into one compound expression. Both subexpressions must be true for the compound expression to be true.
or: The or operator connects two Boolean expressions into one compound expression. One or both subexpressions must be true for the compound expression to be true. It is only necessary for one of the subexpressions to be true, and it does not matter which.

not The not operator is a unary operator, meaning it works with only one operand. The operand must be a Boolean expression. The not operator reverses the truth of its operand. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.

19
Q

1 # This program determines whether a bank customer
  2 # qualifies for a loan.
  3
  4 MIN_SALARY = 30000.0 # The minimum annual salary
  5 MIN_YEARS = 2   # The minimum years on the job
  6
  7 # Get the customer’s annual salary.
  8 salary = float(input(‘Enter your annual salary: ‘))
  9
 10 # Get the number of years on the current job.
 11 years_on_job = int(input(‘Enter the number of ‘ +
 12 ‘years employed: ‘))
 13
 14 # Determine whether the customer qualifies.
 15 if salary >= MIN_SALARY and years_on_job >= MIN_YEARS:
 16 print(‘You qualify for the loan.’)
 17 else:
 18 print(‘You do not qualify for this loan.’)

A

Example

20
Q

For finding values within a range, it is best to use a _____ operator?

A

-And

Ex: if x >= 20 and x <= 40:
  print(‘The value is in the acceptable range.’)

21
Q

For finding values outside of a range, it is best to use a _______ operator.

A

-Or

EX: if x < 20 or x > 40:
  print(‘The value is outside the acceptable range.’)

22
Q

Describe what the following program does:
if turtle.heading() >= 90 and turtle.heading() <= 270:
  turtle.setheading(180)

A

If the turtle is facing an angle of greater than 90 degrees AND less than 270 degrees, then the turtle heading will be set to 180

23
Q
Explain what the following code does: 
if turtle.speed() == 0:
     turtle.pencolor('red')
 elif turtle.speed() > 5:
     turtle.pencolor('blue')
 else:
     turtle.pencolor('green')
A

If the speed is 0, the pen color is set to red. Otherwise, if the speed is greater than 5, the pen color is set to blue. Otherwise, the pen color is set to green: