Using a variable outside of a defined function [duplicate] - python

This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 months ago.
If I had a function that I made:
def a():
n = 2*2
How could I access n out of the function without calling a?

You cannot. You will need to define the variable outside of the function, or call the function and return it.

You need to return it, so do:
def a():
n = 2*2
return n
print(a())
Output:
4
You can also do print, but return is better, check this: What is the formal difference between "print" and "return"?

Related

Why does this return None? Instead of returning fact? [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 6 months ago.
(it returns none)---> why?
fact = 1
def factorial(n):
if (n-1)!=0:
global fact
fact=fact*n
n=n-1
print(fact)
factorial(n)
else:
return fact
n=int(input())
g=factorial(n)
print(g)
Because you need to return factorial(n) in factorial function, otherwise it just gets called and does not return any result in the calling function. Also, you don't need the global variable, simply pass it along with n in the factorial function itself when doing recursive call.
Also, there's a cleanest solution without any unnecessary variables:
def factorial(n):
if n < 2:
return 1
else:
return n * factorial(n-1)
And if you dont wanna reinvent the wheel just use math module:
import math
math.factorial(1234)

Same method with two different names? [duplicate]

This question already has answers here:
Can one function have multiple names?
(3 answers)
Closed 5 years ago.
I wold like to use same method but two different names.
For example:
def func(a):
print a
def func2(a):
print a
n= "yes"
func(n)
func2(n)
answer should be:
"yes"
"yes"
Would there be any way I can do:
def fun(a) or func2(a):
print a
or something like this?
Python functions are just objects, you can assign one to another name:
def fun(a):
print a
func2 = fun
Now the names func2 and fun reference the same function object, you can call it through either name.
def func(a):
print("a")
n= "yes"
def fun(a):
func(a)
func(n)

"Burn in" a global variable on function definition [duplicate]

This question already has answers here:
Read/Write Python Closures
(8 answers)
Local variables in nested functions
(4 answers)
Closed 5 years ago.
What is the way to "burn in" an external variable to a function upon definition?
funcs = []
for n in range(5):
def f(x):
return n * x
funcs.append(f)
del n
But trying
[g(2) for g in funcs]
or
funcs[1](2)
Gives an error
NameError: name 'n' is not defined
Is there a way to allow defining a function with a parameter which will become fixed for the function scope?

Confusion as to function parameters [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 years ago.
I am trying to make a simple function that accepts 2 parameters, and adds them together using "+".
def do_plus (a,b):
a=3
b=7
result = a + b
print (result)
However, I get no value returned, the function is executed but no output is shown.
You're missing the indentation.
a=3
b=7
def do_plus (a,b):
result =a+b
print (result)
# and you have to call the function:
do_plus(a,b)
You probably want to separate logic from input/output, as so:
def do_plus(a, b):
result = a + b
return result
res = do_plus(3, 7)
print(res)
try this:
def do_plus (a,b):
print=a+b
do_plus(3, 7)
you can call your function "do_plus" passing parameters and print wath the function return
Attention to the "spaces" before result is important in python the identation of script
It's hard to tell from your code because the indentation is off, but a simple addition function can be something like:
def addition(a, b):
return a + b
You are accepting parameters a and b, but then assigning them values 7 and 3, so that no matter what, it will return 10.

How should I code a function to turn a parameter I input into 1? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 8 years ago.
I am the new to Python. Here is a problem that I have encountered.
Assume that there is a function called set1(x).
Here is the code:
def set1(x):
x = 1;
When I run this,
m = 5
set1(m)
m
the value of m is still 5.
So how should I code the function if I want the parameter to become 1 whenever I call the set1() function?
You can return the value
def set1():
return 1;
And call it as
m=5
m = set1()
print (m)
It will print 1
Another way but bad method is to make it global
def set1():
global m
m = 1
Functions use a local namespace in which they declare their variables, which means that in most cases when you pass an argument to a function, it will not effect the argument outside of the function (in the global namespace). So while the value changes within the function, it does not change outside of the function.
If you want the function to change the value, you need to return the value as such:
def set1(x):
x=1
return x
>>> m=5
>>> m = set1(m)
>>> m
1
This said, if your argument is mutable, it can change.
def set2(x):
x[0] = 1
>>> m = [2]
>>> set2(m)
>>> m[0]
1

Categories

Resources