Starting Out With Python Chapter 3

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What types of relationships between values can you test with relational operators?

== < > <= >= !=

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

What is a Boolean expression?

An expression that can be evaluated as either true or false

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/False: The Python language is not sensitive to block structuring of code.

False

What is a decision structure?

It is a program structure that can execute a set of statements only under certain circumstances.

What does the logical not operator do?

It reverses the truth of a Boolean expression.

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.

What is a control structure?

The logical design which is used to control or handle the execution order of statements. A set of actions define the flow of events as decided by the flow chart.

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.

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

This appears after a condition

a colon :

single alternative decision structure

a decision structure that provides only one alternative path of execution

control structure

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

block

a set of statements that belong together as a group.

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

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:

In a decision structure, the action is _______________ executed because it is performed only when a certain condition is true.

conditionally

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.

The decision structure that has two possible paths of execution is known as _____.

double alternative

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

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!")

In Python we use the ___ statement to write a single alternative decision structure.

if

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

In Python, this is required; because the Python interpreter uses it to tell where the block begins and ends.

indent

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"

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

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

What does the following expression mean? x <= y

x is less than or equal to y


Set pelajaran terkait

US History Chapter 9: Nationalism and Sectionalism, 1815-1828

View Set

TAMUSA A&M Business Statistics Chapter 1 Supplementary Exercise

View Set

Artificial Intelligence Chapter 11

View Set

Major Theoretical Concepts of Anthropology

View Set

Chapter 32 Eukaryotic Pathogens: Fungal and Parasitic Diseases

View Set

Chapter exam 5: Private insurance plans for seniors

View Set