简体   繁体   English

Python:如何创建一个函数? 例如 f(x) = ax^2

[英]Python: How to create a function? e.g. f(x) = ax^2

I want to have some sort of reference to a function but I do not know if I need to use a def f(x) or a lambda of some kind.我想对某个函数有某种引用,但我不知道是否需要使用def f(x)或某种形式的lambda

For instance I'd like to print f(3) and have it output 9a , or is this not how python works?例如,我想print f(3)并让它输出9a ,或者这不是 python 的工作方式?

Second question : Assuming I have a working function, how do I return the degree of it?第二个问题:假设我有一个工作函数,我如何返回它的degree

To create a function, you define it.要创建一个函数,你需要定义它。 Functions can do anything, but their primary use pattern is taking parameters and returning values.函数可以做任何事情,但它们的主要使用模式是接受参数和返回值。 You have to decide how exactly it transforms parameters into the return value.您必须决定它如何准确地将参数转换为返回值。

For instance, if you want f(x) to return a number, then a should also be a numeric variable defined globally or inside the function:例如,如果你想让f(x)返回一个数字,那么a也应该是一个全局或函数内部定义的数字变量:

In [1]: def f(x):
   ...:     a = 2.5
   ...:     return a * x**2
   ...: 

In [2]: f(3)
Out[2]: 22.5

Or maybe you want it to return a string like this:或者,您可能希望它返回这样的字符串:

In [3]: def f(x):
   ...:     return str(x**2) + 'a'
   ...: 

In [4]: f(3)
Out[4]: '9a'

You have to specify your needs if you need more help.如果您需要更多帮助,您必须指明您的需求。


EDIT: As it turns out, you want to work with polynomials or algebraic functions as objects and do some algebraic stuff with them.编辑:事实证明,您想使用多项式或代数函数作为对象,并用它们做一些代数运算。 Python will allow doing that, but not using standard data types. Python 将允许这样做,但不能使用标准数据类型。 You can define a class for a polynomial and then define any methods or functions to get the highest power or anything else.您可以为多项式定义一个类,然后定义任何方法或函数以获得最高的幂或其他任何东西。 But Polynomial is not a built-in data type.Polynomial不是内置数据类型。 There may be some good libraries defining such classes, though.不过,可能有一些很好的库定义了这样的类。

Python (and most other computer languages) don't do algebra, which is what you'll need if you want symbolic output like this. Python(和大多数其他计算机语言)不做代数,如果你想要像这样的符号输出,这就是你需要的。 But you could have a function f(a,x) which returns the result for particular (numerical) values of a :但是,你可以有一个函数f(a,x)它返回的特定(数字)值,结果a

def f(a, x):
   return a*x*x

But if you want a program or language which actually does algebra for you, check out sympy or commercial programs like Mathematica.但是如果你想要一个真正为你做代数的程序或语言,请查看sympy或商业程序,如 Mathematica。

If you are just working with polynomials, and you just need a data structure which deals well with them, check out numpy and its polynomial class .如果您只是使用多项式,并且您只需要一个可以很好地处理它们的数据结构,请查看numpy 及其多项式类

I normally use lambda for short and simple functions:我通常将 lambda 用于简短的函数:

f = lambda a, x: a * x**2

here a and x are parameters of my function.这里 a 和 x 是我的函数的参数。 You need to enter a and x您需要输入 a 和 x

f(2,4)

If you want a as a constant parameter eg.如果您想将 a 作为常量参数,例如。 a=2: a=2:

f = lambda x: 2 * x**2
f(5)

if you have a list of input values of x, you can combine map with lambda.如果您有 x 的输入值列表,则可以将 map 与 lambda 结合使用。 it is straighforward and easily readable.它简单易读。

(*map(lambda x: 3 * x**2, [1,2,3,4]),) 

or或者

list(map(lambda x: 3 * x**2, [1,2,3,4])

cheers!干杯!

def func():
    print "F(x) = 2x + 3"
    x = int(raw_input('Enter an integer value for x: '))
    Fx = 2 * x + 3
    return Fx

print func()

have fun :)玩得开心 :)

Cheese,奶酪,

you can use the def function in Python to create a math function, you could type this:你可以使用 Python 中的def函数来创建一个数学函数,你可以输入:

def f(x):
    return(2x + (3 + 3) * 11 + 88) # <- you could make your own function.

print(f(3))

Log:日志:

220

Like THAT类似

or in this:或在此:

def f(a, x):
    return((a + x) ** (a * x))

then...然后...

print(f(1, 2))

Log...日志...

6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何矢量化 python 中包含限制的 function(例如 sin(x)/x) - How to vectorize a function in python that includes a limit (e.g. sin(x)/x) 如何在 Python 中创建 x 列表(例如使用 while 循环)? - How do I create x lists in Python (e.g. with a while-loop)? 如何动态更改功能接口,例如f()-&gt; f(a,b,c =&#39;default&#39;) - How to dynamically change a function interface, e.g. f() -> f(a, b, c='default') 如何使用指示 Python 中 XY 轴频率的分布曲线(例如,正态分布)创建线性回归 plot? - How to create a linear regression plot with distribution curves (e.g., normal distribution) that indicate the frequency at the X-Y axes in Python? 在python中调用fortran代码时如何处理全局变量(例如,使用f2py)? - How to deal with global variables when calling fortran code in python (e.g. with f2py)? 如何为 x 轴创建自定义数字间距,例如 1/x? - How to create a custom numeric spacing for the x-axis, e.g. like 1/x? 如何在 Python 中每隔 x 秒将声音写入音频文件(例如 .mp3)? - How to write every x seconds a sound into an audio file (e.g. .mp3) in Python? 将可选的 arguments 传递给例如 ax.set(ylabel=...) - Passing optional arguments to, e.g., ax.set(ylabel=…) python中any()函数的对立面是什么,例如没有任何 - What is the opposite of any() function in python, e.g. without any 可视化 Python function 流(例如作为树或概念图) - Visualize Python function flow (e.g. as tree or concept map)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM