Python入门基础:函数知识点(一):简单基础类型

本文介绍了Python函数的基础知识,包括函数的意义、定义、返回值、参数传递方式、默认参数、关键字参数、可变长度参数及匿名函数(lambda)。通过实例解析了Python中参数的值传递特性,并给出了相关代码示例。

函数是一组语句,它们接受输入、进行一些特定的计算并产生输出。

函数存在的意义:

 1. 提高代码的复用性
 2. 将复杂的逻辑简单化,分功能化

 

函数定义:

小知识 : []表示可选  <>表示必备

函数名字最好能直接表示该函数的功能,多个单词之间用_链接

<def> <函数名>([参数列表])<:>
# 待执行语句
# 如果有需要显式返回<return 标识符>或<return>

Python提供了内置函数,如print()等。用户定义函数:可以创建自己的函数

# A simple Python function to check
# whether x is even or odd
def evenOdd( x ):
    if (x % 2 == 0):
        print "even"
    else:
        print "odd"
  
# Driver code
evenOdd(2)
evenOdd(3)

产出:

even
odd

函数的返回值

函数可以返回任何类型的数据,函数内执行到return后结束,后面代码不再执行.

def func():
    res = 1+1
    print('我执行了')
    return res
    print('我不会执行')

print('程序正常执行')
# 函数内的语句确实是执行了,打印我执行了
func()
# 想要拿到函数return的值需要重新找个变量接收一下
# 注意这里再次打印了一次我执行了  因为函数再次调用了一次
res = func()
print(res)
# 不写return默认返回None
def func():
    print('我执行了')

res = func()
print(res)

多个返回值

多个变量接收

def func():  return 1,2,3
a,b,c = func()print(a,b,c)

输出:

一个变量接收是一个元组

def func():  return 1,2,3
res = func()print(res)

输出:

参考传递还是价值传递?

注意:在Python中,每个变量名都是引用。

当我们将变量传递给函数时,将创建对对象的新引用。

Python中传递的参数与Java中传递的引用相同。

# Here x is a new reference to same list lst
def myFun(x):
   x[0] = 20
  
# Driver Code (Note that lst is modified
# after function call.
lst = [10, 11, 12, 13, 14, 15] 
myFun(lst);
print(lst) 

产出:

[20, 11, 12, 13, 14, 15]

当我们传递引用并将接收到的引用更改为其他引用时,传递参数和接收参数之间的连接就会中断。

例如,考虑下面的程序。

def myFun(x):
  
   # After below line link of x with previous
   # object gets broken. A new object is assigned
   # to x.
   x = [20, 30, 40]
  
# Driver Code (Note that lst is not modified
# after function call.
lst = [10, 11, 12, 13, 14, 15] 
myFun(lst);
print(lst) 

产出:

[10, 11, 12, 13, 14, 15]

如果我们分配一个新值(在函数中),则另一个示例演示引用链接被破坏。

def myFun(x):
  
   # After below line link of x with previous
   # object gets broken. A new object is assigned
   # to x.
   x = 20
  
# Driver Code (Note that lst is not modified
# after function call.
x = 10 
myFun(x);
print(x) 

产出:

10

练习:尝试猜测以下代码的输出。

def swap(x, y):
    temp = x;
    x = y;
    y = temp;
  
# Driver code
x = 2
y = 3
swap(x, y)
print(x)
print(y)

产出:

2
3

关键字传参(参数名对应)

def sue_for_peace(num1,num2):    print(num1,num2)    res = num1+num2    return res
print(sue_for_peace(num2=3,num1 = 4))

混合使用(先位置参数再关键字参数)

def sue_for_peace(num1,num2,num3):
    print(num1,num2,num3)
    res = num1+num2+num3
    return res

print(sue_for_peace(3,num3=1,num2 = 2))

默认参数:

默认参数是一个参数,如果该参数的函数调用中没有提供一个值,则该参数假定为默认值。

# Python program to demonstrate
# default arguments
def myFun(x, y=50):
    print("x: ", x)
    print("y: ", y)
  
# Driver code (We call myFun() with only
# argument)
myFun(10)

产出:

('x: ', 10)
('y: ', 50)

与C++默认参数一样,函数中的任意数量的参数都可以具有默认值。

但是一旦有了默认参数,它右边的所有参数也必须有默认值。

混合使用时,先按位置传递参数,后按关键字传递参数,最后是默认值

错误示范:

def sue_for_peace(num1=1,num2,num3): # 第一行这里就错了,默认值只能再最后,编辑器报错
    print(num1,num2,num3)
    res = num1+num2+num3
    return res

print(sue_for_peace(2,3))
def sue_for_peace(num1, num2, num3=3):
    print(num1, num2, num3)
    res = num1+num2+num3
    return res

print(sue_for_peace(num2=2,3)) # 先位置后关键字,编辑器报错

print(sue_for_peace(1,num1=2)) # 不允许给同一个形参传两个值

关键词参数:

允许调用者用值指定参数名,就不需要记住参数的顺序。

# Python program to demonstrate Keyword Arguments
def student(firstname, lastname): 
     print(firstname, lastname) 
    
    
# Keyword arguments                  
student(firstname ='Geeks', lastname ='Practice')    
student(lastname ='Practice', firstname ='Geeks')  

产出:

('Geeks', 'Practice')
('Geeks', 'Practice')

可变长度参数:

# Python program to illustrate  
# *args for variable number of arguments
def myFun(*argv): 
    for arg in argv: 
        print (arg)
    
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks') 

产出:

Hello
Welcome
to
GeeksforGeeks
# Python program to illustrate  
# *kargs for variable number of keyword arguments
  
def myFun(**kwargs): 
    for key, value in kwargs.items():
        print ("%s == %s" %(key, value))
  
# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')    

产出:

last == Geeks
mid == for
first == Geeks

匿名功能:在Python中,匿名函数意味着函数没有名称。正如我们已经知道的,def关键字用于定义普通函数,lambda关键字用于创建匿名函数。请看这关于细节。

# Python code to illustrate cube of a number  
# using labmda function 
    
cube = lambda x: x*x*x 
print(cube(7)) 

产出:

343

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值