Functions, Logic, and Conditionals Flashcards
What is “def”
This command allows you to define a function
What is a function made up of? How do they work?
A function has a header and a body. The header contains the function, the body contains the computations in the function. A function takes inputs, computes them as defined, and returns an output. Functions CAN contain (or “call”) other functions.
Eg. # converts fahrenheit to celsius def fahrenheit2celsius(fahrenheit): celsius = (5.0 / 9) * (fahrenheit - 32) return celsius
What is the most common error made in building a function, which will return a value of “None”
Forgot to include a “return” statement in the bottom of the function body.
In the expression “6 + 7 % 5”, which operator is evaluated first (ie. which has higher precedence)?
% is evaluated first, so parentheses needed around anything it applies to.
Eg. (6 + 7) % 5
What does the “import” statement do in Python?
“import” loads the functions of an external module for use.
Eg. import “math”
Boolean Expressions NOT, AND, and OR. What makes each true?
All may be true or false.
NOT is true if x is false.
AND is true if x and y are true
OR is true if x or y is true
Comparison operators? (six)
- > greater than
- < less than
3, >= greater or equal to - <= less or equal to
- == equal to
- != not equal to
Conditional statements? (Three)
"If" = If value is true, then perform this action "Elif" = if the value is true but another condition is not met, then do perform a different action "Else" = if value is false, then perform this action