第一周总结和复习
概述
1.Python是一种解释型语言,解释型语言的最大优点是平台可移植性,最大的缺点是执行效率低下。
2.今天当计算机硬件已经足够发达的时候,我们追求的更多的并不是程序的执行效率。
3.可以从Python的官方网站下载Python的安装程序以及查看相关文档。
说明: 如果要在linux环境下更新到Python3.x版本需要通过源代码构建安装。
4.我们可以使用Python的包管理工具pip来安装第三方模块。pip install ipython jupyter
或 “`shell python -m pip install ipython jupyter
5. 第一个Python程序 - hello,world
"""
第一个Python程序
version:0.5
Auther: xxx
Date: xxxx-xx-xx
Modifier: 王大锤
Date:xxxx-xx-xx
"""
# 使用了Python内置的print函数
print('hello,world!')
•如果想用交互式环境进行Python开发那么可以使用ipython或者jupyter的notebook项目 ```shell
jupyter notebook
•如果要做团队开发以及需要使用多文件多模块协作的大型项目,我们推荐使用PyCharm官方网址
变量
1.变量的作用:变量是数据的载体,内存里的一个空间
2.变量的命名:由数字,字母,下划线组成,数字不能在首位,不能使用特殊字符,不能有关键字
3.变量的类型:整数,浮点数,字符串,布尔
运算符
1.赋值运算符: = += -= *= /=
2.算术运算符: + - / % *
3.关系运算符: > < == !=
4.逻辑运算符: and or not
5.身份运算符: is
分支结构
1.if elif else
个人所得税:
import math
salary = float(input('本月收入'))
insurance = float(input('五险一金'))
diff = salary - insurance - 3500
if diff <= 0:
tax = 0
deduction = 0
elif diff <= 1500:
tax = 0.03
deduction = 0
elif diff <= 4500:
tax = 0.1
deduction = 105
elif diff <= 9000:
tax = 0.2
deduction = 555
elif diff <= 35000:
tax = 0.25
deduction = 1005
elif diff <= 55000:
tax = 0.3
deduction = 2755
elif diff <= 80000:
tax = 0.35
deduction = 5505
else:
tax = 0.45
deduction = 13505
personal = abs(diff * tax - deduction)
print('个人所得税:¥%.2f元' %personal)
print('实际到手工资:¥%.2f元' %(salary - insurance - personal))
循环结构
1. for _ in range()
100以内的偶数求和:
my_sum = 0
for num in range(1,101): # for num in range(2, 102, 2)(起始值,终止值,步长)
if num % 2 == 0:
my_sum += num
print(my_sum)
"""
公鸡5元一只,母鸡3元一只,小鸡1元三只
用100元买100只鸡 问公鸡母鸡小鸡各多少只
x +y + z == 100
5 * x +3 * y + z //3 == 100
z % 3 == 0
穷举法 - 穷尽所有的可能性直到找到正确答案
"""
for x in range(21):
for y in range(34):
for z in range(0,100,3):
if x + y + z ==100 and 5 * x + 3 * y + z // 3 == 100:
print(x, y , z)
#省掉第三条循环
#z = 100 - x - y
# if 5 * x + 3 * y + z // 3 == 100 and z % 3 == 0
# print(x, y , z)
2. while
ABCDE五人夜间到河边捕鱼,捕完鱼后五人在河边睡着.
天亮后A先醒来,将所捕鱼平均分伟五份,结果余一条,将余的一条扔掉,带走自己的一堆.B醒来将余下的四堆又分为五份,也余一条,同样仍掉,也带走自己的一堆.C、D、E醒来后也如此,问他们这天晚上至少捕到多少条鱼?
fish = 1
while True:
totle = fish
is_enough = True
for _ in range(5):
if (totle- 1) % 5 == 0:
totle = (totle - 1) // 5 * 4
else:
is_enough = False
break
if is_enough:
print(fish)
break
fish += 1
Craps赌博游戏:
from random import randint
face1 = randint(1,6)
face2 = randint(1,6)
first_point = face1 + face2
player = 1000
print('玩家摇出了%d点' % first_point)
go_on = False
game_over = True
while game_over:
if player > 0:
if first_point == 7 or first_point == 11:
print('玩家胜')
player += 100
print(player)
elif first_point == 2 or first_point == 3 or first_point == 12:
print('庄家胜')
player -= 100
print(player)
else:
go_on = True
while go_on:
face1 = randint(1,6)
face2 = randint(1,6)
current_point = face1 + face2
print('玩家摇出了%d点' % current_point)
if current_point == 7:
print('庄家胜')
player -= 100
print(player)
go_on = False
elif current_point == first_point:
print('玩家胜')
player += 100
print(player)
go_on = False
if player == 0:
game_over = False
print('sorry,game over!')
本文为Python初学者提供了全面的学习指南,包括Python语言的特点、安装方法、常用开发工具介绍及基本语法讲解,如变量定义、运算符使用、条件分支与循环结构等,并通过实例帮助读者理解和掌握。
696

被折叠的 条评论
为什么被折叠?



