近一周的python心得

第一周总结

概述

1.python是一种解释型语言,解释型语言的最大优点是平台可移植性,最大的缺点是执行效率低下。

2.当今天计算机硬件已经足够发达的时候,我们追求更多的并不是程序的执行效率而是开发效率。

3.python的官方网站下载python的安装程序以及查看文档。

说明:如果要在linux环境下更新到python3.x版本需要通过源代码构建安装

4.我们可以使用python的包管理工具pip来安装第三方模块。

pip install ipython jupyter

5.第一个python程序-hello,world

print('hello,world!')
Version:0.5
Author:熊彪
Date:2018-03-02
Modifier:***
  • 如果想用交互式环境进行python开发那么可以使用ipython或者jupyter的notebook项目
  • 如果要做团队开发以及需要使用多文件多模块协作的大型项目可以从JetBrains公司的官方网站下载

变量

1.变量的作用

2.变量的命名

由数字、字母、下划线组成,数字不能开头,不能用关键字符

不能有空格,大小写敏感,最好用小写

不能让保留字符与敏感字符冲突,有多个单词的用下划线连接起来或者用驼峰命名法

3.变量的类型

运算符

1.赋值运算符

2.算术运算符

3.关系运算符
< > == != <= >=

4.逻辑运算符

and,or,not

5.身份运算符

is

分支结构

if……elif……else

循环结构

for..in..(知道次数)

while…(不知道次数)

数据类型有:

数字型(number),布尔型(boolean),空类型(None),字符型(string),列表(list),元组(tuple),字典(dict),集合(set)
,

                                #getpass函数隐藏密码
import getpass
username=input('请输入用户名:')
password=getpass.getpass('请输入密码:')
if username=='ab' and password=='cd':
    print('欢迎使用本系统')
else:
    print('用户名或密码错误!')
                       #输入任意三个数求三角形的周长和面积
import math
a=float(input('a='))
b=float(input('b='))
c=float(input('c='))
if a+b>c and b+c>a and a+c>b:
    perimeter=a+b+c
    print('周长:'+str(perimeter))
    half=perimeter/2
    area=math.sqrt(half*(half-a)*(half-b)*(half-c))
    print('面积:'+str(area))
else:
    print('三条边的长度不能构成三角形!')
                                       #分段函数求值
x=float(input('x='))
if x<-1:
    y=5*x+3
elif x<=1:
    y=2*x-1
else:
    y=3*x-5
print(y)
from random import randint
f1=f2=f3=f4=f5=f6=0
for x in range(60000):
    face=randint(1,6)
    if face==1:
        f1+=1
    elif face==2:
        f2+=1
    elif face==3:
        f3+=1
    elif face==4:
        f4+=1
    elif face==5:
        f5+=1
    else:f6+=1
print('1点摇出了%d次' % f1)
print('2点摇出了%d次' % f2)
print('3点摇出了%d次' % f3)
print('4点摇出了%d次' % f4)
print('5点摇出了%d次' % f5)
print('6点摇出了%d次' % f6)
import time
for num in range(10):
    print(num,'hello,world!',sep=':')
    time.sleep(1)
                                       #求1到100的偶数和
my_sum=0
for  num in range(1,101):
    if num%2==0:
        my_sum+=num
print(my_sum)


my_sum=0
for num in range(2,101,2):
    my_sum+=num
print(my_sum)

                               #求100以内3或5的倍数和
my_sum=0
num=1
while num<=100:
    if num%3==0 or num%5==0:
        my_sum+=num
    num+=1
print(my_sum)

                                       #人机猜数
from random import randint
answer=randint(1,100)
counter=0
game_over=False
while not game_over:
    your_answer=int(input('请输入:'))
    counter+=1
    if your_answer<answer:
        print('大一点')
    elif your_answer>answer:
        print('小一点')
    else:
        print('恭喜你猜对了')
        break
if counter>7:
    print('智商着急')
                                   #计算个人所得税
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))

a=int(input('a='))
b=int(input('b='))
c=int(input('c='))
if a>b:
    my_max=a
else:
    my_max=b
if c>my_max:
    my_max=c
print(my_max)

                                     #三元条件运算符
a=int(input('a='))
b=int(input('b='))
c=int(input('c='))
my_max=a>b and a or b
my_max=c>my_max and c or my_max
print(my_max)

                                           #排序
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)

                                    #判断质数
from math import sqrt
x=int(input('输入一个整数:'))
is_prime=True
for num in range(2,int(sqrt(x))+1):
    if x%num==0:
        is_prime=False
        break
if is_prime:
    print('%d是质数' %x)
else:
    print('%d不是质数' %x)

                                       #乘法表
for row in range(1,10):
    for col in range(1,row+1):
        print('%d*%d=%d' %(row,col,row*col),end='\t')
    print()

for row in range(5):
    for _ in range(5- row - 1):
        print(' ', end='')
    for _ in range(2 * row+ 1):
        print('*', end='')
    print()
                    #用Ascii编码,chr函数是编码转字符,ord是字符转编码
for row in range(1,6):
    for col in range(1,row+1):
        print(chr(row+64),end='')
    print()

                             #找100内的质数
from math import sqrt
for x in range(2,101):
    is_prime=True
    for y in range(2,int(sqrt(x))+1):
        if x%y==0:
            is_prime=False
    if is_prime:
        print('%d' %x,end='  ')

       #穷举法   由于浮点数表示法的问题不要做浮点数的==和!=运算,因为结果可能跟预期的完全不同
for x in range(15):
    for y in range(26):
        for z in range(0,100,3):
            if x+y+z==100 and 5*x+3*y+z//3==100:
                print(x,y,z)
                   # 找水仙花数
for x in range(100,1000):
    if x==(x//100)**3+((x//10)%10)**3+(x%10)**3:
        print(x,end='  ')


                          # 找完美数
from time import time
from math import sqrt
start=time()
for n in range(2,10001):
    total=1
    for i in range(2,int(sqrt(n))+1):
        if n%i==0:
            total+=i
            if i!=n//i:
                total+=n//i
    if total==n:
        print(n)
end=time()
print((end-start),'秒')

                  # 用turtle画图
import turtle
turtle.pensize(4)
turtle.speed(10)
turtle.pencolor('red')
for x in range(10,100):
    turtle.forward(x)
    turtle.right(90)
    turtle.forward(x)
    turtle.right(90)
    turtle.forward(x)
    turtle.right(90)
    turtle.forward(x)
turtle.mainloop()



import turtle
# 设置屏幕大小
# turtle.screensize(2000, 4000)
# write()往turtle中写内容   norma(正常)  italic(斜体)
# turtle.write('你好,杨晗', font=('雅黑', 30, 'normal'))
# 将画笔的箭头隐藏起来
turtle.hideturtle()
# 将画笔的箭头展示出来
turtle.showturtle()

# 开始填充
'''
turtle.begin_fill()
turtle.circle(100, steps = 3)
turtle.color('pink')
turtle.end_fill()



turtle.reset()


turtle.pensize(30)
turtle.begin_fill()
turtle.circle(50, steps = 7 )
turtle.color('orange')
turtle.end_fill()
turtle.hideturtle()

turtle.done()


#奥运五环
import turtle
turtle.pensize(10)
turtle.color('black')
turtle.circle(100)

turtle.penup()#抬起画笔
turtle.goto(-200,0)#画笔移动,x轴向左移动是负的,向右移是正的,y轴向上移是正的,向下移是负的
turtle.pendown()#画笔放下
turtle.color('blue')
turtle.circle(100)

turtle.penup()
turtle.goto(200,0)
turtle.pendown()
turtle.color('yellow')
turtle.circle(100)

turtle.penup()
turtle.goto(100,-170)
turtle.pendown()
turtle.color('red')
turtle.circle(100)

turtle.penup()
turtle.goto(-100,-170)
turtle.pendown()
turtle.color('gray')
turtle.circle(100)

turtle.done()


#五星红旗
import turtle
turtle.speed(2)

def my_goto(x, y):
    turtle.up()
    turtle.goto(x, y)
    turtle.down()


def drawstar(x):
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(x)
        turtle.right(144)
    turtle.end_fill()


turtle.setup(600, 400, 0, 0)

turtle.color("yellow")
turtle.bgcolor("red")#背景色
turtle.fillcolor("yellow")

my_goto(-250, 75)
drawstar(100)
#四个小五角星的位置
# -120,150
# -80,90
# -80,30
# -120,-30
for i in range(4):
    x = 1
    if i in [0, 3]:
        x = 0
    my_goto(-120 + x * 40, 150 - i * 60)
    turtle.left(30 - i * 15)
    drawstar(30)

my_goto(0, 0)
turtle.hideturtle()

turtle.done()
# 捕鱼游戏
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
                            #人机划拳游戏
from random import randint
for _ in  range(10):
    a=True
    while a:
        player = int(input('请玩家输入:'))
        if player==1:
            print('你出的是石头')
            break
        elif player==2:
            print('你出的是剪刀')
            break
        elif player==3:
            print('你出的是布')
            break
        else:
            print('请重新输入')
    computer=randint(1,3)
    if computer==1:
        print('机器出的是石头')
    elif computer==2:
        print('机器出的是剪刀')
    else:
        print('机器出的是布')
    if player==computer:
        print('平局')
    elif (player==1 and computer==2)or(player==2 and computer==3)or(player==3 and computer==1):
        print('人胜出')
    else:
        print('机器胜出')

Craps赌博游戏

这是一种比较有趣的编程游戏,对于我这种初学python的人来说开始会觉得比较难,但是经过不断的修改之后终于还是把它弄出来了,我没有用一些深奥的函数,比较适合初学python的人理解。这个游戏的规则是摇两个色子,它们的点数和如果为7或者11的话则玩家胜,如果为2,3,12的话则庄家胜,其他点数则为平局,如果摇到平局了则记录下这次的点数和,后面继续摇的时候如果等于平局的点数和则玩家胜,如果后面的点数和为7的话则庄家胜,其他点数则为平局,直到玩家把钱输完终止。

from random import randint
name = input('请输入你的大名:')
print('欢迎参加赌博赢钱游戏')
money = int(input('请输入你本次参与现金:'))
go_on=False
while not go_on:
    yazhu=int(input('请输入押注:'))
    face1=randint(1,6)
    face2=randint(1,6)
    first_point=face1+face2
    print('玩家摇出了%d点' %first_point)
    if first_point==7 or first_point==11:
        money=money+yazhu
        print('玩家胜,玩家剩余金额%d' %money)
        if money<=0:
            break

    elif first_point==2 or first_point==3 or first_point==12:
        money-=yazhu
        print('庄家胜,玩家剩余金额%d' %money)
        if money<=0:
            break

    else:
        print('平局')
        print(money)
        go_on=True
        while go_on:
            yazhu=int(input('请输入赌注:'))
            face3 = randint(1, 6)
            face4 = randint(1, 6)
            current_point = face3 + face4
            print('摇出了%d点' % current_point)
            if current_point == 7:
                money -= yazhu
                print('庄家赢,玩家剩余金额为%d' %money)
                if money<=0:
                    break
                    False
            elif current_point == first_point:
                money += yazhu
                print('玩家赢,玩家剩余金额为%d' %money)
                if money<=0:
                    break
                    False
            else:
                print('平局')
                False
print('游戏结束')
                          #Craps赌博游戏
               ######这是第二种写法,更加标准

from random import randint
######定义摇n颗色子的函数 它会返回摇出的点数
def roll_dice(n):
    total=0
    for _ in range(n):
        total+=randint(1,6)
    return total

print('欢迎参加赌博赢钱游戏')
money = 1000
while money>0:
    print('玩家总资产:',money)
    while True:
        yazhu = int(input('请输入押注:'))
        if 0<yazhu<=money:
           break
    first_point=roll_dice(2)
    print('玩家摇出了%d点' %first_point)
    go_on=False
    if first_point==7 or first_point==11:
        money=money+yazhu
        print('玩家胜' )
        go_on=False
    elif first_point==2 or first_point==3 or first_point==12:
        money-=yazhu
        print('庄家胜')
        go_on=False
    else:
        print('平局')
        print(money)
        go_on=True
        while go_on:
            current_point = roll_dice(2)
            print('摇出了%d点' % current_point)
            if current_point == 7:
                money -= yazhu
                print('庄家赢' )
                break
            elif current_point == first_point:
                money += yazhu
                print('玩家赢')
                break
            else:
                print('平局')

print('游戏结束')
from random import randint

"""
Craps 赌博游戏

Author:熊彪
Date:2018-03-02
version: 0.1

"""
PLAYER_WIN = 1  # 玩家获胜
COM_WIN = 0  # 电脑获胜
com_money = 1000  # 电脑初始金额
player_money = 1000  # 玩家初始金额


def calculation(debt, state):
    """
    该方法计算玩家和电脑的余额
    :param debt:赌注
    :param state:判断是否胜出
    :return:null
    """
    global com_money
    global player_money
    if state == PLAYER_WIN:
        com_money -= debt
        player_money += debt
    elif state == COM_WIN:
        com_money += debt
        player_money -= debt
    print('电脑余额: %d  玩家余额: %d' % (com_money, player_money), end='\n\n')


def recharge(charge_money):
    """
    对用户进行充值
    :param charge_money: 充值金额
    :return:null
    """
    global player_money
    player_money += charge_money


print('欢迎加入Craps游戏!目前你的余额为:%d  电脑的余额为:%d' % (player_money, com_money), end='\n\n')
game_over = False
while not game_over:
    while True:
        player_debt = int(input('玩家押注:'))
        if player_debt > player_money:
            print('押注失败,余额不足')
        else:
            break
    com_debt = randint(1, com_money)
    print('电脑押注:%d' % com_debt)
    face1 = randint(1, 6)
    face2 = randint(1, 6)
    my_point = face1 + face2
    print('第一次的点数为%d' % my_point)
    if my_point == 7 or my_point == 11:
        print('玩家胜')
        calculation(com_debt, PLAYER_WIN)
    elif my_point == 2 or my_point == 3 or my_point == 12:
        print('电脑胜')
        calculation(player_debt, COM_WIN)
    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('电脑胜')
                calculation(player_debt, COM_WIN)
                go_on = False
            elif current_point == my_point:
                print('玩家胜')
                calculation(com_debt, PLAYER_WIN)
                go_on = False
    if com_money <= 0:
        print('最终==玩家==获胜')
        game_over = True
    elif player_money <= 0:
        is_recharge = input('哈哈哈~你已经没有余额了是否需要充值???(Y/N)')
        if is_recharge == 'Y' or is_recharge == 'y':
            money = int(input('输入你的充值金额:'))
            recharge(money)
        else:
            print('最终==电脑==获胜')
            game_over = True

猜你喜欢

转载自blog.csdn.net/xiongbiao_xiongqi/article/details/79430607