Starting Out With Python Chapter 3
32
A blank space is represented by this number:
How does a dual alternative decision structure work?
A dual alternative decision structure has 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.
decision structure
A statement that uses a condition to determine which set of statements to execute; also known as selection structures - a specific action is performed only if a certain condition exists; if it does NOT exist, it is NOT performed
Compound Boolean Expression
An expression that includes more than one Boolean expression.
The decision structure that has two possible paths of execution is known as_____.
Dual alternative
Boolean expression
Evaluates to either true or false; used in the conditional of an if-structure. TRUE or FALSE
True/False: The Python language is not sensitive to block structuring of code.
False
if statement
In Python we use the ___ ___________to write a single alternative decision structure.
conditionally executed
In a decision structure, when an action is performed if a certain condition is true
What does the logical not operator do?
It reverses the truth of a Boolean expression.
<
Less than operator
When you write an if-else statement, follow these guideline for indentation:
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.
==
The equality operator (sometimes read: "equal equal") is used to compare two values, and returns a Boolean (true/false). Avoid confusion with the assignment operator "=",
97 through 122
The lowercase characters a through z are represented by the numbers:
sequence structure
The simplest type of control structure It is a set of statements that execute in the order in which they appear ( a set of ordered stepst) ie: name = input('What is your name? ') age = (int(input('What is your age? ')) print('Here is the data you entered:') print('Name: ') print('Age: ') is heavily used in programming, but can not handle every task
65 through 90
The uppercase characters A through Z are represented by the numbers:
The logical and operator and the logical or operator allow you to do what?
They allow you to connect multiple Boolean expressions to create a compound expression.
What does the and operator do?
This operator connects two Boolean expressions into one compound expression. Both subexpressions must be true for the compound expression to be true.
What does the or operator do?
This 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.
What does the not operator do?
This 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.
What is the "if" statement
This statement is used to create a decision structure, which allows a program to have more than one path of execution. This statement causes one or more statements to execute only when a Boolean expression is true.
nested
To test more than one condition, a decision structure can be _______________ inside another decision structure.
True/False: The if statement causes one or more statements to execute only when a Boolean expression is true.
True
Boolean variable can reference one of two values: _____.
True or false
48 through 57
When the digits 0 through 9 are stored in memory as characters, they are represented by the numbers:
single alternative decision structure
a decision structure that provides only one alternative path of execution in Python we use the if statement to write this
control structure
a logical design that controls the order in which a set of statements execute ie: name = input('What is your name? ') age = input('What is your name? ') print('Here is the data you entered:') print('Name: ', name) print('Age: ', age) (determines the order in which a set of statements execute)
block
a set of statements that belong together as a group. it must be indented in Python; the Python interpreter uses it to tell where it begins and ends
Write an if/else statement that compares age with 65, adds 1 to senior_citizens if age is greater than or equal to 65, and adds 1 to non_seniors otherwise.
age == 65 if age >= 65: senior_citizens += 1 else: non_seniors += 1
Block structure programming
allows the programmer to create blocks that may also include nested block so that statements or codes can be grouped together.
condition
an expression that is evaluated as either true or false, with a colon following the expression
Given the variable c, whose associated value is a str, write an expression that is True if and only if c is not equal to a string consisting of a single blank.
c != str(" ")
Assume that c is a variable has been assigned a string value. Write an expression whose value is true if and only if c is a tab character.
c == ' '
Assume that c is a variable that has been assigned a string value. Write an expression whose value is true if and only if c is a space character.
c == ' '
Assume that c is a variable that has assigned a string value. Write an expression whose value is true if and only if c is a newline character.
c == '\n'
Which of the following is the correct clause to use to determine whether choice is other than 10?
choice != 10:
a(n)_____ structure is a logical design that controls the order in which a set of statements execute.
control
relational operator
determines whether a specific relationship exists between two values. for example, the greater than operator (>) determines whether one value is greater than the other.
True/False: Python uses the same symbols for the assignment operator and the equality operator.
false
True/False: The not operator is a unary operator and it must be a compound expression.
false
What is the result of the following Boolean expression, if x equals 5, y equals 3, and z equals 8? not (x < y or z > x) and y < z
false
What is the result of the following Boolean expression, if x equals 5, y equals 3, and z equals 8? x < y and z > x
false
>
greater than operator
>=
greater than or equal to operator
dual alternative decision structure
has two possible paths of execution one path is taken if the condition is true and the other path is taken if the condition is false
dual-alternative decision structure
has two possible paths of execution, one path is taken if the condition is true and the other path is taken if the condition is false
Working overtime is defined as having worked more than 40 hours during the week. Given the variable hours_worked, write an expression that evaluates to True if the employee worked overtime.
hours_worked > 40
You have to do your chemistry homework, but you hate looking up elements on the periodic table! Write a program that takes the name of an element (as a string, independent of case) from standard input and prints a double representing its atomic weight to standard output. Only implement the program for the first three elements, hydrogren, helium, and lithium, which have the respective atomic weights 1.008, 4.0026, and 6.94. If anything else is given as input, print the statement "Sorry, I don't recognize that element!"
hydrogen = 1.008 helium = 4.0026 lithium = 6.94 elementName = input().lower() if elementName == 'hydrogen': print(hydrogen) elif elementName == 'helium': print(helium) elif elementName == 'lithium': print(lithium) else: print("Sorry, I don't recognize that element!")
Assume that isIsosceles is a boolean variable, and that the variables isoCount,triangleCount, and polygonCount have all been initialized. Write a statement that adds 1 to each of these count variables (isoCount,triangleCount, and polygonCount)if isIsosceles is true.
if isIsosceles == True: isoCount +=1 triangleCount +=1 polygonCount +=1
Write a conditional that decreases the value associated with shelf_life by 4 if the value associated with outside_temperature is greater than 90.
if outside_temperature > 90: shelf_life-=4
Write an if/else statement that compares sold_yesterday and sold_today, and based upon that comparison assigns sales_trend the value -1 (the case where sold_yesterday is greater than sold_today) or 1.
if sold_yesterday > sold_today: sales_trend = -1 else: sales_trend = 1
Write an if/else statement that assigns True to fever if temperature is greater than 98.6; otherwise it assigns False to fever.
if temperature > 98.6: fever = True else: fever = False
Write a conditional that multiplies the value associated with pay by one-and-a-half if worked_overtime is associated with True.
if worked_overtime == True: pay = pay*1.5
Given x and y, each associated with an int, write a fragment of code that associates the larger of these with another variable named max.
if x > y: max = x else: max = y
Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min.
if x<=y and x<=z: min=x if y<=x and y<=z: min=y if z<=x and z<=y: min=z
In Python we write a dual alternative decision structure as an _____ statement
if-else
Write an expression that evaluates to True if the value of index is greater than the value of last_index.
index > last_index
Write a program that assigns two integer values from standard input to the variables int1 and int2, then prints "True" if they are equal, and "False" if they are not.
int1 = int(input()) int2 = int(input()) if int1 == int2: print('True') else: print('False')
Write an expression that evaluates to True if the variable last_name refers to a str that is greater than the str "Dexter".
last_name > "Dexter"
<=
less than or equal to operator
or
logical 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. x == y or x == z
and
logical operator that connects two Boolean expressions into one compound expression. both subexpressions must be true for the compound expression to be true x > y and a < b
not
logical operator that is a unary operator, meaning it works with only one operand. The operand must be a Boolean expression It 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. not (x > y)
=!
not equal to operator
When using the____ operator, one or both subexpressions must be true for the compound expression to be true.
or
Short-circuit evaluation (of a logical expression)
process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. Both the and and or operators perform this.
Write an expression that evaluates to True if and only if the variable s does not refer to the str "end".
s != "end"
Write an expression that evaluates to True if and only if s refers to the str "end".
s == end
Write an expression that evaluates to True if the str associated with s1 is greater than the str associated with s2.
s1 > s2
flag variable
signals when some condition exists in the program Boolean variables are commonly used as this
What statement do you use in Python to write a dual alternative decision structure?
the if-else statement
True/False: Decision structures are also known as selection structures.
true
True/False: Expressions that are tested by the if statement are called Boolean expressions.
true
True/False: Nested decision structures are one way to test more than one condition.
true
What is the result of the following Boolean Expression, if x equal 5, y equals 3, and z equals 8? x < y or z > x
true
if_else statement
will execute one block of statements if its condition is true, or another block if its condition is false
What does the following expression mean? x <= y
x is less than or equal to y