python学习(十)之阶段性总结
【摘要】
库名的两种调用方式
import turtle
from turtle import *
12
1.9*9乘法口诀
ps:注意缩进不能变,{:2} ':‘前无空格意思为结果保留两位有效位 en...
库名的两种调用方式
import turtle
from turtle import *
- 1
- 2
1.9*9乘法口诀
ps:注意缩进不能变,{:2} ':‘前无空格意思为结果保留两位有效位 end=’'也无空格 可在print{:2}后加空格,代码更加美观
for i in range(1,10):
for j in range(1,i+1):
print("{}*{}={:2} " .format(j,i,i*j),end='')
print(' ')
- 1
- 2
- 3
- 4
2.阶乘累加
sum, tmp = 0, 1
for i in range(1,11):
tmp *= i
sum += tmp
print('运算结果为:{} '.format(sum))
- 1
- 2
- 3
- 4
- 5
3.猴子吃桃
n = 1
for i in range(5,0,-1):
n = (n+1)<<1
print(n)
- 1
- 2
- 3
- 4
4.菜谱
diet = ['西红柿', '花椰菜', '黄瓜', '牛排', '虾仁']
for x in range(0,5):
for y in range(0,5):
if (x!=y):
print('{}{}' .format(diet[x],diet[y]))
- 1
- 2
- 3
- 4
- 5
5.五角星绘制
color和fillcolor的区别,color可以确定两个即画笔颜色和填充颜色,fillcolor只能确定填充颜色
from turtle import *
fillcolor('red')
begin_fill()
while True:
forward(200)
right(144)
if abs(pos()) < 1:
break
end_fill()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
6.太阳花绘制
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
7.螺旋线绘制
turtle.speed(‘fastest’)速度很快
import turtle
import time
turtle.speed('fastest')
turtle.pensize(2)
for x in range(100):
turtle.forward(2*x)
turtle.left(90)
time.sleep(3)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
8.彩色螺旋线绘制
turtle.tracer(False)直接画完,不显示过程,只显示结果
import turtle
import time
turtle.pensize(2)
turtle.bgcolor('black')
colors = ['red', 'yellow', 'purple', 'blue']
turtle.tracer(False)
for x in range(400):
turtle.forward(2*x)
turtle.color(colors[x % 4])
turtle.left(91)
turtle.tracer(True)
time.sleep(3)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
9 float可用eval替换 ,将字符串str当成有效的表达式来求值并返回计算结果。
参考http://blog.csdn.net/u011138533/article/details/61920477
#tempConvert.py
val = input('请输入温度值(如:32C): ')
if val[-1] in ['C', 'c']:
f = 1.8 * float(val[0:-1]) + 32
print('转化后的温度为: %.2fF'%f)
elif val[-1] in ['F', 'f']:
c = (float(val[0:-1] - 32))/1.8
print('转化后的温度为: %.2fC'%c)
else:
print('输入有误')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
10.蟒蛇绘制
import turtle
def drawSnake(rad, angle, len, neckrad):
for i in range(len):
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.circle(rad, angle/2)
turtle.fd(rad)
turtle.circle(neckrad+1, 180)
turtle.fd(rad*2/3)
def main():
turtle.setup(3100, 800, 0, 0)
pythonsize = 30
turtle.pensize(pythonsize)
turtle.pencolor('blue')
turtle.seth(-40)
drawSnake(40, 80, 5, pythonsize/2)
main()
彩色蟒蛇
import turtle
def drawSnake(rad, angle, len, neckrad):
colors = ['red', 'yellow', 'purple', 'blue', 'green']
for i in range(len):
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.pencolor(colors[i%5])
turtle.circle(rad, angle/2)
turtle.fd(rad)
turtle.circle(neckrad+1, 180)
turtle.fd(rad*2/3)
turtle.done()
def main():
turtle.setup(3100, 800, 0, 0)
pythonsize = 30
turtle.pensize(pythonsize)
turtle.seth(-40)
drawSnake(40, 80, 5, pythonsize/2)
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
# -*- coding: utf-8 -*-
def main():
fileName = input("FileName")
infile = open(fileName, 'r')
sum = 0
cnt = 0
for line in infile:
sum += eval(line)
cnt += 1
print("Average Num is : ", sum/cnt)
main()
def main():
fileName = input("FileName")
infile = open(fileName, 'r')
sum = 0.0
cnt = 0
line = infile.readline()
while line != "":
for xstr in line.split(","):
sum += eval(xstr)
cnt += 1
line = infile.readline()
print("Average Num is : ", sum/cnt)
main()
num1, num2 = eval(input("Please input 2 num sepratedbu a comma"))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
python 的6种数据类型
数字类型、字符串类型
元组类型、列表类型
文件类型、字典类型
- 1
- 2
- 3
- 4
复述表示为complex
可用type查看数据类型
len可查看字符串长度
eval 可把字符串表达式转化为表达式
str 可将数字转化为字符串
- 1
- 2
- 3
- 4
- 5
花括号叫字典dict
中括号叫列表list
小括号叫元组tuple 不可修改
- 1
- 2
- 3
python采用两种字符串格式的方法
1.类似C语言,使用%
2.使用format() <模板字符串>.format(<逗号分隔的参数>)
模板字符串用一系列的槽组成,按照序号进行参数替换。槽中没有序号则按照顺序依次替换
槽的内部样式为:{<参数序号>:<格式控制标记>}
- 1
- 2
- 3
- 4
- 5
结束一个死循环程序可以用 ctrl+c
strip()字符串用于移除字符串头尾制定的字符,默认是空格
- 1
- 2
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/77862667
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)