程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Functions for getting started with Python

編輯:Python

python It is a very practical technology , Today, I'd like to briefly introduce python The function in .

First , Function is an integration of algorithms , Easy to be called in multiple places .

Two : Fixed format of function :、

The function is displayed according to the specific algorithm , But there is a fixed format ;、

deffun_name( var):

# Specific algorithm

return result

The result given to the caller after the function finishes execution

   return, As long as the implementation of the return This call to the function will be stopped

   1. If the function doesn't write return, no return value . What was received was None

   2. Yes return. return It doesn't write anything or return The back is None. return None

   3.return A value . Return a result

   Parameters :  Information passed to the function when the function is called  

  Shape parameter :  Variables in the position of the function declaration

    Actual parameters :  The specific value given in the function call    

    The ginseng :  A procedure that assigns an argument to a formal parameter during a function call .

    Actual parameters :

       1. Positional arguments . Pass information to formal parameters by position

       2. Key parameters . Pass information to the parameter according to the name of the parameter

       3. Mixing parameters . First position, then keyword

   Shape parameter :

       1. Positional arguments

       2. Default parameter , Must be written after the position parameter

       3. Dynamic parameters

for example :

1 The determinants of arguments :

1.  # Assign values to formal parameters according to their positions

def food(breakfast,lunch,dinner):

   print(" I want to eat ",breakfast,lunch,dinner)

food(" milk "," The steak "," Apple ") # I want to eat milk The steak Apple

2.   Assign a value to the parameter according to its name

def food(breakfast,lunch,dinner):

   print(" I want to eat ",breakfast,lunch,dinner)

food(breakfast=" milk ",lunch=" The steak ",dinner=" Apple ")# I want to eat milk The steak Apple

2 List of formal parameters :

   Declare parameters by position , Just like arguments

def information(name,age,sex):

   print(name,age,sex)

information(" Xiaohong ","29"," Woman ")

information(" Xiaomei ","1"," Woman ")

information(" Xiao Ming ","29"," male ")

information(" aunt ","43"," Woman ")

​ When passing values to parameters , No value given , Default values work . Make sure you have at least one value to use . Give a worthy word , The default value does not work . The order : The position parameter must be placed first .  The default value parameter must be placed later .

def information(name,age,sex=" Woman "):

   print(name,age,sex)

information(" Xiaohong ","29",)

information(" Xiaomei ","1",)

information(" Xiao Ming ","29"," male ")

information(" aunt ","43",)    

The result at this time is similar to 1 The results of the position parameters are consistent , Use for input that is consistent in most cases , Special separate treatment is enough .

  Dynamic parameters , Used when parameters are uncertain .

​  def fun (*args)

fun(args)

The Works of Liezi 1. If the parameters are uncertain , The following list , Each person eats differently , Different kinds of food , In this case, dynamic parameter transfer is used .

def fun(*food):

 #* It represents an indefinite parameter , You can pass any message , The parameter name is still food, And the received information is Yuanzu ()

   print(" I want to eat ",food)

fun(" meat ")    #* Represents the dynamic transfer of position parameters     As a result, I want to eat ( meat ',)

fun(" Steamed Rice "," Spicy strips "," instant noodles ")# I want to eat (' Steamed Rice ', ' Spicy strips ', ' instant noodles ')

The Works of Liezi 2. The dynamic parameter must be after the position parameter .

def fun (*fruit,a,b):

    print(" I want to eat ",fruit,a,b)

fun(" Apple "," pears "," Banana ")  # At this time, the program will report an error , All by food To receive the ,a and b Never receive parameters .

def fun (a,b,*fruit):

    print(" I want to eat ",a,b,fruit)

fun(" Mango. "," pears "," Banana "," The oranges ")  # I want to eat Mango. pears (' Banana ', ' The oranges ')


All right. , I'll introduce you here today , Continue another day .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved