Week 3 - Conditional Branching

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

T/F: The expression x==y is equivalent to x is y.

False - x==y is a value comparison and x is y is an id comparison

2 == "2"

False -can still be compared, assessing if the values are equal or not equal to

T/F: All boolean operators have the same precedence.

False -they have the same groupings, but the orders of operations(precedence) differ

control structure

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

sequence structure

a set of statements that execute in the order they appear

Identity operators: is/is not

is and is not allow you to determine whether two variables are the same object -two variables are bound to the same object -is returns True only if the operands reference the same object -is not returns True if the operands reference the different object -DO NOT compare object values, it is a mistake to use == and != -is and is not check the ids

what is try/except used for?

it is used to catch and handle errors that could crash a program

how to find out what the keys are in a dictionary?

keys( )

how do multi-branch statements work?

-each branch condition is checked in sequence -elif (short for else if) is used to add additional conditions -as soon as one condition is True, the branches statements are executed and no subsequent branch is considered -if none of the conditions is True, the else branch executes

not in operator

-syntax: item not in sequence -expression returns True if item is not found in the sequence, False otherwise

try/except

-when a try is reached, the statements in the try block are executed -if no exception occurs, the except block is skipped and the program continues -if an exception does occur, the except block is executed and the program continues after the except block -any statements in the try block not executed before the exception occurred are skipped

precedence rules

1- anything in parentheses ( ) 2- arithmetic operators * / % + - 3- relational operators < <= > >= == != 4- not operator 5- and operator 6- or operator

how does if-else statement work?

1- condition is evaluated 2- if the result of the condition is True, one block of instructions is executed 3- if the result of the condition is False, the other block of instructions is executed

to exit the program

1- import the module sys 2- command sys.exit( ) -used when an error will happen where we need to exit the program

how many spaces needed to be indented after an if statement?

4

ternary operation

A conditional expression that has three operands -format: (value to yield when true) if (condition) else (value to yield when false)

ValueError

A type conversion failed

NameError

A variable name was used that doesn't exist

how to get the keys of a dictionary called D?

D.keys( )

ZeroDivisionError

Division by zero attempted

3 > "2"

Error -cannot do value comparison between integer and string

epsilon

The difference threshold indicating that floating-point numbers are equal

"dog" > "cat"

True -can compare strings by lexicographic order --> unicode values for each character -case sensitive - d unicode value is larger than c

and operator

True only when BOTH parts are True -ex: x=11, y=9 (x > 10) and (y < 8) False

or operator

True when either part is true -easier than doing an elif statement -ex: x=11, y=9 (x > 10) and (y < 8) True

what would be the statement that checks if value x is in the list L?

X in L

lower( )

change the case of a string to all lowercase

upper( )

change the case of a string to all uppercase

relational operator

determines whether a specific relationship exists between two values -known as comparison operators -binary -yield a boolean value --> True or False

==

equal to -do not confuse with assignment operator =

boolean expressions

expression that yields a value of True or False -also known as conditions -typical formed with relational operators

Exception error

general for all non-critical errors

membership operators with dictionary

membership operators check for a specific key, not for the values associated with the key

!=

not equal to

boolean operators

not, and, or

operator chaining

performs comparisons left to right -ex: 1 <= x <=20

repetition structures

repeat a set of statements as many times as necessary -known as loops

not operator

reverses the value of truth -ex: x=3 not(x > 5) --> x is not greater than 5 True

decision structure definition

specific action(s) performed only if a condition exists -known as selection structure or conditional branching

how does the if statement work?

the block of instructions associated to the if is performed only when the result of the condition is true -if the result of the condition is false, the instructions are skipped

comparing floating-point numbers

they should not be compared using == because floating-point numbers cannot be exactly represented in the limited available memory bits -expected to be close, but not exactly equal

try/except format

try: #normal code that might produce errors except Exception as e: #exception handling code

if statement

used to create a decision structure -allows a program to have more than one path of execution (branches) -causes one or more statements to execute only when a boolean expression is true

in operator

used to determine whether an item is contained in a sequence -string, list, tuple, dictionary, set -syntax: item in sequence --> 3 in list -expression returns True if item is found in the sequence, False otherwise

refactor

when you change your code for readability, but you don't add/remove for functionality


Kaugnay na mga set ng pag-aaral

Immune and Hematologic Disorders

View Set

Anatomy and Physiology Final Exam

View Set

Exam OSHA 30 FALL HAZARDS Redvector Clicksafety - Flash Cards

View Set

Music History Final - Short Answer

View Set