Python的第一周学习总结

day01

2018.2.26
今天第一个学习内容就是python之禅,在python交互式环境下输入import this即可看到其内容。Python之禅可以说是官方给的一种用python语言写代码的一种规则,当自己不太明确自己代码写的怎么样的时候可以参照python之禅。

import this

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

然后还初步的学习了怎么使用turtle进行简单的画图

import turtle
t= turtle.Turtle()
t.fillcolor('black')
t.begin_fill()
t.speed(10)
t.circle(25)
t.end_fill()

a=1
for i in range(120):
    if 0<=i<30 or 60<=i<90:
        a=a+0.2
        t.lt(3) #向左转3度
        t.fd(a) #向前走a的步长
    else:
        a=a-0.2
        t.lt(3)
        t.fd(a)

turtle.mainloop()

通过第一天的学习,认识了Python这种语言,更加明确了自己以后的发展道路,再今后的学习日子里,送给自己四个字“不忘初心”!

day 02

2018.2.27

学习的重点内容:

内容一:

变量类型(python的内置函数)
int():将一个数值或字符串转换成整数,可以指定进制。
float():将一个字符串转换成浮点数。
str():将指定的对象转换成字符串形式,可以指定编码。
chr():将整数转换成该编码对应的字符串(一个字符)。
ord():将字符串(一个字符)转换成对应的编码(整数)。

内容二:

运算符
赋值运算符:=
算术运算符:+ - * / // % **
关系运算符:> >= < <= == !=
逻辑运算符:and or not

内容三:

变量的命名规则
1. 变量名由字母、数字和下划线构成,数字不能开头
2. 大小写敏感
3. 不要关键字保留字冲突

官方建议:用全小写字母,多个单词用下划线连接。见名知意。

内容四:

Python在语言层面没有定义常量的语法
但是我们可以通过吧变量名用全大写来做一个隐含提示(隐喻)
全大写的变量要当做常量来看待,在代码中不能修改它的值

经验提示:符号常量总是优于字面常量

相关练习

练习一:华氏温度摄氏温度双向转换

"""
华氏温度摄氏温度的双向转换

Version: 0.1
Author: zcj
Date: 2018-02-27
"""
a=float(input('请输入一个温度值:'))
b=input('请输入该温度的单位:')
b='摄氏'
C=a
F=C*1.8+32
print('%f摄氏温度等于%f华氏温度'%(C,F))
b='华氏'
F=a
C=(F-32)/1.8
print('%f华氏温度等于%f摄氏温度'%(F,C))

练习二: 游泳池输入半径,过道宽3m,25元一平米,外过道砌围墙,围墙每米35.5元

"""
输入半径计算圆的周长和面积

Version: 0.1
Author: zcj
Date: 2018-02-27
"""
import math
radius=float(input('请输入泳池的半径:'))
money=(math.pi*(radius+3)**2 - math.pi*radius**2)*25 + 2*math.pi*(radius+3)*35.5
print('需要花%.2f元'%money)

练习三:给三条边长,算面积周长

"""
输入三条边的边长,计算三角形的面积周长

Version: 0.1
Author: zcj
Date: 2018-02-27
"""
import math
a=float(input('请输入第一条条边的边长:'))
b=float(input('请输入第二条条边的边长:'))
c=float(input('请输入第三条条边的边长:'))
if a+b>c and a+c>b and b+c>a  :
    L=a+b+c
    print('这个三角形的周长是%f'%L)
    S=math.sqrt(0.5*L*(0.5*L-a)*(0.5*L-b)*(0.5*L-c))
    print('这个三角形的面积是%f'%S)
else :
    print('这三条边不能构成一个三角形')

day 03

2018.2.28

学习的重点内容

分支结构

在Python中,要构造分支结构可以使用if、elif和else关键字。

print输出语句默认连接符号为空格,可用sep进行设置;结束语句默认为换行,可用end进行设置,如下所示。

for x in range(1000)
    print(x,'hello,world!',sep = ':',end = '/t')

循环结构

在Python中构造循环结构有两种做法,一种是for-in循环,一种是while循环

for-in循环

如果明确的知道循环执行的次数,则推荐使用for-in循环,格式为 for _ in range()
range类型可以用来产生一个不变的数值序列

  • range(101)可以产生一个0到100的整数序列。
  • range(1, 100)可以产生一个1到99的整数序列。
  • range(1, 100, 2)可以产生一个1到99的奇数序列,其中的2是步长,即数值序列的增量。
while循环

如果要构造不知道具体循环次数的循环结构,我们推荐使用while循环,while循环通过一个能够产生或转换出bool值的表达式来控制循环,表达式的值为True循环继续,表达式的值为False循环结束。

注意: while循环可用break关键字结束,break只能终止它所在的那个循环。除此之外还有一个关键字continue,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。

相关练习:

练习一:反转的猜数字

"""
输入一个数字,让电脑来猜

Version: 0.1
Author: zcj
Date: 2018-02-28
"""
from random import randint
a=int(input('请输入你需要让计算机猜的数字'))
x=0
y=100
b = randint(x,y)
print(b)
counter = 0
while True :
    c=int(input('1代表猜大了,2代表猜小了:'))
    counter += 1
    if c==1 :
        y=0.5*y
        b=randint(x,y)
        print(b)
    elif c==2 : 
        x=0.5*(y+x)
        b=randint(x,y)
        print(b)
    else:
        print('我猜了%d次终于猜对了'%counter)
        break

练习二:人机猜拳123,初始都有1000块,钱输了结束

"""
猜拳游戏

Version: 0.1
Author: zcj
Date: 2018-02-28
"""
from random import randint
computer_money=1000
my_money=1000
print('1表示石头,2表示剪刀,3表示布')
while my_money and computer_money != 0 :
    my_number= int(input('请输入你猜拳的数字:'))
    computer_number = randint(1,3)
    if computer_number == 1 and my_number == 2 :
        computer_money += 100
        my_money -= 100
        print('你输了,电脑赢了100,你还剩%d,电脑还剩%d'%(my_money,computer_money))
    elif computer_number == 2 and my_number ==3 :
        computer_money += 100
        my_money -= 100
        print('你输了,电脑赢了100,你还剩%d,电脑还剩%d'%(my_money,computer_money))
    elif computer_number == 3 and my_number ==1 :
        computer_money += 100
        my_money -= 100
        print('你输了,电脑赢了100,你还剩%d,电脑还剩%d'%(my_money,computer_money))
    elif computer_number == 1 and my_number == 3 :
        my_money += 100
        computer_money -= 100
        print('你赢了了,电脑输了100,你还剩%d,电脑还剩%d'%(my_money,computer_money))
    elif computer_number == 2 and my_number ==1 :
        my_money += 100
        computer_money -= 100
        print('你赢了了,电脑输了100,你还剩%d,电脑还剩%d'%(my_money,computer_money))
    elif computer_number == 3 and my_number ==2 :
        my_money += 100
        computer_money -= 100
        print('你赢了了,电脑输了100,你还剩%d,电脑还剩%d'%(my_money,computer_money))
    else :
        print('你们出的一样,这局平局')
if computer_money == 0 :
    print('你赢了,电脑输光了')
else :
    print('电脑赢了,你输光了')

练习三:计算个人所得税

"""
总收入 - 五险一金 大于 三千五达到标准
(总收入 - 五险一金 -3500)* 税率 -速算扣除数
本月实际收入

Version: 0.1
Author: zcj
Date: 2018-02-28
"""
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 <= 1500 :
    tax = 0.03
    deduction = 0
elif diff <= 55500 :
    tax = 0.3
    deduction = 2755
elif diff <= 80000 :
    tax = 0.35
    deduction = 5505
else :
    tax = 0.42
    deduction = 13505
#ads()用于取绝对值
personal = abs(diff * tax -deduction)
print('个人所得税为¥%f元'%personal)
print('实际到手收入为¥%f元'%(salary-insurance-personal))

day 04

2018.3.1

学习的重要内容

每日笔记

三元条件运算符
如果a>b,条件成立取a,条件不成立取b
my_max = a > b and a or b
my_max = c > my_max and c or my_max

在python中,想交换两个变量的值,可直接用下述方法实现
a , b = b , a

提示:由于浮点数表示法的问题
在实际开发的过程中请不要做浮点数的==和!=的运算
print(0.1+0.2+0.3)
print(0.3+0.2+0.1)

穷举法-穷尽所有的可能性直到找到正确答案

chr 是将编码还原成字符 ord 是将字符还原成编码

print(ord('曾'))
print(chr(2636))

分支和循环结构的练习

相关练习

练习一:输出2到100之间的素数

"""
输出2到100之间的素数

Version: 0.1
Author: zcj
Date: 2018-02-29
"""
for x in range(2,101) :
    for y in range (2,x) :
        if x % y == 0 :            
            break
    else  :
        print(x,end='\t',sep='')

练习二:输出相应图形

"""
输出下列图形 
   *            *      A          A
  ***          **      BB        B B
 *****        ***      CCC      C C C
*******      ****      DDDD    D D D D

Version: 0.1
Author: zcj
Date: 2018-02-29
"""
a=6
for x in range(a) : 
    for _ in range(a - x - 1) :
        print(' ',end = '')
    for _ in range(2 * x + 1) :
        print('*',end = '')
    print()

row=6
for i in range(row):
    for j in range(row):
        if j < row - i - 1:
            print(' ', end='')
        else:
            print('*', end='')
    print()

for x in range(1,6) :
    for y in range(1,x+1) :
        print(chr(x+64),end ='')
    print()

c = 6    
for x in range(1,c) :
    for _ in range(c - 1 - x):
        print(' ',end ='')
    for y in range(1,x+1) :
        print(chr(x+64),end =' ')
    print()

练习三:五人捕鱼问题

"""
第一个人把捕到的鱼平均分成五份还多一条,多的一条扔掉自己拿走一份
第二个人以为第一个人没拿,又平均分成五份多一条,同样多的一条扔掉自己拿走一份
同样第三个人,第四个人,第五个人都是如此。
求五人最少补了多少条鱼
"""
Version: 0.1
Author: zcj
Date: 2018-3-1

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

练习四:求100到1000之间的水仙花数

"""
求100`999之间的水仙花数,如下
153 = 1 ** 3 + 5 ** 3 + 3 ** 3

Version: 0.1
Author: zcj
Date: 2018-3-1
"""
for x in range (100,1000):
    a = x // 100
    b = (x % 100) // 10
    c = x % 10
    if x == a ** 3 + b ** 3 + c ** 3 :
        print(x)

练习五:找出一万以内的完美数

注意:通过该程序应当学会怎样去优化一个程序。

优化程序可以从两个方面去入手:
1. 从代码的执行效率进行优化
2. 从代码的数量进行优化

"""
找出10000以内的完美数
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 +14

Version: 0.1
Author: zcj
Date: 2018-3-1
"""
from math import sqrt
from time import time
start = time()
for x in range(2,10000):
    z = 1
    for y in range(2,int(sqrt(x))+1):
        if x % y == 0 :
            z = z + y
            if  y != x // y:
                z = z + x//y
    if x == z :
        print(x)
end = time()
print(end - start,'秒')

day 05

2018.3.2

总结复习之前所学

写craps赌博小游戏

总结

经过第一周的学习,发现自己应该多加的练习,确实上课时候看老师写代码的时候很简单,自己一写起来各种各样的问题都有了。想起老师给我们讲的那个下棋的例子,量变引起质变,只有多做练习,多自己亲手敲代码,才可能有以后的顿悟。每天都给自己敲一个警钟,不忘初心,方得始终!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值