How do I do multiple things inside one lambda function? - python

I am learning Python and I want to do multiple things inside one lambda function.
Just for a small example if I want to do addition, subtraction, and multiplication in one function, how do I do that?
I tried to use code like this just to see if it would work and it didn't:
a = 1
b = 2
myFunction = lambda a, b: a + b, b - a, a * b
print(myFunction(a, b))

You can group those operations in a tuple
a = 1
b = 2
myFunction = lambda a, b: (a + b, b - a, a * b)
myFunction(a, b)
output:
(3, 1, 2)
NB. The mistake in you code is that myFunction was a tuple containing your lambda as first element, not a function.

I think there is a slight syntax issue around a lambda returning a tuple.
Use this syntax:
a = 1
b = 2
myFunction = lambda a, b: (a + b, b - a, a * b)
print(myFunction(a, b))

Related

How to use one output of a function and store a second one=

So I have a function which outputs 2 values:
def example(a, b):
c = math.floor(a / b)
a = a%b
return (c, a)
I want to use this function this way:
print("text: ", c)
How can I use the function and print c, but store x for later?
Your function will return a tuple containing the two values. You can assign the result of calling your function to a variable.
Note,
that the parentheses are not required in your return statement.
you can replace math.floor(a / b) with a // b which will also do a floor division.
An example is shown below where the result of calling the function is unpacked into two variables, c and a.
def example(a, b):
c = a // b
a = a % b
return c, a
c, a = example(6, 3)
print("text:", c)
Alternatively, you can also store the result in a single variable that references your tuple as follows:
data = example(6, 3)
print("text:", data[0])
First, you need to call the function and assign its return values to some variables:
x, y = example(42, 5)
Then you can print the results:
print(x)
print(y)
You can even skip the variable assignment if you wish so
print("text:", example(a, b)[0])
but it's ugly

Using arguments as variable names: Why does it solve this problem?

I'm going through some material about functions and I'm writing Python code to make some sense of the pseudocode examples.
The goal is printing the variables after I call the function, then check the new values.
def my_sum(x, y, z):
z = x + y
return x, y, z
A = 1
B = 2
C = 0
my_sum(A, B, C)
my_sum(B, C, A)
my_sum(C, A, B)
my_sum(A, B, C)
my_sum(B, C, A)
print(A, B, C)
My first instinct was to write this procedural approach, but when I do the calling the program won't give the right answer, because A, B and C aren't saving whatever is happening inside the function. So A is always 1, B is 2 and so forth
It turns out when I assign the calling with the arguments, the variables A, B and C receive the new values and they're now keeping it. Finally it prints 21, 8, 13, which is the answer.
A, B, C = my_sum(A, B, C)
B, C, A = my_sum(B, C, A)
C, A, B = my_sum(C, A, B)
A, B, C = my_sum(A, B, C)
B, C, A = my_sum(B, C, A)
How would you implement it or what are the other ways of writing this algorithm?
The thing is I can't wrap my head around why this works at all! It was just a random guess that happened to solve my problem.
python don't have pass by reference option, only pass by value, so your construction is correct, because you returning NEW values (in tuple form), not changing value of variables, that you are passing in.
In Python, an assignment made to a parameter name never affects the value of the name that the caller uses. They are separate names that initially reference the same object, but once the parameter name is assigned something else (like a sum), it references a different object.
Your second attempt works because the function returns a tuple with the values of the three paramater names and your main program unpacks that tuple back into its own names.
However, since the function doesn't need the original value of the third argument, and it doesn't touch the first two arguments, the caller doesn't really need to pass the third argument, and doesn't need to update its own names for the first two arguments... So the function could be designed to only take two arguments and return the new value:
def my_sum(x, y):
return x + y
A = 1
B = 2
C = my_sum(A, B)
A = my_sum(B, C)
B = my_sum(C, A)
C = my_sum(A, B)
A = my_sum(B, C)
Lets start with your function definition and one call.
def my_sum(x, y, z):
z = x + y
return x, y, z
A = 1
B = 2
C = 0
my_sum(A, B, C)
Without the function, this is functionally the same as:
A = 1
B = 2
C = 0
x = A
y = B
z = C
z = x + y
_ = x, y, z
# x, y, and z are discarded since you don't do anything with the return value
You shouldn't expect this to change A, B, or C or if you do you have a misconception about how python variables or names work.
Python variables or names are just a dict with a name pointing to a value.
A = 1
B = 2
C = 0
my_sum(A, B, C)
# this is a very condensed version of what python does in the background
dict_of_globals = dict()
dict_of_globals['A'] = 1
dict_of_globals['B'] = 2
dict_of_globals['C'] = 3
my_sum_local_dict = dict()
my_sum_local_dict['x'] = dict_of_globals['A']
my_sum_local_dict['y'] = dict_of_globals['B']
my_sum_local_dict['z'] = dict_of_globals['C']
# and so on..
Since you only ever assign 1 to dict_of_globals['A'], it would be unreasonable to expect it to be anything other than 1.
The reason this works:
A, B, C = my_sum(A, B, C)
is because you are assigning the return value back to A.
A = x # etc..
# or:
dict_of_globals['A'] = my_sum_local_dict['x']

Transforming lambda expression to simple function

I'm curious if it is possible to avoid lambda expressions in the following case.
For example using lambda expression I can simple define a function that returns a lambda exp.
def square_fun(a,b,c):
return lambda x: a*x**2 + b*x + c
After we can call it using:
f = square_fun(1,2,3)
f(1) # here x = 1
How can I get the same behaviour avoiding lambda expression?
for example, square_fun must return another function
def square_fun(a,b,c):
return f...
In python you can define a function inside another function, so the following snippet should give you the same behavior:
def square_fun(a, b, c):
def result(x):
return a*x**2 + b*x + c
return result
f = square_fun(1, 2, 3)
print(f(1))
# Should print 6
So, I'll try to explain what's going on here:
In the line f = square_fun(1, 2, 3), f will actually be mapped to the internal function (aka. result)
Please note that return result does not have the () at the end, hence the function is not called yet
The function gets called in print(f(1)) and the variable x takes 1 as its value
Finally the result function returns the computed value using x, and a, b and c (from square_fun(1, 2, 3))
You can find additional info here => https://www.learnpython.org/en/Closures
You can also use functools.partial:
from functools import partial
def square_fun(x, a, b, c):
return a * x ** 2 + b * x + c
f = partial(square_fun, a=1, b=2, c=3)
print(f(1))
# should print 6

Python syntax clarification

Scrolling through the python 2.7 docs I came across this snippet
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print a,
a, b = b, a+b
But I don't understand the last line, and unsure of how I would google for it.
How should I read a, b = b, a+b, or, what does it mean ?
Python evaluates the right-hand side of assignments first. It evaluates
b, a+b
from left to right. It then assigns the values to the variables a and b respectively.
So a, b = b, a+b is equivalent to
c = b
d = a+b
a = c
b = d
except that it achieves the result without explicit temporary variables.
See the docs on Python's evaluation order.
There is a subtle point here worth examining with an example. Suppose a = 1, b = 2.
a, b = b, a+b
is equivalent to
a, b = 2, 1+2
a, b = 2, 3
So a gets assign to 2, b is assigned to 3.
Notice that this is not equivalent to
a = b
b = a + b
Since the first line would assign
a = 2
b = 2 + 2 = 4
Notice that done this (wrong) way, b ends up equal to 4, not 3. That's why it is important to know that Python evaluates the right-hand side of assignments first (before any assignments are made).
It is setting a to b, and b to a + b, without needing an intermediate variable. It could also be accomplished with:
temp = a
a = b
b = temp + b

How does this implementation of swap works in Python?

def fibonacci(num):
a=0
b=1
for i in range(num):
a, b=b, a+b
print a
How does the line inside the loop works?
Somehow a & b 's values change, can seems to understand how..
EDIT:
For some reason I got confused, thought that the middle exp of b=b is something new...
didn't read it well..
It really is (a,b) = (b, a+b) which is the basic form of swap in python (:
b, a+b creates a tuple
This tuple is unpacked back into a and b
This line a, b = b, a+b is equivalent to (a, b) = (b, a+b), which is a tuple assignment.
The line in question can be more clearly written (through tuple packing on the right side and sequence unpacking on the left side) as:
(a, b) = (b, a + b)
As the assignments to a and b are carried out in parallel, this is exactly the same as:
new_a = b
new_b = a + b
a = new_a
b = new_b

Categories