Chapter 3 Flashcards
What is the control structure?
a logical design that controls the order in which a set of statements execute.
What is a sequence structure?
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)
What is a decision structure?
a specific action is performed only if a certain condition exists.
-If the condition does not exist, the action is not performed
What is a single alternative decision structure?
There is only one alternative path of execution if the one condition is true
How is an alternative decision structure written out in Python?
Using an if statement:
if condition:
statement
statement
etc.
-Note that the indentation is required
What is a boolean expression?
Expression taht is tested by the if statement to determine true/false
What are the different relational operators used with if statements in Python?
> Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to
If you type a Boolean expression at the _____ prompt, the interpreter will evaluate the expression and display its value as either True or False.
> > >
Ex: 1 >>> x = 1 [Enter] 2 >>> y = 0 [Enter] 3 >>> y < x [Enter] 4 True 5 >>> x < y [Enter] 6 False 7 >>>
if sales > 50000:
bonus = 500.0
What does the above statement mean?
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.
What is a dual alternative decision structure and what line of code is used for it?
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
if condition: statement statement etc. else: statement statement etc.
Explain what this means:
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.
if temperature < 40:
print(“A little cold, isn’t it?”)
else:
print(“Nice weather we’re having.”)
Explain what this does?
If temperature is less than 40, it will print the indented line below. Otherwise, it will print the indented line below else
How does indentation work with if-else statement
- 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 does ASCII work with if and if-else statement comparisons?
The strings are translated into their corresponding ASCII values.
Ex: “M” is the number 77 in ASCII.
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')
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’.