Section 04: Functions and Statements Flashcards

1
Q

What are the different types of operators?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define

Boolean Values:

A

**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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Comparison Operators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define

Control Flow

A

Control flow dictates order of execution

Specific control flow statements changes order of execution

  1. Which part of the code should always be executed
  2. Which parts of the code should be executed only under certain circumstances
  3. Which blocks of code should be executed repeatedly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Code

(1) If Statement:

A

If Statement: list of instructions if the conditions is True

else Statement: list of instructions if the conditions is false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code

(2) Chained Conditionals:

A
  1. if condition 1:
    List instructions to do if condition 1 is true
  2. elif condition 2:
    List of instructions to do if condition 2 is true
  3. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Code

(3) Nested Conditionals

A

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')

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Errors

NameError

A

NameError: Raised when local or global name is not found → only applies to unqualified names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Errors

TypeError

A

TypeError: Raised operation or function applied to an object inappropriate type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Errors

SyntaxError

A

SyntaxError: Raised parser encounters a syntax error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define

Function

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define

Void Functions

A

Void Functions: functions not return a value but displays a message

Does not return a value

Why: Avoid repetition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define

Fruitful Functions

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Code

Function Object

A
  • (): parameters = have empty or not
  • must indent statements to show part of the function defined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Function Definitions

Flow of Execution

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Define

Input Argument:

In relation to parameters

A
  • Parameter not empty ⇒ has an input argument (any kind of expression)
    • Expression always be evaluated before being given to the function
    • Result of evaluation will be assignmed to parameter inside function
    • Arguments adds flexibility to function

Local variables: Only exist inside definition

Ex:
def function_name(input_val):
print('Some shit', input_val)

17
Q

Flow of Execution

When variable is called:

A

Variables created for each parameter
- Initialized with values provided through function call
Body of function executed

18
Q

Flow of Execution

When nothing left in body to execute

A
  • All local variables are discarded
  • Program goes back to line function called from
19
Q

The return statement:

A

Want create function returns value → return statement
Return: allows terminate the execution of function as soon as statement executed
- “Gives back” a value

Why use return value?

  1. Allows function store output, which can be used later
  2. Can save return value in a var or used fruitful functions as part of an expression
20
Q

Scope of Variables:

A
  • Variable only exists inside body of the function in which it is created
  • Not exist outside function

> Call ”scope” variable that part of the code which it exists

Difference between local and global variables:

  1. Local Variables: Variable declared insie function’s body
    • Private memory space
    • Memory space allocated to input argument
  2. Global Variable:

Inside a Function:

  • Local variables: functions argument(s), and all other vars created in function
  • Global variables: values can be accessed directly → changed through global keyword

Best Practices:

  • Avoid referring global vars in functions → makes confusing
21
Q

Void Type

NonType

A
  • Parity function → void type: no return value
  • Void: May have input argument but no return value

Return value: None

NoneType: Used to indicate the absence of a value
- Fruitful functions those return value other than None
- Should then have at least one return statement

22
Q

Docstring:

A

Docstring: (documentation string) string write after header fo a function explain how it works

simmilar to comments but ignored by interpreter

  • Use ””” or ’”
23
Q

Parts of a Docstring:

A
  1. Description: what the function does
  2. Type Contract: Describe type of value
  3. Example: write couple of example function w corresponding return values