Chapters 2-4: Functions and Conditionals Flashcards
Boolean Expression
Expression either true or false.
=
Assignment operator
==
Comparison operator - equals
greater than, =/greater than, is/is not operators
> , >=, is, is not
elif
Used after the if, short for else if. can be as many as you want in a block, or none. Used same as if, ie if …..:
else
used at and of the block (if needed) - sets what happens if no conditions (if and elif) are met). used by itself, ie else:
nested conditional
when a conditional eg ‘else:’ has 2 sub braches, eg an if and an else
try/except
try: some code. if there is an error (eg cant float an input because it’s words), move on to the except:. No exception? skip the except:.
Guardian pattern
A logical expression guarding against a runtime error, using the fact logical expressions are evaluated left to right. eg [something] and x!=0 and (3/x)>2. x!=0 guards against runtime error caused if x=0, as 3 cant be divided by it.
what error message tells you about error locaction
tells you where python detected the error, not necessarily where it is. true error may be earlier in code.
Function
named sequence of statements that performs a computation, eg type()
Argument
the expression in the brackets of a function, ie the the expression the function “takes” before it “return”s a “value”
max()/min()
Functions which return the largest/smallest character/number in a string/comma’d list
len()
Function which returns the number of characters in a string
import math
Imports the “math” module. Can then be used, eg assign radians=0.7, height=math.sin(radians) (prefix functions with math.)