Decomposition part 3 Flashcards

1
Q

Write a function called “emphasize” that takes a string as a parameter. This function returns a modified version of the string:
- !!! will be added onto the end

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

Creating variables all at once at the start of a function

A
  • Stylistic Note, not syntactically required but a stylistic approach
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

idenitifers (constants or variables) that are declared within the bidt of a function have a ——

A

local scope (the function)

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

identifiers (constants or variables) that are created outside the body of a function have a —–

A

global scope (the program)
- to the end of the start() call

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

Tell me the output of this program:

num1 = 10

def fun():
   print(num1)

def start():
   fun()
   print(num2)

num2 = 20

start()
A

10
20

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

When a variable is declared outside of a funtion, it becomes a

A

global variable

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

You can acess the contents of global variables anywhere in the program, in python, this can occur

A

even if the “global” keyword is not used

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

Why is the use of glonal variables bad programming style

A

they can be accidentally modified anywhere in the program

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

Difference between global variables and global constants:

A

global constants: used for values that remain constant
global variables: used for data that can be modififed during execution

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

Whats the output?

num = 1

def fun():
   num = 2
   print(num)

def start():
   print(num)
   fun()
   print(num)

start()
A

1
2
1

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

passing variables does not…..

A

overwrite local variables

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

If you want to modify the value of a global variable from within the function, it will…. +ex (2)

A

not change it, just creates a local variable
ex:
~~~
name = 5 #global name
def fun():
name = 2 #local name
~~~

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

if you want to modify global variables from within the function, you need to

A

use the global keyword.
Prefacing the name of a variable with the keyword global in a function will indicate changes in the funtiuon will refer to the global variable rather than creating a local one
~~~
global <variable>
~~~</variable>

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

Tell me the output:
~~~
num = 1

def fun():
global num
num = 2
print(num)

def start():
print(num)
fun()
print(num)

start()
~~~

A

1
2
2

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

Boolean functions +ex (3)

A
  • Return a Bolean value (T/F)
  • Typically the Boolean function will ask the question about a parameter(s):
  • ex: is it true that the string can be converted to a number? (def isNum(aString):)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write me a Boolean function that: sees if the the entered value is a number before proceeding

A
17
Q

Decompose this long function fo rme

A
18
Q

function definition needs to be —— the —–

A

before
function call

19
Q

with python, the missing set of brackets in a function call do not produce a (2)

A

syntax/translation error

20
Q

When creating empty functions…

A

it cannot be fully empty, a function must have at least one instruction in the body.
- ex: add “print()”