Python how to mutate a variable in a method call [duplicate] - python

This question already has answers here:
Can we have assignment in a condition?
(10 answers)
How to avoid writing request.GET.get() twice in order to print it?
(10 answers)
How to do variable assignment inside a while(expression) loop in Python?
(6 answers)
Closed 4 years ago.
Is it possible to modify variables in if statements, loops, and function calls in Python like you can in C and Java?
Ex:
i=0
while((i+=1)<10): #invalid syntax
print(i)
If not why is that?

as long as the datatype is mutable
def modified(data):
data['a'] = 5
x = {'b':7}
modified(x)
print(x)
def increment_a(data):
data['a'] += 1
return data['a']
x = {'a':1}
while increment_a(x) < 10:
print(x)
however strings and integers are immutable

Related

How do I change the format in which a function is displayed when using print()? - Python [duplicate]

This question already has answers here:
Python3 tuple and list, print out string comma-seperated
(2 answers)
Printing tuple with string formatting in Python
(16 answers)
How to unpack and print a tuple
(2 answers)
Closed 1 year ago.
How can I change the format in which multiple return values from a function are displayed. When I print the function, the two return values are displayed inside parentheses. Is there a way to change this?
def test():
x = 1 + 1
y = 2 + 2
return x, y
print(test())
This prints the following: (2, 4)
I would like for it to be displayed like this: 2 and 4
You can use string format:
def test():
return 1, 2
print("%s and %s" % (test()))
Here and here you can read more about its functionalities.

How does Python know I want to double number I did not define? (Beginner) [duplicate]

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 2 years ago.
x = 10
def double(y):
return 2 * x
print(double(x))
Output is 20
As far as I know, it should return None because in function "double" I double x which is undefined that block.
x is defined outside of the function scope as a global variable that is also available in the function
It is because x is a global variable, so when you call double it is just multiplying x by 2 no matter what
if you were to say put
x = 10
def double(y):
return 2 * x
print(double(40))
you would still get 20 as you are returning the variable x multiplied by 2.

Is there any functional difference between return x,y and return (x,y)? Or is it purely syntactical? [duplicate]

This question already has answers here:
When are parentheses required around a tuple?
(3 answers)
Closed 3 years ago.
So, for example, is there any actual difference between:
def test1():
x = 1
y = 2
return x,y
And:
def test2():
x = 1
y = 2
return (x,y)
Both are valid constructors of the builtin tuple type, so, yes, the difference is purely a different syntax for the same thing.

Variable scope of array different from double in python [duplicate]

This question already has answers here:
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
Changing one list unexpectedly changes another, too [duplicate]
(5 answers)
Closed 4 years ago.
When I run the following block of code in python, it seems like for input x as a integer, func3 and func4 are correctly capturing the scope of the input. However, if x is an array, func2 sees input x not as [10], but as [9]. Why? Can functions in python change variables outside the scope of that specific function? Ie, it seems like func1 is modifying the global x.
def func1(x):
x[0] -= 1
return(x)
def func2(x):
x[0] -= 2
return(x)
def func3(x):
x -= 1
return(x)
def func4(x):
x -= 2
return(x)
if __name__ == "__main__":
x = [10]
print(func1(x)) # [9]
print(func2(x)) # [7]
x = 10
print(func3(x)) # 9
print(func4(x)) # 8

why do these two functions not return in same form [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Pass list to function by value [duplicate]
(4 answers)
Closed 6 years ago.
For instance,
def T(x):
for i in range(1,len(x)-1):
x[i]+=x[i-1]+2
def f(x):
x=x+2
return x
x=[1,2,3,4,5]
;T(x)
;print(x)
[1, 5, 10, 16, 5]
the variable x changes in this case but,
x=3
;f(x)
;print(x)
x=3
x does not change in this case.
why is this happening?
Generally speaking, mutable object is passed as reference while immutable ones are passed by values.
To get same result as (1):
x = 3
x = f(x)
print(x)
You can check this web for more info on this.

Categories