Decomposition part 2 Flashcards

1
Q

Difference between

age = seven
print (seven)

and

age = “seven”
print (seven)

A

In Python, quotation marks are used to denote string literals. When you assign a value to a variable using quotation marks, you are assigning a string value to that variable. For example:

If you don’t use quotation marks, Python assumes that you are referring to a variable name. When you write age = seven, Python interprets seven as a variable and tries to find its value. If there is no variable named seven defined, it raises a NameError because it cannot find the variable.

To assign a string value to the age variable, you need to use quotation marks.

age = seven means “set the value of the variable age to the value of the variable seven”. age = “seven” means “set the value of the variable age to the string “seven”.

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

How can you acess local variables outside of a function?

A

Parameter Passing

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

Whate does parameter passing do?

A

Passes a copy of the contents of a variable as the function is called

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

What does parameter passing communicate?

A

Communicate information about local variables

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

Format for parameter passing

A

List the names of the variables in the round brackets that will store the name of the information passed in

Format:

def <Function> (parameter 1, parmeter 2......)</Function>

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

Parameters are mention in the —– while the actual arguments are passed during —-

A
  1. function definition
  2. a function call
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Each parameters creates variables that are….

A

local to the function

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

Passing variables or named constants:

What it looks like+ex

A

Just specify the name of the identifier
e.g. print(num), random.ranrange(MIN, MAX)

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

Passing unnamed constants:

What it looks like+ex

A

Just specify the value of the parameter
e.g. input(“Enter your name: “), print (“Hello”)

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

You can’t pass —– parameters

A

negative

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

In this program, how many local variables do we have

def fun(num1):
print(num1)
num2= 20
print(num2)

 def start():
 num1=1
 fun(num1)
 
 start()
A

To summarize, when passing parameters to a function in Python, you are passing references to the objects.

We have 3 local variables, 2 in fun and 1 in start

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

Even if the parameters are unnamed in the sense that they don’t have explicit variable names associated with them, you can still pass their values as arguments to functions using either positional or keyword arguments. Give me an example and tell me how it works:

A

print (“Lucy”)
input (“Enter your name”)

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

Whats the output?

def myfunc():
  x = 300
  def myinnerfunc():
    print(x)
  myinnerfunc()

myfunc()
A

In the example, the variable x is not available outside the function, but it is available for any function inside the function:
The inner function myinnerfunc() can access the variable x defined in the outer function myfunc(). This behavior is known as “lexical scoping” or “closure” in Python.

300

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

Whats the output?

x = 300

def myfunc():
  x = 200
  print(x)

myfunc()

print(x)
A

200
300

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

Does local variables get used first or global?

A

In Python, when a variable is referenced within a function, the interpreter first looks for that variable’s value within the local scope of the function. If the variable is not found in the local scope, the interpreter then searches for it in the global scope.

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

We only use the input() function when

A

There’s mention of user input

17
Q
A
def fun(parameter1, parameter2):
    print (parameter1, parameter2)
fun (1, 123)

or:

def fun (a, b):
    print (a, end= " ")
    print (b)
           
fun("hi", "love")
18
Q

which is correct?

def fun(parameter1, parameter2):
    print (parameter1, parameter2)
fun ("1", "123")

and 

def fun(parameter1, parameter2):
    print (parameter1, parameter2)
fun (1, 123)
A

Both versions of the code are correct, but they have different parameter values.

In the first version:

def fun(parameter1, parameter2):
print(parameter1, parameter2)
fun(“1”, “123”)

The function fun is called with string arguments “1” and “123”. When executed, it will print “1 123”. In this case, the parameters parameter1 and parameter2 are treated as strings.

In the second version:

def fun(parameter1, parameter2):
print(parameter1, parameter2)
fun(1, 123)

The function fun is called with integer arguments 1 and 123. When executed, it will print 1 123. In this case, the parameters parameter1 and parameter2 are treated as integers.

So, the choice between the two versions depends on whether you want to pass and treat the parameters as strings or integers. Both are correct, but the behavior and output of the function will differ.

19
Q

Write me a celsius to frahrenheit converter with functions

A
def introduction():
    print("""
Celsius to Fahrenheit converter
This program will convert a given Celsius temperature to an equivalent
Fahrenheit value.

    """)
-
def display(celsius, fahrenheit):
        print("")
        print("Celsius value: ", celsius)
        print("Fahrenheit value:", fahrenheit)

def convert():
        celsius = float(input("Type in the celsius temperature: "))
        fahrenheit = celsius * (9 / 5) + 32
        display(celsius, fahrenheit)		
		
def start():
        introduction()
        convert()

start()
20
Q

How many variables in this code:

convert (fahrenheit, celsius)
display (fahrenheit, celsius)

A

4 variables

21
Q

Fix this:

A
22
Q

using return values for multiple values format:

A
23
Q

A function will immediately end and return back to the caller if: (2)

A
  1. a return instructuion is encountered (return can be empty)
  2. There are no more instructions in the function. (Aka change in indenting as it exit the function)
24
Q

Just because a functio returns a value does not automatically mean the return value will be usable by the caller of that function. Function return value must be…..+ex (2)

A

explicitly saved by the caller of the function

25
Q

Parameter passing is used to…

A

pass info into a function before the function executes (during the function call).

26
Q

Parameters are copied into….

A

variables that are local to the function

27
Q

Passing variable vs returning difference

A
28
Q

Return values are used to

A

communicate info out of a function as the function ends (going back/returning to a caller)

29
Q

The return value must be

A

stored in the caller of the function

30
Q

Boolean Functions

A

Not performing an action but asking a question.
Ex:
isNum()
*you pass a parameter to it, a string, and it determine if the string can be converted to a number.”

31
Q

Camel case

+ex

A

Naming system that capitalize the first letter of each word except the first
ex: toUpper()