Section 04: Functions and Statements Flashcards
What are the different types of operators?
Values and Types: Different types
Arithmetic Operators: +, -, *, /, %, //, **
- Int
and float
- Expressions containing them evaluate to int/float values
- Function in math module
**String operators**: `+, *` - Operate on strings
Define
Boolean Values:
**Either equal to True
or False
**
- Useful checking conditions and making options
-Takes boolean expressions as inputs and produces type of boolean
Types: not, and, or
-
not
: unary operator -
and, or
: binary operator
Comparison Operators
To compare two numbers, use the Relational Operators
Use ==
to compare if two numbers are equal (*=
is used for value assignment)*
>>> x = 1.1 + 2.2
>>> print(x == 3.3)
#Print: False
Define
Control Flow
Control flow dictates order of execution
Specific control flow statements changes order of execution
- Which part of the code should always be executed
- Which parts of the code should be executed only under certain circumstances
- Which blocks of code should be executed repeatedly
Code
(1) If
Statement:
If
Statement: list of instructions if the conditions is True
else
Statement: list of instructions if the conditions is false
Code
(2) Chained Conditionals:
-
if
condition 1:
List instructions to do if condition 1 is true -
elif
condition 2:
List of instructions to do if condition 2 is true -
else:
List of instructions to do if both condition 1 and 2 are false
- Only one block will be executed → order matters
- The final
else
is optional
Code
(3) Nested Conditionals
If
statement within an if
statement
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')
Errors
NameError
NameError
: Raised when local or global name is not found → only applies to unqualified names
Errors
TypeError
TypeError
: Raised operation or function applied to an object inappropriate type
Errors
SyntaxError
SyntaxError
: Raised parser encounters a syntax error
Define
Function
Function: named block of code preforms task
- must be inside a module: file contains a collection of related functions
- Call a function execute code in function defintion
- Infinitely many
Input: argument
Output: return value
Ex: print(), pow(x,y), input() ...
Type: void and fruitful functions
Define
Void Functions
Void Functions: functions not return a value but displays a message
Does not return a value
Why: Avoid repetition
Define
Fruitful Functions
return
a value
Fruitful Funcions: perform calculation then returns a result
- Ex:
math.sqrt(num)
returns square root of num
return value can be stored ad saved into a variable
Code
Function Object
-
()
: parameters = have empty or not - must indent statements to show part of the function defined
Function Definitions
Flow of Execution
- Functions definitions are executed like other statements
effect of execution is to create function object - Statements inside a function are not executed until function is called
- Must define function → then call it
Detour: instead going to next statement, first statement of function called is executed → then go to where left off