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
python学习第三天 -----2019年4月23日的更多相关文章
- Linux自用指令——2019年10月23日
1.ls ls命令是列出目录内容(List Directory Contents)的意思.运行它就是列出文件夹里的内容,可能是文件也可能是文件夹. ls -a 列出目录所有文件,包含以.开始的隐藏文件 ...
- 16.go语言基础学习(上)——2019年12月16日
2019年12月13日10:35:20 1.介绍 2019年10月31日15:09:03 2.基本语法 2.1 定义变量 2019年10月31日16:12:34 1.函数外必须使用var定义变量 va ...
- 19.go语言基础学习(下)——2019年12月16日
2019年12月16日16:57:04 5.接口 2019年11月01日15:56:09 5.1 duck typing 1. 2. 接口 3.介绍 Go 语言的接口设计是非侵入式的,接口编写者无须知 ...
- 我的Python成长之路---第四天---Python基础(15)---2016年1月23日(寒风刺骨)
二.装饰器 所谓装饰器decorator仅仅是一种语法糖, 可作用的对象可以是函数也可以是类, 装饰器本身是一个函数, 其主要工作方式就是将被装饰的类或者函数当作参数传递给装饰器函数.本质上, ...
- 我的Python成长之路---第四天---Python基础(16)---2016年1月23日(寒风刺骨)
四.正则表达式 字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在.比如判断一个字符串是否是合法的Email地址,虽然可以编程提取@前后的子串,再分别判断是否是单词和 ...
- 我的Python成长之路---第四天---Python基础(14)---2016年1月23日(寒风刺骨)
一.生成器和迭代器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. ...
- 2019年1月23日,好像是这个日子,RF发布了 1.7.3.1 支持python3.6以上了,安装成功。
安装步骤:(win10 家庭版 64) 1.安装Python3.7.2,记得勾选添加Path 2.pip install robotframework 3.pip install wxPython 4 ...
- 2019年8月23日 星期五(Workerman)
Workerman,高性能socket服务框架 Workerman是什么? Workerman是一款纯PHP开发的开源高性能的PHP socket 服务框架. Workerman不是重复造轮子,它不是 ...
- 2019年8月23日 星期五(韩天峰的swoole)
Swoole:面向生产环境的 PHP 异步网络通信引擎 使 PHP 开发人员可以编写高性能的异步并发 TCP.UDP.Unix Socket.HTTP,WebSocket 服务. Swoole 可以广 ...
随机推荐
- SQL Server ->> Sparse File(稀疏文件)
Sparse File(稀疏文件)不是SQL Server的特性.它属于Windows的NTFS文件系统的一个特性.如果某个大文件中的数据包含着大量“0数据”(这个应该从二进制上看),这样的文件就可以 ...
- 学习笔记---Javascript事件Event、IE浏览器下的拖拽效果
学习笔记---Javascript事件Event.IE浏览器下的拖拽效果 1. 关于event常用属性有returnValue(是否允许事件处理继续进行, false为停止继续操作).srcE ...
- 华为手机在开发Android调试时logcat不显示输出信息的解决办法
手机连接电脑RUN AS logcat 提示:Unable to open log device '/dev/log/main': No such file or directory 信息 本人华为C ...
- js中公有方法、特权方法、静态方法
1.公有属性和公有方法 1 2 3 4 5 6 7 8 9 function User(name,age){ this.name = name;//公有属性 this.age = age; } ...
- 浅谈SAP Cloud for Sales 自动化
在Jerry还在本科进行计算机理论知识学习时,我曾经把软件开发里的质量工程师(Quality Engineer)理解成是每天只是简单地做着运行开发人员编写好的软件,如果发现问题,通知开发人员去修改这种 ...
- vbox安装 ubuntu server 后 安装增强包
用vbox安装虚拟机系统如果不装增强包, 有很多东西就有点不好用-用vbox安装ubuntu server时,点击菜单中的安装增强功能.因为ubuntu server版本没有ui,所以不能很方便滴找到 ...
- PostgreSQL的generate_series函数应用
一.简介 PostgreSQL 中有一个很有用处的内置函数generate_series,可以按不同的规则产生一系列的填充数据. 二.语法 函数 参数类型 返回类型 描述 generate_serie ...
- AngularJs学习笔记--E2E Testing
原版地址:http://docs.angularjs.org/guide/dev_guide.e2e-testing 当一个应用的复杂度.大小在增加时,使得依靠人工去测试新特性的可靠性.抓Bug和回归 ...
- 关于notify() 和notifyAll() 一个需要注意的地方
notify() 和 notifyAll()都是唤醒其他正在等待同一个对象锁的线程. 下面是我遇到的一个问题,记下来,免得忘了. 直接上代码,有错误的代码: 代码描述:有一个Caculate类,类中又 ...
- MyBatis(3)开发dao方法
本次全部学习内容:MyBatisLearning SqlSession SqlSession是一个面向用户(程序员)的接口. SqlSession中提供了很 ...