Decomposition part 2 Flashcards
Difference between
age = seven
print (seven)
and
age = “seven”
print (seven)
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 can you acess local variables outside of a function?
Parameter Passing
Whate does parameter passing do?
Passes a copy of the contents of a variable as the function is called
What does parameter passing communicate?
Communicate information about local variables
Format for parameter passing
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>
Parameters are mention in the —– while the actual arguments are passed during —-
- function definition
- a function call
Each parameters creates variables that are….
local to the function
Passing variables or named constants:
What it looks like+ex
Just specify the name of the identifier
e.g. print(num), random.ranrange(MIN, MAX)
Passing unnamed constants:
What it looks like+ex
Just specify the value of the parameter
e.g. input(“Enter your name: “), print (“Hello”)
You can’t pass —– parameters
negative
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()
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
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:
print (“Lucy”)
input (“Enter your name”)
Whats the output?
def myfunc(): x = 300 def myinnerfunc(): print(x) myinnerfunc() myfunc()
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
Whats the output?
x = 300 def myfunc(): x = 200 print(x) myfunc() print(x)
200
300
Does local variables get used first or global?
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.