python学习第三天 -----2019年4月23日
第三周-第03章节-Python3.5-集合及其运算
集合是一个无序的,不重复的数据组合,它的主要作用如下:
- 去重,把一个列表变成集合,就自动去重了
- 关系测试,测试两组数据之前的交集、差集、并集等关系
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: 集合.py
@time: 2019/05/01
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
list_1=[1,4,5,7,3,6,7,9]
list_1=set(list_1)
list_2=set([2,6,0.66,22,8,4])
print(list_1,list_2)
#交集
re1=list_1.intersection(list_2)
print("=========>交集",re1)
#并集
re2=list_1.union(list_2)
print("=========>并集",re2)
#差集 in list_1 but not in list_2
re3=list_1.difference(list_2)
print("=========>差集",re3)
re4=list_2.difference(list_1)
print(re4)
#子集
re5=list_1.issubset(list_2)
print("=========>子集",re5)
list_3 = set([1,3,7])
re7=list_3.issubset(list_1)
re8=list_1.issuperset(list_3)
print(re7,re8)
#父集
re6=list_1.issuperset(list_2)
print("=========>父集",re6)
#反向差集/对称差集
re9=list_1.symmetric_difference(list_2)
print("=========>反向差集/对称差集",re9)
list_4 = set([5,6,7,8])
re10=list_3.isdisjoint(list_4)
print(re10)
================================================================================================
G:\Python3.7.3\python.exe G:/practise/S14/day3/集合.py
{1, 3, 4, 5, 6, 7, 9} {0.66, 2, 4, 6, 8, 22}
=========>交集 {4, 6}
=========>并集 {0.66, 1, 2, 3, 4, 5, 6, 7, 8, 9, 22}
=========>差集 {1, 3, 5, 7, 9}
{0.66, 8, 2, 22}
=========>子集 False
True True
=========>父集 False
=========>反向差集/对称差集 {0.66, 1, 2, 3, 5, 7, 8, 9, 22}
False
Process finished with exit code 0
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: 集合.py
@time: 2019/05/01
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
list_1=[1,4,5,7,3,6,7,9]
list_1=set(list_1)
list_2=set([2,6,0,66,22,8,4])
print(list_1,list_2)
#交集
print("================>交集",list_1 & list_2)
#并集
print("================>并集",list_1 | list_2)
#差集
print("================>差集",list_1-list_2)
#对称差集
print("================>对称差集",list_1 ^ list_2)
#集合的添加(一项)
list_1.add(9999)
print(list_1)
#集合的添加(多项)
list_1.update([222,333,444])
print(list_1)
===============================================================================================
G:\Python3.7.3\python.exe G:/practise/S14/day3/集合.py
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
================>交集 {4, 6}
================>并集 {0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22}
================>差集 {1, 3, 5, 7, 9}
================>对称差集 {0, 1, 2, 66, 3, 5, 8, 7, 9, 22}
{1, 3, 4, 5, 6, 7, 9, 9999}
{1, 3, 4, 5, 6, 7, 9, 333, 9999, 444, 222}
Process finished with exit code 0
第三周-第04章节-Python3.5-文件读与写详解1
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: 文件操作.py
@time: 2019/05/01
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
###打开文件并赋值给data
#data=open("yesterday").read()
#print(data)
#########读文件,一遍读完了之后就不会从头开始读了
f=open("yesterday") #文件句柄
data = f.read()
data2 = f.read()
print("#########data############",data)
print("#########data2############",data2)
####################################################################################33
G:\Python3.7.3\python.exe G:/practise/S14/day3/文件操作.py
#########data############ Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed
我曾千万次梦见
The splendid things I planned
那些我计划的绚丽蓝图
I always built to last on weak and shifting sand
但我总是将之建筑在易逝的流沙上
I lived by night and shunned the naked light of day
我夜夜笙歌 逃避白昼赤裸的阳光
And only now I see how the time ran away
事到如今我才看清岁月是如何匆匆流逝
Yesterday when I was young
昨日当我年少轻狂
So many lovely songs were waiting to be sung
有那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
I ran so fast that time and youth at last ran out
我飞快地奔走 最终时光与青春消逝殆尽
I never stopped to think what life was all about
我从未停下脚步去思考生命的意义
And every conversation that I can now recall
如今回想起的所有对话
Concerned itself with me and nothing else at all
除了和我相关的 什么都记不得了
The game of love I played with arrogance and pride
我用自负和傲慢玩着爱情的游戏
And every flame I lit too quickly, quickly died
所有我点燃的火焰都熄灭得太快
The friends I made all somehow seemed to slip away
所有我交的朋友似乎都不知不觉地离开了
And only now I'm left alone to end the play, yeah
只剩我一个人在台上来结束这场闹剧
Oh, yesterday when I was young
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that won't be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂
#########data2############
###文件写入 write 可写不可读
f1 = open("yesterday2","w")
f1.write("我爱北京天安门,\n")
f1.write("天安门上太阳升\n")
###文件追加 append 可追加不可读
f1 = open("yesterday2","a")
f1.write("我爱北京天安门.........\n")
f1.write("天安门上太阳升........................")
f1.close()
=========================================================================
yesterday2内容:
我爱北京天安门,
天安门上太阳升
我爱北京天安门.........
天安门上太阳升........................
第三周-第05章节-Python3.5-文件读与写详解2
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: 文件操作.py
@time: 2019/05/01
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
'''
####文件读取 读取前五行
f = open("yesterday","r")
for i in range(5):
print(f.readline())
'''
'''
####循环整个文件,用readlines
f = open("yesterday","r")
for line in f.readlines():
print(line.strip())
'''
###第十行打印分割线,enumerate取得下标
for index,line in enumerate(f.readlines()):
if index == 9:
print("===========我是分割线===============")
continue
print(line.strip())
===========================================================================
G:\Python3.7.3\python.exe G:/practise/S14/day3/文件操作.py
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
===========我是分割线===============
我曾千万次梦见
The splendid things I planned
那些我计划的绚丽蓝图
I always built to last on weak and shifting sand
但我总是将之建筑在易逝的流沙上
I lived by night and shunned the naked light of day
我夜夜笙歌 逃避白昼赤裸的阳光
And only now I see how the time ran away
事到如今我才看清岁月是如何匆匆流逝
Yesterday when I was young
昨日当我年少轻狂
So many lovely songs were waiting to be sung
有那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
I ran so fast that time and youth at last ran out
我飞快地奔走 最终时光与青春消逝殆尽
I never stopped to think what life was all about
我从未停下脚步去思考生命的意义
And every conversation that I can now recall
如今回想起的所有对话
Concerned itself with me and nothing else at all
除了和我相关的 什么都记不得了
The game of love I played with arrogance and pride
我用自负和傲慢玩着爱情的游戏
And every flame I lit too quickly, quickly died
所有我点燃的火焰都熄灭得太快
The friends I made all somehow seemed to slip away
所有我交的朋友似乎都不知不觉地离开了
And only now I'm left alone to end the play, yeah
只剩我一个人在台上来结束这场闹剧
Oh, yesterday when I was young
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that won't be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂
Process finished with exit code 0
效率最高的写法:
f = open("yesterday","r")
for line in f:
print("=========line=======>",line.strip())
===============================================================================
G:\Python3.7.3\python.exe G:/practise/S14/day3/文件操作.py
=========line=======> Somehow, it seems the love I knew was always the most destructive kind
=========line=======> 不知为何,我经历的爱情总是最具毁灭性的的那种
=========line=======> Yesterday when I was young
=========line=======> 昨日当我年少轻狂
=========line=======> The taste of life was sweet
=========line=======> 生命的滋味是甜的
=========line=======> As rain upon my tongue
=========line=======> 就如舌尖上的雨露
=========line=======> I teased at life as if it were a foolish game
=========line=======> 我戏弄生命 视其为愚蠢的游戏
=========line=======> The way the evening breeze
=========line=======> 就如夜晚的微风
=========line=======> May tease the candle flame
=========line=======> 逗弄蜡烛的火苗
=========line=======> The thousand dreams I dreamed
=========line=======> 我曾千万次梦见
=========line=======> The splendid things I planned
=========line=======> 那些我计划的绚丽蓝图
=========line=======> I always built to last on weak and shifting sand
=========line=======> 但我总是将之建筑在易逝的流沙上
=========line=======> I lived by night and shunned the naked light of day
=========line=======> 我夜夜笙歌 逃避白昼赤裸的阳光
=========line=======> And only now I see how the time ran away
=========line=======> 事到如今我才看清岁月是如何匆匆流逝
=========line=======> Yesterday when I was young
=========line=======> 昨日当我年少轻狂
=========line=======> So many lovely songs were waiting to be sung
=========line=======> 有那么多甜美的曲儿等我歌唱
=========line=======> So many wild pleasures lay in store for me
=========line=======> 有那么多肆意的快乐等我享受
=========line=======> And so much pain my eyes refused to see
=========line=======> 还有那么多痛苦 我的双眼却视而不见
=========line=======> I ran so fast that time and youth at last ran out
=========line=======> 我飞快地奔走 最终时光与青春消逝殆尽
=========line=======> I never stopped to think what life was all about
=========line=======> 我从未停下脚步去思考生命的意义
=========line=======> And every conversation that I can now recall
=========line=======> 如今回想起的所有对话
=========line=======> Concerned itself with me and nothing else at all
=========line=======> 除了和我相关的 什么都记不得了
=========line=======> The game of love I played with arrogance and pride
=========line=======> 我用自负和傲慢玩着爱情的游戏
=========line=======> And every flame I lit too quickly, quickly died
=========line=======> 所有我点燃的火焰都熄灭得太快
=========line=======> The friends I made all somehow seemed to slip away
=========line=======> 所有我交的朋友似乎都不知不觉地离开了
=========line=======> And only now I'm left alone to end the play, yeah
=========line=======> 只剩我一个人在台上来结束这场闹剧
=========line=======> Oh, yesterday when I was young
=========line=======> 噢 昨日当我年少轻狂
=========line=======> So many, many songs were waiting to be sung
=========line=======> 有那么那么多甜美的曲儿等我歌唱
=========line=======> So many wild pleasures lay in store for me
=========line=======> 有那么多肆意的快乐等我享受
=========line=======> And so much pain my eyes refused to see
=========line=======> 还有那么多痛苦 我的双眼却视而不见
=========line=======> There are so many songs in me that won't be sung
=========line=======> 我有太多歌曲永远不会被唱起
=========line=======> I feel the bitter taste of tears upon my tongue
=========line=======> 我尝到了舌尖泪水的苦涩滋味
=========line=======> The time has come for me to pay for yesterday
=========line=======> 终于到了付出代价的时间 为了昨日
=========line=======> When I was young
=========line=======> 当我年少轻狂
Process finished with exit code 0
###取出第十行
count = 0
f = open("yesterday","r")
for line in f:
if count == 9:
print("=========分割线=======>")
count += 1
continue
print(line.strip())
count +=1
====================================================================================
G:\Python3.7.3\python.exe G:/practise/S14/day3/文件操作.py
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
=========分割线=======>
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed
我曾千万次梦见
The splendid things I planned
那些我计划的绚丽蓝图
I always built to last on weak and shifting sand
但我总是将之建筑在易逝的流沙上
I lived by night and shunned the naked light of day
我夜夜笙歌 逃避白昼赤裸的阳光
And only now I see how the time ran away
事到如今我才看清岁月是如何匆匆流逝
Yesterday when I was young
昨日当我年少轻狂
So many lovely songs were waiting to be sung
有那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
I ran so fast that time and youth at last ran out
我飞快地奔走 最终时光与青春消逝殆尽
I never stopped to think what life was all about
我从未停下脚步去思考生命的意义
And every conversation that I can now recall
如今回想起的所有对话
Concerned itself with me and nothing else at all
除了和我相关的 什么都记不得了
The game of love I played with arrogance and pride
我用自负和傲慢玩着爱情的游戏
And every flame I lit too quickly, quickly died
所有我点燃的火焰都熄灭得太快
The friends I made all somehow seemed to slip away
所有我交的朋友似乎都不知不觉地离开了
And only now I'm left alone to end the play, yeah
只剩我一个人在台上来结束这场闹剧
Oh, yesterday when I was young
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that won't be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂
Process finished with exit code 0
第三周-第06章节-Python3.5-文件读与写详解3
动态进度条:
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: 进度条.py
@time: 2019/05/01
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
import sys,time
for i in range(100):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)
============================================================================================
G:\Python3.7.3\python.exe G:/practise/S14/day3/进度条.py
####################################################################################################
Process finished with exit code 0
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:chenjisong
@file: 进度条.py
@time: 2019/05/01
url:https://www.liaoxuefeng.com
functions:
Software:JetBrains PyCharm 4.5.3
"""
with open("yesterday","r") as f:
print(f.readline())
####################################################################################################
G:\Python3.7.3\python.exe G:/practise/S14/day3/文件操作.py
Somehow, it seems the love I knew was always the most destructive kind
Process finished with exit code 0
第三周-第11章节-Python3.5-函数与函数式编程1
example:func_test1
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: func_test1.py
@time: 2019/05/12
功能:
笔记:1、面向对象:华山派----》类----》class
2、面向过程:少林派----》过程----》def
3、函数式编程:逍遥派----》函数----》def
y=2x
过程是没有返回值的函数
"""
###定义了一个过程
def func1():
'''
testing1
'''
print("in the func1")
return 0
###过程
def func2():
"testing2"
print("in the func2")
x = func1()
y = func2()
print("from func1 return is %s" %x)
print("from func2 return is %s" %y)
=================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/func_test1.py
in the func1
in the func2
from func1 return is 0
from func2 return is None
Process finished with exit code 0
example:func_test2
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: func_test2.py
@time: 2019/05/12
功能:
笔记:
"""
####第一种方法
import time
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
#with open("b.txt","a+") as f:
# f.write("%s end action\n" %time_current)
def test1():
print("test1 starting action...")
with open("b.txt","a+") as f:
f.write("%s test1 end action\n" %time_current)
def test2():
print("test2 starting action...")
with open("b.txt","a+") as f:
f.write("%s test2 end action\n" %time_current)
def test3():
print("test3 starting action...")
with open("b.txt","a+") as f:
f.write("%s test3 end action\n" %time_current)
test1()
test2()
test3()
========================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/func_test2.py
test1 starting action...
test2 starting action...
test3 starting action...
Process finished with exit code 0
example:func_test3
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: func_test3.py
@time: 2019/05/12
功能: 1、代码重用
2、保持一致性
3、可扩展
笔记:func_test2和fun_test3相比,test3封装了函数,代码行数明显减少
"""
######第二种方法
import time
def logger():
time_format = "%Y-%m-%d %X"
time_current = time.strftime(time_format)
with open("a.txt","a+") as f:
f.write("%s end action\n" %time_current)
def test1():
print("test1 starting action...")
logger()
def test2():
print("test2 starting action...")
logger()
def test3():
print("test3 starting action...")
logger()
test1()
test2()
test3()
======================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/func_test3.py
test1 starting action...
test2 starting action...
test3 starting action...
Process finished with exit code 0
example:func_test4
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: func_test4.py
@time: 2019/05/12
功能:
笔记:函数可以返回字符串,列表,元祖,字典,还可以多从返回
"""
def test1():
print("in the test1")
def test2():
print("in the test2")
return 0
def test3():
print("in the test3")
return 1,"hello",["a","b","c"],{"name":"alex"}
x=test1()
y=test2()
z=test3()
print(x)
print(y)
print(z)
===========================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/func_test4.py
in the test1
in the test2
in the test3
None
0
(1, 'hello', ['a', 'b', 'c'], {'name': 'alex'})
Process finished with exit code 0
func_test5
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: func_test5.py
@time: 2019/05/12
功能:
笔记:形参:形式参数,不占用内存空间
实参:实际参数,占用内存空间
实参与形参是一一对应的关系
关键参数不能放在位置参数前面
"""
###
def test(x,y):
print(x)
print(y)
test(x=1,y=2) #与形参顺序无关
test(1,2) #与形参一一对应
#test(x=2,3)
test(3,y=2)
==============================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/func_test5.py
1
2
1
2
3
2
Process finished with exit code 0
func_test7
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: func_test7.py
@time: 2019/05/12
功能: 参数组
笔记:*args只能接收位置参数,转换成元祖的方式不能接收关键字参数
**kwargs 把N个关键字参数转换成字典的方式
"""
########可接收元祖的字符串
def test(x,*args):
print(1)
print(args)
test(1,2,3,4,5,6,7)
test(*[1,1,3,3,5,5,7]) #args=tuple([1,1,3,3,5,5,7])
###可接收字典的关键字
def test2(**kwargs):
print(kwargs)
print(kwargs["name"])
print(kwargs["age"])
print(kwargs["sex"])
test2(name="alex",age=8,sex="F")
test2(**{"name":"alex","age":8,"sex":"F"})
def test3(name,**kwargs):
print(name)
print(kwargs)
test3("alex",age=18,sex="F")
def test4(name,age=18,*args,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)
test4("alex",age=34,sex="m",hobby="tesla")
=============================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/func_test7.py
1
(2, 3, 4, 5, 6, 7)
1
(1, 3, 3, 5, 5, 7)
{'name': 'alex', 'age': 8, 'sex': 'F'}
alex
8
F
{'name': 'alex', 'age': 8, 'sex': 'F'}
alex
8
F
alex
{'age': 18, 'sex': 'F'}
alex
34
()
{'sex': 'm', 'hobby': 'tesla'}
Process finished with exit code 0
第三周-第15章节-Python3.5-局部变量与全局变量作用域1
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: 局部变量.py
@time: 2019/05/12
功能:全局变量与局部变量
笔记:全局变量与局部变量的定义:
在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量
全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序。
当局部变量与全局变量同名时:
在定义局部变量的子程序内,局部变量起作用;在其他地方全局变量起作用
"""
school = "Oldboy edu."
def change_name(name):
global school
school = "Mafe LINUX"
print("before change:",name,school)
name = "Alex Li" #这个函数就是这个变量的作用域
age = 23
print("after change:",name)
print("school:",school)
name="alex"
change_name(name)
print(name)
print("school:",school)
==============================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/局部变量.py
school: Oldboy edu.
before change: alex Mafe LINUX
after change: Alex Li
alex
school: Mafe LINUX
Process finished with exit code 0
第三周-第17章节-Python3.5-递归
递归.py
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:Lenovo
@file: 递归.py
@time: 2019/05/12
功能: 在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
笔记:递归特性:
1、必须要有一个明确的结束条件
2、每次进入更深一层递归时,问题规模相比上次递归都应有所较少
3、递归下利率不高,递归层次过多会导致栈溢出(在计算机中,函数调用通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会建一层栈帧。由于
栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出,)
"""
def calc(n):
print(n)
if int(n/2) > 0:
return calc(int(n/2))
print("------->",n)
calc(1000)
=================================================================================================
D:\Python3.7.3\python.exe D:/PycharmProjects/老男孩S14/函数/递归.py
1000
500
250
125
62
31
15
7
3
1
-------> 1
Process finished with exit code 0
第三周-第18章节-Python3.5-函数式编程与函数不同
转自:http://www.cnblogs.com/alex3714/articles/5717620.html