第一周总结和复习
概述
1.Python是一种解释型语言,解释型语言的最大优点是平台可移植性,最大的缺点是执行效率低下。
2.今天当计算机硬件已经足够发达的时候,我们追求得更多的并不是程序的执行效率,而是开发效率。
3.Python的官方网站下载Python的安装程序以及查看相关文档。
说明:如果要在Linux环境下更新到Python 3.x版本需要通过源代码构建安装。
4.我们可以使用Python的包管理工具pip来安装第三方模块。
pip install ipython jupyter
或
python -m pip install ipython jupyter
5.第一Python程序 - hello,world
"""
第一个Python程序
Version:0.1
Auther:贺松
Date:2018-3-3
"""
# 使用了Python内置的print函数打印字符串
print('hello,world')
-如果使用交互式环境进行Python开发可以使用ipython或者jupyter的notebook项目
jupyter notebook
-如果要做团队开发以及需要使用多文件模块的大型项目,我们推荐使用Pycharm的集成开发工具,可以前往Jetbrains的官方网站进行下载
变量
1.变量的作用(变量是数据的载体)
2.变量的命名(1.字母,数字,下划线,数字不能开头,不能使用特殊字符。2.大小写敏感,如‘A’和‘a’就是不同的变量。 3.避开关键字和系统保留字)
3.变量的类型(整型,浮点型,字符串型,布尔类型)
"""
第二个Python程序
Version:0.1
Auther:贺松
Date:2018-3-3
"""
a = float(input())
b = (a + 3)
print(b)
运算符
1.赋值运算符(=)
2.算术运算符(+:加;-:减乘;* :乘;/:除;//:整除;%:求余数)
3.关系运算符(<:小于;>:大于;<=:小于等于;>=大于等于)
4.逻辑运算符(and;or;not)
5.身份运算符(is;is not)
"""
第三个Python程序:求和,求商,求幂,整除,求余数
Version:0.1
Auther:贺松
Date:2018-3-3
"""
x = float(input('x = '))
y = float(input('y = '))
print('%f + %f = %f' % (x,y,x + y))
print('%f / %f = %f' % (x,y,x / y))
print('%f ** %f = %f' % (x,y,x ** y)) # 求幂
print(x // y)
# 求模(求余数)
print('%f %% %f = %f' % (x,y,x % y))
分支结构
if elif else(冒号,缩进)
"""
第四个Python程序:求和,求商,求幂,整除,求余数
Version:0.1
Auther:贺松
Date:2018-3-3
"""
a = float(input('请输入第一个数字:'))
b = float(input('请输入第二个数字:'))
c = float(input('请输入第三个数字:'))
if a > b and a > c:
print('其中最大的数字是:%.2f' % a)
elif b >a and b >c:
print('其中最大的数字是:%.2f' % b)
else:
print('其中最大的数字是:%.2f' % c)
"""
第五个Python程序:三个数从小到大排列
Version:0.1
Auther:贺松
Date:2018-3-3
"""
a = int(input('a = '))
b = int(input('b = '))
c = int(input('c = '))
if a > b:
a,b = b,a
if b > c:
b,c = c,b
if a > b:
a,b = b,a
print(a,b,c)
循环结构
for(知道循环次数)
while(不知道循环次数)
"""
第六个Python程序:输入一个数字,判断是不是素数
Version:0.1
Auther:贺松
Date:2018-3-3
"""
from math import
a = int(input('a = '))
is_prime = True
for _ in range(2,a):
if a % _ == 0:
is_prime = False
break
if is_prime:
print('这是一个素数')
else:
print('这不是一个素数')
"""
第七个Python程序:输出1~100中的素数
Version:0.1
Auther:贺松
Date:2018-3-3
"""
is_prime = True
for i in range(2,101):
is_prime = True
for x in range(2,i):
if i % x == 0:
is_prime = False
break
if is_prime:
print(i)
"""
第八个Python程序:乘法口诀
Version:0.1
Auther:贺松
Date:2018-3-3
"""
for row in range(1,10):
for col in range(1,row + 1):
print('%d*%d=%d' % (row,col,row * col),end='\t')
print()
"""
第九个Python程序:100元买100只鸡
Version:0.1
Auther:贺松
Date:2018-3-3
"""
for x in range(21):
for y in range(34):
z = 100 - x - y
if z % 3 == 0 and 5 * x + 3 * y + z // 3 == 100:
print(x,y,z)
"""
第十个Python程序:水仙花数100~999之间
"""
for a in range(1,10):
for b in range(0,10):
for c in range(0,10):
if a ** 3 + b ** 3 + c ** 3 == a * 100 + b * 10 + c:
print(a,b,c,sep='')
"""
第十一个Python程序:找出10000以内的完美数
"""
from time import time
from math import sqrt
start = time()
for x in range(1,10000):
a = 1
for y in range(2,int(sqrt(x) + 1)):
if x % y == 0:
a += y
if y != x // y:
a += x // y
if a == x:
print(x)
end = time()
print(str(end - start) + '秒')
"""
第十二个Python程序:用turtle模块画图
"""
import turtle
turtle.pencolor('red')
turtle.pensize(5)
turtle.begin_fill()
turtle.fillcolor('blue')
for _ in range(8):
turtle.forward(100)
turtle.right(45)
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
turtle.mainloop()
"""
第十三个Python程序:输出有规律的图形
"""
for x in range(1,6):
print(' ' * (6 - x) + str('*' * (2 * x - 1)))
"""
第十四个Python程序:fish
"""
fish = 1
while True:
total = fish
is_enough = True
for _ in range(5):
if (total -1) % 5 == 0:
total = (total - 1) // 5 * 4
else:
is_enough = False
break
if is_enough:
print(fish)
break
fish +=1
"""
第十五个Python程序:人机猜拳游戏
"""
from random import randint
for _ in range(10):
a = True
while a:
player = int(input('请玩家出拳:'))
if player == 1:
print('您出的是石头')
break
if player == 2:
print('您出的是剪刀')
break
if player == 3:
print('您出的是布')
break
else:
print('请重新输入!')
cp = randint(1,3)
if cp == 1:
print('电脑出的是石头')
if cp == 2:
print('电脑出的是剪刀')
if cp == 3:
print('电脑出的是布')
if (player == 1 and cp == 2) or (player == 2 and cp == 3) or (player == 3 and cp ==1):
print('you win!\n')
elif (player == 1 and cp == 3) or (player == 2 and cp == 1) or (player == 3 and cp ==2):
print('you lose!\n')
else:
print('平局!\n')