Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new on Python.
I want to create a function with two vectors inside. I tried like this
def twovectors((velocity1,length1),(velocity2,length2)):
but I have a message error like
SyntaxError: invalid syntax.
Please, need help.
You cannot put tuple in the function definition as a parameter. Check Multiple Function Arguments or 8.6. Function definitions in the Python language reference.
Try something like this this:
def twovectors(vector1, vector2):
velocity1, length1 = vector1
velocity2, length2 = vector2
# Other code...
I used tuple unpacking to expand provided tuple arguments.
You write functions in python in this way :
def twovectors(velocity1, velocity2):
# You can get the length of those vectors after you get inside the function
len1, len2 = len(velocity1), len(velocity2)
// Your code here
return whateveryouwantto
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am writing a code with the purpose of calculating a math function (given as an input) and calculate it with different values of x (without defining the x).
Example: input: y=log(2x) and i have to calculate for x=3,5,8
With the math module is quite easy to calculate the value of y given x but i can't find a solution to keep the x as variable and then calculate y depending on the change of x.
Since the moment that i can't write the function as a string i tried to define a new function
Example: def function(f, x)
So i just needed to input function(f=math.log(2x),x) but the problem is still there because when i recall the function changing the x (Example : function(f,x=5)) i don't have the f defined anymore.
So briefly, how i can make the functions of the math module dipendent from x?
You simply need to define a new function.
def example(x):
return math.log(2 * x)
Then you can call the function using the usual syntax
example(x)
As correctly noted in the comments, you can also define this function using anonymous syntax
example = lambda x: math.log(2 * x)
example(x)
Although this sort of defeats the purpose of anonymous functions if you're planning on giving the function a name anyway.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
x = 2
creates an integer variable.
y.append(3)
gives an error message (name 'y' is not defined).
In the first one a variable x can be assigned a value without first defining it, why can't we do the same thing with the lists (you have to first define it using l = []). Is this the result of a fundamental design choice in the Python language?
Thanks.
The actual act of assignment also creates the variable if it doesn't exist.
When you attempt to call a member function the variable needs to exist, so the Python interpreter knows what kind of variable it is (the type of the variable). If the interpreter doesn't know the kind of the variable, it can't know what member functions exist on the object.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have seen other people ask a question but the only answers I have seen simply explain that python doesn't have the same concept of pass by reference vs pass by value as languages like C do.
for example
x=[0]
def foo(x):
x[0] += 1
In the past, I have been using this work around but it seems very un-pythonic so I'm wondering if there is a better way to do this.
let's assume for what ever reason returning values won't work, like in the case where this code runs on a separate thread.
Some python objects are immutable (tuple, int, float, str, etc). As you have noted, you cannot modify these in-place.
The best workaround is to not try to fake passing by reference, instead you should assign the result. This is both easier to read and less error prone.
In your case, you could call:
x = 0
def f(x):
return x + 1
x = f(x)
If you truly need to fake passing by reference (and I don't see why you would need that), your solution works just fine, but keep in mind that you do not actually modify the object.
x = 0
x_list = [x]
print(id(x_list[0])) # 1844716176
def f(x_list):
x_list[0] += 1
f(x_list)
print(x) # 0, not modified
print(id(x_list[0])) # 1844716208
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In python, if a person has the following code, will the 2 methods called automatically add? how does this work?
example:
def trip_cost(city,days):
return plane_ride_cost(city) + hotel_cost(days) + rental_car_cost(days)
Will hotel_cost(days) and rental_car_cost(days) both be added together when they are returned to trip_cost(city,days) ??
If the return values of the methods you invoke have a defined + operator they will add up.
If the 3 return numeric types (for example: float, int): yes
But if 2 return numeric and one returns a str: no
The return values may also be instances of objects defined by yourself which may have a defined + operator (which may or may not be compatible with numeric types or with strings)
Each function call [e.g. plane_ride_cost(city)] is evaluated and the results will be added together (in left to right order as +'s have equal precedence) then the overall result returned.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm looking for a resource that will help me turn my python function definitions into professional quality functions.
For example, my current python definitions have the following form:
def foo(arg1,arg2,arg3=some_val,arg4=some_other_val):
""" Do something with the args """
# Create data using args
if arg3 == this_val:
return dataset_1, dataset_2
else :
return dataset_1, dataset_2, dataset_3
I would like for a way to return the data I'm interested in based on what return data I ask for.
For example, I could say:
ds_1, ds_2, ds_3 = foo(arg1,arg2)
or
ds_1, ds_2 = foo(arg1,arg2,arg3=only_two_datasets_bool)
How do I make it so the function doesn't need the optional argument to know I only want two datasets?
This is analogous to say matplotlib constructors where one can say:
n = plt.hist(data)
where n is the histogram but one can also do
n, bins = plt.hist(data)
and the hist function knows to return those two values with the same input of data (ie no optional arguments stating it should return bins).
While it may seem clunky and inelegant, you could unpack three variables and only use two of them if desired:
def foo(arg1, arg2):
return (arg1,arg2,3)
a, b, _ = asd(1,2)
print a, b # will print "1 2"
While the _ does not mean "unused variable" in python as it does in many other languages, many (or perhaps most) will recognize it as such. Even languages who don't offer explicit support for it, like Lua, encourage the use of _ as a placeholder variable due to conventions.
Use it in such a way that it is clear what the _ means though, as it in python shell contains the value of the previously evaluated expression:
>>> 1+2
3
>>> _+3
6
>>>