Heim  >  Artikel  >  Backend-Entwicklung  >  Ausführliche Erläuterung der Verwendung von Python-Funktionen

Ausführliche Erläuterung der Verwendung von Python-Funktionen

高洛峰
高洛峰Original
2017-03-23 14:33:111404Durchsuche

1. Grundlegende Definition der Funktion


def 函数名称(参数)
         执行语        
         return 返回值

def: Schlüsselwort zur Definition der Funktion; Wie der Name schon sagt, ist es der Name der Funktion. Es kann nicht verwendet werden, um die Funktion mit dem Namen Camel zu benennen verwendet werden;

Parameter: Zur Bereitstellung von Daten für die Funktion wird zwischen formalen Parametern und tatsächlichen Parametern unterschieden.

Ausführungsanweisung: Wird auch als Funktionskörper bezeichnet und zum Ausführen einer Reihe verwendet von logischen Operationen;

Rückgabewert: Nach der Ausführung der Funktion werden die an den Aufrufer zurückgegebenen Daten standardmäßig auf „Keine“ gesetzt. Wenn also kein Rückgabewert vorhanden ist, müssen Sie keine Rückgabe schreiben.

2. Gewöhnliche Parameter der Funktion

Parameter mit der direktesten Eins-zu-Eins-Beziehung, wie zum Beispiel:


def fun_ex(a,b):            #a,b是函数fun_ex的形式参数,也叫形参
    sum=a+b    print('sum =',sum)
fun_ex(1,3)                  #1,3是函数fun_ex的实际参数,也叫实参#运行结果sum = 4

3. Standardparameter der Funktion

Definieren Sie einen Standardwert für den Parameter aufgerufen wird, keine Parameter angegeben sind, verwendet die Funktion Standardparameter, die am Ende der Parameterliste platziert werden müssen, wie zum Beispiel:


def fun_ex(a,b=6):    #默认参数放在参数列表最后,如b=6只能在a后面
    sum=a+b    print('sum =',sum)
fun_ex(1,3)
fun_ex(1)#运行结果sum = 4sum = 7

4. Funktion Dynamische Parameter

müssen nicht angeben, ob der Parameter ein Tupel oder Wörterbuch ist, die Funktion konvertiert ihn automatisch in ein Tupel oder Wörterbuch, wie zum Beispiel:

#转换成元组的动态参数形式,接受的参数需要是可以转成元组的形式,就是类元组形式的数据,如数值,列表,元组。

def func(*args):
    print(args,type(args))

func(1,2,3,4,5)

date_ex1=('a','b','c','d')
func(*date_ex1)

#运行结果
(1, 2, 3, 4, 5) <class &#39;tuple&#39;>
(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;) <class &#39;tuple&#39;>

动态参数形式一
#转换成字典的动态参数形式,接收的参数要是能转换成字典形式的,就是类字典形式的数据,如键值对,字典

def func(**kwargs):
    print(kwargs,type(kwargs))

func(a=11,b=22)

date_ex2={&#39;a&#39;:111,&#39;b&#39;:222}
func(**date_ex2)

#运行结果
{&#39;b&#39;: 22, &#39;a&#39;: 11} <class &#39;dict&#39;>
{&#39;b&#39;: 222, &#39;a&#39;: 111} <class &#39;dict&#39;>

动态参数形式二
#根据传的参数转换成元组和字典的动态参数形式,接收的参数可以是任何形式。
def func(*args,**kwargs):
    print(args, type(args))
    print(kwargs,type(kwargs))

func(123,321,a=999,b=666)

date_ex3={&#39;a&#39;:123,&#39;b&#39;:321}
func(**date_ex3)

#运行结果
(123, 321) <class &#39;tuple&#39;>
{&#39;b&#39;: 666, &#39;a&#39;: 999} <class &#39;dict&#39;>
() <class &#39;tuple&#39;>
{&#39;b&#39;: 321, &#39;a&#39;: 123} <class &#39;dict&#39;>

动态参数形式三
5. Der Rückgabewert der Funktion

Wenn Sie eine Funktion ausführen, müssen Sie im Allgemeinen einige Informationen daraus abrufen. In diesem Fall müssen Sie return verwenden, um den Rückgabewert abzurufen, z. B.:

def fun_ex(a,b):
    sum=a+b
    return sum      #返回sum值

re=fun_ex(1,3)   
print(&#39;sum =&#39;,re)

#运行结果
sum = 4

6.lambda-Ausdruck

ist Wird verwendet, um einfache Funktionen auszudrücken, wie zum Beispiel:

#普通方法定义函数
def sum(a,b):
    return a+b
sum=sum(1,2)
print(sum)

#lambda表达式定义函数
myLambda = lambda a,b : a+b
sum=myLambda(2,3)
print(sum)

#运行结果
5

7. Integrierte Funktionen
1) Liste der integrierten Funktionen

dict()dir()hex()id()input()oct()open() <td>str()<span style="font-size: 15px"><code class="xref py py-func docutils literal"><span class="pre"></span> bool()exec()isinstance()ord ()sum()bytearray()filter()issubclass()pow()<span class="pre">iter()</span><span class=" pre">tuple()</span><td><span style="font-size: 15px"><code class="xref py py-func docutils literal"><span class="pre">callable()</span><span class="pre">len()</span><span class="pre">property()<tr class="row-odd"> <td><span style="font-size: 15px"><code class="xref py py-func docutils literal"><span class="pre">chr()</span><span class="pre">type()</span><td><span style="font-size: 15px"><code class="docutils literal"><span class="pre">frozenset()</span><span class="pre">list ()</span><span class="pre">vars()</span><span class="pre">zip()</span><span class="pre">__import__()</span><span class="pre">hasattr()</span><span class="pre"> Round()</span>
    Eingebaute Funktionen    
<span class="pre">abs()</span>abs() <span class="pre">dict()</span> <span class="pre">help()</span> help()<span class="pre">min()</span> <span class="pre">setattr()</span>min()
<span class="pre">all()</span>setattr() <span class="pre">dir()</span> <span class="pre">hex()</span>all() <span class="pre">next()</span> <span class="pre">slice()</span>
<span class="pre">any()</span> next()<span class="pre">pmod()</span> <span class="pre">id()</span>slice() <span class="pre">object()</span> <span class="pre">sorted()</span>any()
<span class="pre">ascii()</span>pmod () <span class="pre">enumerate()</span><span class="pre">input()</span> object()<span class="pre">oct()</span> <span class="pre">staticmethod()</span>sorted()
<span class="pre">bin()</span> ascii()<span class="pre">eval()</span> <span class="pre">int()</span>enumerate() <span class="pre">open()</span><span class="pre">str()</span>
<span class="pre">bool()</span> staticmethod()<span class="pre">exec()</span> <span class="pre">isinstance()</span> bin()<span class="pre">ord()</span> <span class="pre">sum()</span>eval()
<span class="pre">bytearray()</span>int() <span class="pre">filter()</span><span class="pre">issubclass()</span> <span class="pre">super()<code class="xref py py-func docutils literal"><span class="pre">super()</span>
<span class="pre">bytes()</span><span class="pre">bytes()</span> <span class="pre">float()</span><span class="pre">float()</span> <span class="pre">iter()</span> print() <span class="pre">print()<span style="font-size: 15px"><code class="docutils literal"><span class="pre">tuple()</span>
<span class="pre">format()</span><span class="pre ">callable()</span> <span class="pre">len()</span><span class="pre">format()</span> <span class="pre">property()</span> type()
<span class="pre">list()</span><span class="pre">chr()</span> <span class="pre">range()</span><span class="pre">frozenset()</span> <span class="pre">vars()</span>
<span class="pre">classmethod()</span><span class="pre">range()</span> <span class="pre">getattr()</span> <span class="pre">locals()</span> <span class="pre">repr()</span><span class="pre">classmethod()</span> <span class="pre">zip()</span> <span class="pre">getattr()</span>
<span class="pre">compile()</span><span class="pre">locals()</span> <span class="pre">globals()</span> <span class="pre">repr()</span> <span class="pre">map()</span> <span class="pre">reversed()</span> <span class="pre">__import__()</span><span class="pre"> compile()</span>
<span class="pre">globals()<code class="xref py py-func docutils literal"><span class="pre">complex()</span> <span class="pre">hasattr()</span><span class="pre">map()</span> <span class="pre">max()</span><span class="pre">reversed()</span> <span class="pre">round()</span>
<span class="pre">delattr()</span>complex() <span class="pre">hash()</span> memoryview() max()<span style="font-size: 15px"><code class="docutils literal"><span class="pre">set()</span>

 <span class="pre">delattr()<code class="xref py py-func docutils literal"><span class="pre">hash( )<code class="docutils literal"><span class="pre">memoryview()<code class="docutils literal"><span class="pre">set()  </span>

Das obige ist der detaillierte Inhalt vonAusführliche Erläuterung der Verwendung von Python-Funktionen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn