1.输入两个整数,打印较大的那个值
2.输入三个整数,按照从小到大的顺序打印
3.输入一个三位数,打印其个位、十位、百位上的数
4.输入一个年份,判断是否为闰年,是打印一句话,不是打印另一句话
5.输入一个整数,判断其是否既能被3整除,又能被5整除
二、循环
1.计算1~100之间所有整数的和
2.打印字符a~z
3.打印字符Z~A
4.循环输入10个字符,大写转为小写,小写转为大写,其他字符不处理
5.思考:循环能否嵌套,想一想其应用场景及执行流程
三、循环for-in
-
示例1:遍历字符串
s = 'I love you more than i can say'
for i in s:
print(i)
-
示例2:遍历列表l = ['鹅鹅鹅', '曲项向天歌', '锄禾日当午', '春种一粒粟']
for i in l:
print(i)
-
示例3:遍历字典
d = {'a':'apple', 'b':'banana', 'c':'car', 'd': 'desk'}
for key in d:
多重循环
n = int(input('请输入一个整数:'))
'''
1
1 2
1 2 3
1 2 3 4
...
1 2 3 4 ... n
'''
'''
j = 1
while j <= n:
i = 1
while i <= j:
print(i, end=' ')
i += 1
print()
j += 1
'''
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end=' ')
print()
for i in range(1, 10):
for j in range(1, i+1):
print('{}x{}={}'.format(j, i, j*i), end=' ')
print()
l = [1, 9, 4, 2, 0, 8, 3, 7]
-
使用冒泡法排序列表
1.冒泡:每次比较相邻的两个元素,不合适就交换,依次向后,一圈下来可以确定一个元素
2.需要使用双重循环,外层循环控制循环的圈数, 内层控制一圈怎么交换
-
遍历列表,打印:我叫xxx,今年yyy岁,我来自zzz,数据如下
lt = [
{'name':'小王', 'age':18, 'info':[('phone', '123'), ('dizhi', '广州')]},
{'name':'小芳', 'age':19, 'info':[('phone', '789'), ('dizhi', '深圳')]},
{'name':'小杜', 'age':22, 'info':[('phone', '567'), ('dizhi', '北京')]},
{'name':'小孟', 'age':28, 'info':[('phone', '000'), ('dizhi', '上海')]},
{'name':'小乔', 'age':26, 'info':[('phone', '111'), ('dizhi', '河南')]},
]
-
从终端输入两个整数m,n,打印m*n的表格,如:2,5,打印如下效果
1 2 3 4 5
6 7 8 9 10
四、函数基础
-
前面所有的练习,能用函数的就封装成函数
-
封装函数:生成随机的字符串,参数:长度(默认为4)、类型(默认为0)
-
试着查找并学习各种数据类型相关的函数
五、匿名函数
-
示例1:比较两个数的大小
def calc(a, b, func):
return func(a, b)
-
示例2:对字典排序l = [
{'name':'xiaowang', 'age':15, 'height':150},
{'name':'xiaodu', 'age':14, 'height':145},
{'name':'xiaopang', 'age':12, 'height':140},
{'name':'banhua', 'age':13, 'height':155},
]
# def d_key(d):
# return d['age']
# l.sort(key=d_key)
# 当列表中的元素无法直接比较大小时,需要传递参数key
# key是一个函数,接受元素作为参数,返回用于比较的项
l.sort(key=lambda x:x['height'])
for i in l:
print(i)
-
练习:
-
试着自己封装一个列表的sort函数,接受参数:排序对象、key、reverse
六、函数基础
-
使用random自己实现randint
-
实现一个简单的计算器,参数在启动程序时输入
-
实现(int)函数:传入'12345',返回12345
-
实现(str)函数:传入12345,返回'12345'
-
号码归属地查询:
5582|1860101|010|北京市|北京联通GSM卡
5583|1860100|010|北京市|北京联通GSM卡
5584|1368141|010|北京市|北京移动神州行卡
5585|1860111|010|北京市|北京联通GSM卡
5586|1358198|010|北京市|北京移动动感地带卡
5587|1361139|010|北京市|北京移动预付费卡
5588|1361138|010|北京市|北京移动神州行卡
5591|1360110|010|北京市|北京移动全球通卡
5748|1364110|010|北京市|北京移动神州行卡
10186|1581584|020|广东省广州市|广东移动全球通卡
15046|1391897|021|上海市|上海移动全球通卡
17250|1502207|022|天津市|天津移动全球通卡
21137|1345272|023|重庆市万州|重庆移动大众卡
22700|1347812|024|辽宁省沈阳市|辽宁移动大众卡
24256|1377065|025|江苏省南京市|江苏移动全球通卡
26360|1898606|027|湖北省武汉市|湖北电信CDMA卡
28709|1860802|028|四川省成都市|四川联通GSM卡
30641|1552961|029|陕西省西安市|陕西联通GSM卡
31700|1563007|0310|河北省邯郸市|河北联通GSM卡
33360|1583396|0311|河北省石家庄市|河北移动全球通卡
34825|1508122|0312|河北省保定市|河北移动全球通卡
35363|1551235|0313|河北省张家口|河北联通GSM卡
37700|1331326|0316|河北省廊坊市|河北电信CDMA卡
43500|1350358|0358|山西省吕梁市|山西移动全球通卡
43908|1553625|0359|山西省运城市|山西联通GSM卡
44521|1335360|0370|河南省商丘市|河南电信CDMA卡
50078|1509369|0378|河南省开封市|河南移动全球通卡
53603|1583981|0398|河南省三门峡|河南移动全球通卡
53916|1335897|0410|辽宁省铁岭市|辽宁电信CDMA卡
55248|1554254|0411|辽宁省大连市|辽宁联通GSM卡
58618|1374272|0427|辽宁省盘锦市|辽宁移动全球通卡
58932|1554183|0429|辽宁省葫芦岛|辽宁联通GSM卡
60268|1340475|0431|吉林省长春市|吉林移动大众卡
-
歌词解析(不强制)
[ti:蓝莲花]
[ar:许巍]
[al:留声十年绝版青春北京演唱会]
[00:-01.70]蓝莲花
[00:-00.70]演唱:许巍
[00:00.00]
[00:00.70]没有什么能够阻挡
[00:06.01]你对自由的向往
[00:11.43]天马行空的生涯
[00:16.99]你的心了无牵挂
[00:21.20]
[02:11.55][01:50.60][00:22.63]穿过幽暗的岁月
[02:16.93][01:55.60][00:27.81]也曾感到彷徨
[02:22.21][02:01.09][00:33.13]当你低头的瞬间
[02:27.62][02:06.33][00:38.32]才发觉脚下的路
[02:31.64][02:10.23][00:42.37]
[02:32.97][00:43.79]心中那自由的世界
[02:38.23][00:49.50]如此的清澈高远
[02:43.30][00:54.31]盛开着永不凋零
[02:47.70][00:58.50]蓝莲花
[02:53.95][03:00.06][01:05.41]
七、递归函数
-
求阶乘
-
斐波那契数列(1,1,2,3,5,8,13,21,34,...)
八、高级函数
-
处理一个元素为字符串的列表,要求将字符串两边的空白去掉,然后首字母大写
-
求列表中所有元素的乘积
-
提取奇数,并且能被3整除的元素
九、目录操作
-
实现一个拷贝文件的功能,提醒:要考虑超大文件问题,如:依次读取1024字节,循环读取
-
import os
# 拷贝文件
def copy(src, dst):
# 此处应该有健壮性判断
if os.path.abspath(src) == os.path.abspath(dst):
print('源文件与目标地址相同,无法拷贝')
return
# 判断是否是目录
if os.path.isdir(src):
print('源文件是目录文件,无法拷贝')
return
# 拷贝的目标地址是目录时(没有指定文件名)
if os.path.isdir(dst):
# 提取源文件的文件名
src_name = os.path.basename(src)
# 拼接目录文件名
dst = os.path.join(dst, src_name)
# 打开源文件
src_fp = open(src, 'r')
# 打开目标文件
dst_fp = open(dst, 'w')
# 读取依次内容
content = src_fp.read(1024)
while len(content):
# 写入目标文件
dst_fp.write(content)
# 循环读取内容
content = src_fp.read(1024)
# 关闭源文件
src_fp.close()
# 关闭目标文件
dst_fp.close()
copy('test.txt', 'abc/123.py')
-
递归删除一个文件夹
-
import os
def remove(file):
# 判断是否存在
if not os.path.exists(file):
print('文件不存在,无法删除')
return
# 删除普通文件
if os.path.isfile(file):
os.remove(file)
return
# 是目录,递归删除
dirs = os.listdir(file)
for f in dirs:
# 拼接文件名
file_name = os.path.join(file, f)
if os.path.isfile(file_name): # 是普通文件
os.remove(file_name)
else: # 是目录文件
remove(file_name)
# 删除空目录
os.rmdir(file)
remove('abc')
-
统计一个文件夹的大小
-
import os
def size(file):
# 判断文件是否存在
if not os.path.exists(file):
print('文件不存在,无法计算大小')
return None
# 是普通文件
if os.path.isfile(file):
return os.path.getsize(file)
# 是目录文件,递归遍历统计
total = 0
dirs = os.listdir(file)
for f in dirs:
file_name = os.path.join(file, f)
if os.path.isfile(file_name):
total += os.path.getsize(file_name)
else:
total += size(file_name)
return total
print(size('C:/Apache24/htdocs/code/day10/01-lianxi/'))
-
拷贝一个文件夹
-
import os
# 拷贝文件
def copy(src, dst):
# 此处应该有健壮性判断
if os.path.abspath(src) == os.path.abspath(dst):
print('源文件与目标地址相同,无法拷贝')
return
# 判断是否是目录
if os.path.isdir(src):
print('源文件是目录文件,无法拷贝')
return
# 拷贝的目标地址是目录时(没有指定文件名)
if os.path.isdir(dst):
# 提取源文件的文件名
src_name = os.path.basename(src)
# 拼接目录文件名
dst = os.path.join(dst, src_name)
# 打开源文件
src_fp = open(src, 'r')
# 打开目标文件
dst_fp = open(dst, 'w')
# 读取依次内容
content = src_fp.read(1024)
while len(content):
# 写入目标文件
dst_fp.write(content)
# 循环读取内容
content = src_fp.read(1024)
# 关闭源文件
src_fp.close()
# 关闭目标文件
dst_fp.close()
# copy('test.txt', 'abc/123.py')
def copy_dir(src, dst):
if os.path.abspath(src) == os.path.abspath(dst):
print('源文件与目标地址相同,无法拷贝')
return
if os.path.isfile(src):
print(src, '源文件不是目录,无法拷贝')
return
# 目标地址不存在
if not os.path.exists(dst):
os.makedirs(dst)
# 判断目标地址是否是目录
if not os.path.isdir(dst):
print('目标地址不是目录,无法拷贝')
return
# 目标地址是目录,递归拷贝
dirs = os.listdir(src)
for f in dirs:
src_file = os.path.join(src, f)
dst_file = os.path.join(dst, f)
if os.path.isfile(src_file):
copy(src_file, dst_file)
else:
copy_dir(src_file, dst_file)
copy_dir('a', 'c/a')
-
移动一个文件夹
-
import os
def move(src, dst):
if os.path.abspath(src) == os.path.abspath(dst):
print('源文件与目标地址相同,无法拷贝')
return
if os.path.isfile(src): # 普通文件
src_fp = open(src, 'r')
dst_fp = open(dst, 'w')
while True:
content = src_fp.read(1024)
if not content:
break
dst_fp.write(content)
src_fp.close()
dst_fp.close()
# 删除源文件
os.remove(src)
else: # 目录
if not os.path.exists(dst):
os.makedirs(dst)
dirs = os.listdir(src)
for f in dirs:
src_file = os.path.join(src, f)
dst_file = os.path.join(dst, f)
move(src_file, dst_file)
# 删除源目录文件
os.rmdir(src)
# move('test.py', 'test2.py')
move('a', 'c')
-
目录整理
-
一个目录中有各种文件,也有文件夹
-
将所有的文件夹统一放到dir目录下
-
将没有后缀的文件统一放到others目录下将有后缀的文件放到后缀名大写的文件夹下
-
# 处理前
test/
dir1/
dir2/
1.py
2.py
3.txt
4.pdf
123
456
# 处理后
test/
DIR/
dir1/
dir2/
PY/
1.py
2.py
TXT/
3.txt
PDF/
4.pdf
OTHERS/
123
456
import os
import shutil
def deal(dir):
if not os.path.isdir(dir):
print(dir, '不是目录,无法整理')
return
dirs = os.listdir(dir)
for f in dirs:
# 拼接文件名
file_name = os.path.join(dir, f)
# 分类处理
if os.path.isdir(file_name): # 是目录
# 拼接DIRS目录
DIRS = os.path.join(dir, 'DIRS')
# DIRS目录不存在则创建
if not os.path.exists(DIRS):
os.mkdir(DIRS)
shutil.move(file_name, DIRS)
else: # 是文件
# 按照'.'进行切割
lt = file_name.rsplit('.', 1)
# 判断是否有后缀
if len(lt) == 1: # 没有后缀
OTHERS = os.path.join(dir, 'OTHERS')
if not os.path.exists(OTHERS):
os.mkdir(OTHERS)
shutil.move(file_name, OTHERS)
else: # 有后缀
SUFFIX = os.path.join(dir, lt[1].upper())
if not os.path.exists(SUFFIX):
os.mkdir(SUFFIX)
shutil.move(file_name, SUFFIX)
deal('test')