乐高积木之函数
函数体
return 返回值1,返回值2
实现打印返回值: print 函数名()
总结:
定义函数时,函数不执行;
调用函数时, 函数才执行;
In [2]: # 形式参数
def add(x, y):
print x + y
# 实参 , x=1, y=2
add(1 2)


print x**y
mypow(2)
# args 可以改为其他变量名;
def add(*args):
# args 实质上是一个元组 ;
# print args
sum = 0
for i in args:
sum += i
print sum
# 实参 , x=1, y=2
def inuser(name, age, **kwargs):
# kwargs 实质上是一个字典 ;
print name, name, kwargs
inuser("user1"
12
city="xi'an"
(2)返回值
1、函数中如果没有return时, 默认返回None;
def add(x,y):
return x+y
# return None
print add(1, 2)
3
None
"""
返回最大值和最小值
:param args: 传入多个数值
:return: 最大值, 最小值
# 实质上 python 只能返回一个值;
# 间接通过元组返回多个值 ;
# 全局变量
num = 1
def fun():
# global 声明 num 为全局变量
global num
# 局部变量
num = 5
fun()
迭代
是否可以for循环遍历的对象;
isinstance判断是否可迭代;
# In [4]: from collections import Iterable
# In [5]: isinstance("hello", Iterable)
# Out[5]: True
# In [6]: isinstance([1,2,3,4], Iterable)
# Out[6]: True
# In [7]: isinstance((1,2,3,4), Iterable)
# Out[7]: True
# In [8]: isinstance({'a':1}, Iterable)
# Out[8]: True
# In [9]: isinstance({1,2,3}, Iterable)
# Out[9]: True
需求: 生成一个列表, 返回1-100中偶数的平方;
[4, 16, 36.......]
3 of 4
01/06/2018 05:16 PM函数操作
http://localhost:8890/notebooks/2018_01_pytho...
In [14]: # 方法 1 :
li = []
for i in range(2,20,2):
li.append(i**2)
print li
[4, 16, 36, 64, 100, 144, 196, 256, 324]
In [16]: # 方法 2 :
[i**2 for i in range(2 20 2)]
Out[16]: [4, 16, 36, 64, 100, 144, 196, 256, 324]
变异的列表生成式
In [17]: # for 循环嵌套 if 语句
[i**2 for i in range(2,20) if i%2==0]
Out[17]: [4, 16, 36, 64, 100, 144, 196, 256, 324]
In [20]: # for 循环嵌套 for 循环 , 两个字符串的全排列
[i+j for i in 'xyz' for j in '123']
Out[20]: ['x1', 'x2', 'x3', 'y1', 'y2', 'y3', 'z1', 'z2', 'z3']
- os.listdir("/etc")
- s.enswith(".conf")
import os
一、函数的定义
def 函数名():函数体
return 返回值1,返回值2
定义一个空函数
• 定义一个什么事也不做的空函数,可以用 pass 语句;
• pass 可以用来作为占位符,还没想好怎么写函数的代码,
就可以先放一个 pass ,让代码能运行起来
def nofunc():
pass
二、函数的调用
函数名()实现打印返回值: print 函数名()
总结:
定义函数时,函数不执行;
调用函数时, 函数才执行;
(1)有参数的函数
必选参数,>默认参数, > 可变参数, > 关键字参数
In [2]: # 形式参数
def add(x, y):
print x + y
# 实参 , x=1, y=2
add(1 2)
2、默认参数
In [ ]: def mypow(x,y=2):print x**y
mypow(2)
3、可变参数
# 形式参数# args 可以改为其他变量名;
def add(*args):
# args 实质上是一个元组 ;
# print args
sum = 0
for i in args:
sum += i
print sum
# 实参 , x=1, y=2
add(1 2 3 4 5 6 7)
4、关键字参数
# kwargs 可以改为其他变量名;def inuser(name, age, **kwargs):
# kwargs 实质上是一个字典 ;
print name, name, kwargs
inuser("user1"
12
city="xi'an"
birth="20180101")
(2)返回值
1、函数中如果没有return时, 默认返回None;
def add(x,y):
return x+y
# return None
print add(1, 2)
3
None
2、返回多个值
def fun(*args):"""
返回最大值和最小值
:param args: 传入多个数值
:return: 最大值, 最小值
# 实质上 python 只能返回一个值;
# 间接通过元组返回多个值 ;
"""
return max(args), min(args)
print fun(91 2 3 12 34)3、函数的作用域
global关键字必须要先声明, 再赋值;# 全局变量
num = 1
def fun():
# global 声明 num 为全局变量
global num
# 局部变量
num = 5
fun()
print num
4、高级特性
切片迭代
是否可以for循环遍历的对象;
isinstance判断是否可迭代;
# In [4]: from collections import Iterable
# In [5]: isinstance("hello", Iterable)
# Out[5]: True
# In [6]: isinstance([1,2,3,4], Iterable)
# Out[6]: True
# In [7]: isinstance((1,2,3,4), Iterable)
# Out[7]: True
# In [8]: isinstance({'a':1}, Iterable)
# Out[8]: True
# In [9]: isinstance({1,2,3}, Iterable)
# Out[9]: True
列表生成式
生成列表的公式需求: 生成一个列表, 返回1-100中偶数的平方;
[4, 16, 36.......]
3 of 4
01/06/2018 05:16 PM函数操作
http://localhost:8890/notebooks/2018_01_pytho...
In [14]: # 方法 1 :
li = []
for i in range(2,20,2):
li.append(i**2)
print li
[4, 16, 36, 64, 100, 144, 196, 256, 324]
In [16]: # 方法 2 :
[i**2 for i in range(2 20 2)]
Out[16]: [4, 16, 36, 64, 100, 144, 196, 256, 324]
变异的列表生成式
In [17]: # for 循环嵌套 if 语句
[i**2 for i in range(2,20) if i%2==0]
Out[17]: [4, 16, 36, 64, 100, 144, 196, 256, 324]
In [20]: # for 循环嵌套 for 循环 , 两个字符串的全排列
[i+j for i in 'xyz' for j in '123']
Out[20]: ['x1', 'x2', 'x3', 'y1', 'y2', 'y3', 'z1', 'z2', 'z3']
练习:找出/etc下文件中以.conf结尾的文件;
提示:- os.listdir("/etc")
- s.enswith(".conf")
import os
print [i for i in os listdir('/etc') if i endswith(".conf")][:5]
未完待续。。。。。。