conditionals chapter 3 Flashcards
What is boolean operation?
A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise:
what is a logical operator?
There are three logical operators: and, or, and not. The semantics (meaning) of these operators is similar to their meaning in English. For example,
what is a conditional excution?
Conditional statements give us this ability. The simplest form is the if statement:
if x > 0 :
print(‘x is positive’)
what is another alternative excution?
A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this:
if x%2 == 0 :
print(‘x is even’)
else :
print(‘x is odd’)
what are chained conditionals?
Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional:
Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional:
what is a nested conditional?
One conditional can also be nested within another. We could have written the three-branch example like this:
if x == y:
print(‘x and y are equal’)
else:
if x < y:
print(‘x is less than y’)
else:
print(‘x is greater than y’)
How is Try and Except display?
The idea of try and except is that you know that some sequence of instruction(s) may have a problem and you want to add some statements to be executed if an error occurs. These extra statements (the except block) are ignored if there is no error.
ex:
inp = input(‘Enter Fahrenheit Temperature:’)
try:
fahr = float(inp)
cel = (fahr - 32.0) * 5.0 / 9.0
print(cel)
except:
print(‘Please enter a number’)
Debugging
The most useful parts are usually:
What kind of error it was, and
Where it occurred.
what are the comparisson operators?
comparison operator
One of the operators that compares its operands: ==, !=, >, <, >=, and <=.
what is a compund statement
compound statement
A statement that consists of a header and a body. The header ends with a colon (:). The body is indented relative to the header.