算法方向-大数据分析与挖掘学习笔记(5)

调试嵌套循环

for x in range(5):
    for y in range(5):
        print(x,end='\t')
    print()

运行结果:

0	0	0	0	0	
1	1	1	1	1	
2	2	2	2	2	
3	3	3	3	3	
4	4	4	4	4

打印九九乘法表

#打印九九乘法表
for m in range(1,10):
    for n in range(1,m+1):
        print('{0}*{1}={2}'.format(m,n,m*n),end='\t')
    print()

运行结果:

1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	

用列表和字典存储表格信息,打印出表中工资高于15000的数据

r1 = dict(name='高小一',age=18,salary=30000,city='北京')
r2 = dict(name='高小二',age=19,salary=20000,city='上海')
r3 = dict(name='高小三',age=20,salary=10000,city='深圳')

tb = [r1,r2,r3]

for x in tb:
    if x.get('salary') > 15000:
        print(x)

运行结果:

{'name': '高小一', 'age': 18, 'salary': 30000, 'city': '北京'}
{'name': '高小二', 'age': 19, 'salary': 20000, 'city': '上海'}

测试break

# 测试break
while True:
    a = input('请输入一个字符(输入Q或q时退出):')
    if a.upper() == 'Q':
        break
    else:
        print(a)

测试continue

# continue测试
empNum = 0
salarySum = 0
salarys = []

while True:
    s = input('请输入员工的薪资(按Q或q结束录入):')

    if s.upper() == 'Q':
        break
    if float(s) < 0:
        print('输入错误,请重新输入!')
        continue

    empNum += 1
    salarys.append(float(s))
    salarySum += float(s)

print('员工数量{0}'.format(empNum))
print('薪资明细:',salarys)
print('平均薪资{0}'.format((salarySum)/empNum))

测试循环中的else

#测试循环中的else

salarySum = 0
salarys = []

for i in range(4):
    s = input('请输入一共4名员工的工资(按Q或q结束录入):')

    if s.upper() == 'Q':
        print('录入完成,退出!')
        break
    if float(s) < 0:
        print('输入错误')
        continue
    salarySum += float(s)
    salarys.append(float(s))
else:
    print('已全部录入4名员工的薪资')

print('录入薪资:',salarys)
print('平均薪资:{0}'.format(salarySum/4))

输入及运行结果:

请输入一共4名员工的工资(按Q或q结束录入):20000
请输入一共4名员工的工资(按Q或q结束录入):20000
请输入一共4名员工的工资(按Q或q结束录入):30000
请输入一共4名员工的工资(按Q或q结束录入):50000
已全部录入4名员工的薪资
录入薪资: [20000.0, 20000.0, 30000.0, 50000.0]
平均薪资:30000.0

推导式测试

#测试推导式

#列表推导式
#推导式实现
y = [x*2 for x in range(1,50) if x%5==0]
print(y)

#循环实现
y = []
for x in range(1,50):
    if x%5 == 0:
        y.append(x*2)
print(y)

#推导式实现
cells = [(row,col) for row in range(1,5) for col in range(1,5)]
print(cells)

#循环实现
cells = []
for row in range(1,5):
    for col in range(1,5):
        cells.append((row,col))
print(cells)


#字典推导式
#推导式实现
my_text = 'i love you, i love sxt, i love gaoqi'
char_count = {c:my_text.count(c) for c in my_text}
print(char_count)

#循环实现
# for x in my_text:
#     my_text.count(x)


#集合推导式
b = {x for x in range(1,100) if x%9==0}
print(b)


#生成器推导式,用来生成元组
#一个生成器只能运行一次,
# 第一次迭代可以得到数据,第二次迭代发现数据已经没有了
#%%
gnt = (x for x in range(1,100)  if x%9==0)
for x in gnt:
    print(x,end=' ')
#%%

运行结果:

[10, 20, 30, 40, 50, 60, 70, 80, 90]
[10, 20, 30, 40, 50, 60, 70, 80, 90]
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
{'i': 4, ' ': 8, 'l': 3, 'o': 5, 'v': 3, 'e': 3, 'y': 1, 'u': 1, ',': 2, 's': 1, 'x': 1, 't': 1, 'g': 1, 'a': 1, 'q': 1}
{99, 36, 72, 9, 45, 81, 18, 54, 90, 27, 63}
9 18 27 36 45 54 63 72 81 90 99 

画圆环改进之画棒棒糖

import turtle

t = turtle.Pen()
# t.speed(0)

my_colors = ['red','yellow','green','pink','blue']
t.width(5)

for i in range(10):
    t.color(my_colors[i%len(my_colors)])
    t.penup()
    t.goto(0,-10*i)
    t.pendown()
    t.circle(15+i*10)

t.goto(0,-10*(i))
t.color('brown')
t.width(10)
t.goto(0,-10*(i)-200)
turtle.done()

运行结果:
在这里插入图片描述
函数
一切皆对象,函数也是对象
变量作用域:全局变量和局部变量
参数的传递:形参和实参
深浅拷贝:

#测试浅拷贝、深拷贝

import copy

def testCopy():
    a = [10,20,[5,6]]
    b = copy.copy(a)

    print('a:',a)
    print('b:',b)

    b.append(30)
    b[2].append(7)
    print('浅拷贝........')
    print('a:',a)
    print('b:',b)

def testDeepCopy():
    a = [10, 20, [5, 6]]
    b = copy.deepcopy(a)

    print('a:', a)
    print('b:', b)

    b.append(30)
    b[2].append(7)
    print('深拷贝........')
    print('a:', a)
    print('b:', b)

testCopy()
testDeepCopy()

运行结果:

a: [10, 20, [5, 6]]
b: [10, 20, [5, 6]]
浅拷贝........
a: [10, 20, [5, 6, 7]]
b: [10, 20, [5, 6, 7], 30]
a: [10, 20, [5, 6]]
b: [10, 20, [5, 6]]
深拷贝........
a: [10, 20, [5, 6]]
b: [10, 20, [5, 6, 7], 30]

浅拷贝:
浅拷贝深拷贝:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值