Python数据科学零基础一本通 作者:洪锦魁
先说一下做这个主要是因为作者
为了省本子,然后经常会出现请修改示例2什么什么的,然后又要翻记录,最最主要的是这本书上的例子是图片!!!
晓得图片是什么意思不,就是不能复制,当然了你买纸质书也复制不到电脑上。
我是大概看到了第二三章的时候,做后面的习题,还要去翻以前,我已经翻了三四次了。代码也重复敲了了三四次,而且网上也没有关于这本书的的课后习题什么的。
于是我决定自己动手丰衣足食,同时也给为这本书烦恼的宝宝们图个方便。
如果是没有购买这本书电子版或纸质版的也可以支持一下书的作者。
最后就是这里面的【插图】是图片,你们有书的话可以联合书一起看。
第二章 认识变量与基本数学运算
2-3 认识程序的意义
程序实例ch2_1.py:
使用程序计算每年可以存储多少钱,下面是整个程序设计。
时薪:hourly_salary,用此变量代替x,即每小时的薪资。
年薪:annual_salary,用此变量代替y,即一年工作所赚的钱。
月支出:monthly_fee,用此变量代替z,即每个月的花费。
年支出:annual_fee,用此变量代替a,即每年的花费。
年存储:annual_savings,用此变量代替b,即每年所存储的钱。
hourly_salary = 125 #小时工资
annual_salary = hourly_salary * 8 * 300 #年薪
monthly_fee = 9000 #月费
annual_fee = monthly_fee * 12 #年费
annual_savings = annual_salary - annual_fee #每年节省
print(annual_savings)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
192000
>>>
2-4 认识注释的意义
程序ch2_1.py中尽管已经为变量设置了有意义的名称,但时间一久,常常还是会忘记各个指令的内涵。
所以笔者建议,设计程序时,应适度地为程序代码加上注释。
在1-10节已经讲解了注释的方法,下面将直接以实例说明。
程序实例ch2_2.py:
重新设计程序ch2_1.py,为程序代码加上注释。
hourly_salary = 125 # 设置时薪
annual_salary = hourly_salary\
* 8 * 300 #计算年薪
monthly_fee = 9000 #设置每月花费
annual_fee = monthly_fee * 12 #计算每年花费
annual_savings = \ #计算每年存储金额
annual_salary - annual_fee
print(annual_savings) #列出每年存储金额
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
192000
>>>
2-11 Python的断行
2-11-1 一行有多个语句
在Python中允许一行有多个,彼此用“;”隔开即可,尽管Python有提供此功能,不过笔者不鼓励如此撰写程序代码。
程序实例ch2_3.py:
一行有多个语句的实例。
x = 10
print(x)
y = 20;print(y)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10
20
>>>
2-11-2 将一个语句分成多行
在设计大型程序时,常会碰上一个语句很长,需要分成两行或更多行撰写,此时可以在语句后面加上“\”符号,Python解释器会将下一行的语句视为这一行的语句。
特别注意,在“\”符号右边不可以加上任何符号或文字,即使是注释符号也是不允许的。
另外,也可以在语句内使用小括号,如果使用小括号,就可以在语句右边加上注释符号。
程序实例ch2_4.py:
将一个语句分成多行的应用。
a = b = c = 10
x = a + b + c + 12
print(x)
#续航方法1
y = a +\
b +\
c +\
12
print(y)
#续航方法2
z = ( a + #此处还可以加上注释
b +
c +
12)
print(z)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
42
42
42
>>>
2-12 专题——复利计算/计算圆面积与圆周长
2-12-1 银行存款复利的计算
程序实例ch2_5.py:
银行存款复利的计算。假设目前银行年利率是1.5%,复利公式如下:
[插图]
现有一笔5万元存款,请计算5年后的本金和。
money = 50000 * (1+0.015)**5
print('本金和是:%d'%money)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
本金和是:53864
>>>
2-12-2 计算圆面积与周长
程序实例ch2_6py:
假设圆半径是5cm,圆面积与圆周长计算公式分别如下:
'''
圆面积 = PI*r*r #Pi = 3.1415926,r是半径
圆周长 = 2*PI*r
'''
from math import pi
r = 5
print('圆面积:单位是平方厘米')
area = pi*r*r
print('%.6f'%area)
circurference = 2*pi*r
print('圆周长:单位是厘米㎝')
print('%.4f'%circurference)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
圆面积:单位是平方厘米
78.539816
圆周长:单位是厘米㎝
31.4159
>>>
习题
- 1.请重新设计c h2_1.py,将打工时薪改为150元。(2-1~2-3节) [插图]
hourly_salary = 150 #小时工资
annual_salary = hourly_salary * 8 * 300 #年薪
monthly_fee = 9000 #月费
annual_fee = monthly_fee * 12 #年费
annual_savings = annual_salary - annual_fee #每年节省
print('每年存款金额 %d'%annual_savings)
- 执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
每年存款金额 252000
>>>
-
2.重新设计ch2_5.py,假设是单利率,5年期间可以领多少利息?(2-5~2-7节) [插图]
-
关于这个单利复利的区别书上没有说。
-
单利就是利不生利,即本金固定,到期后一次性结算利息,而本金所产生的利息不再计算利息。
-
复利其实就是利滚利,即把上一期的本金和利息作为下一期的本金来计算利息。
本金是10万,月利率都是5%,一年后单利和复利分别是:
单利的算法:100000×(1+5%×12)=16万元
复利的算法:100000×(1+5%)^12=17.956万元
money = 50000 * (0.015*5)
print('本金和是:%d'%money)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
本金和是:3750
>>>
- 3.重新设计ch2_5.py,假设期初本金是100 000元,年利率是2%,这是复利计算,请问10年后本金总和是多少?(2-5~2-12节)[插图]
money = 100000 * (1+0.02)**10
print('本金和是:%f'%money)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
本金和是:121899.441999
>>>
- 4.一个幼儿园买了100个苹果给学生当营养午餐,学生人数是23人,每个人午餐可以吃一个,请问这些苹果可以吃几天?第几天苹果会不够供应?同时列出缺少了几个。(2-5~2-12节)
- 解这题用了while哈哈哈哈哈,不知道是不是朝纲了,上面说用(2-5~2-12节)的内容,好吧我再想想别的法子,这个就当解题法1吧!
- 解法1:
apple = 100
students = 23
sum1 = 0
while apple>=0:
#print('apple',apple,'students','students')
sum1 += 1
apple -= students
print('可以吃',sum1-1,'天')
print('第',sum1,'天不够供应')
print('缺少了',-apple,'个')
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
可以吃 4 天
第 5 天不够供应
缺少了 15 个
>>>
- 解法二:(这回不超纲)
apple = 100
students = 23
a,b=divmod(apple,students)
print('可以吃',a,'天')
print('第',a+1,'天不够供应')
print('缺少了',students-b,'个')
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
可以吃 4 天
第 5 天不够供应
缺少了 15 个
>>>
- 5.地球和月球的距离是384 400千米,假设火箭飞行速度是每分钟400千米,请问从地球飞到月球需要多少分钟?(2-5~2-12节)[插图]
earth_moon = 384400
rocket = 400
minutes = earth_moon / rocket
print('从地球飞到月球需要%.2f'%minutes)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
从地球飞到月球需要961.00
>>>
- 6.假设圆柱半径是20厘米,高度是30厘米,请计算此圆柱的体积。圆柱体积计算公式是:圆面积×圆柱高度。(2-5~2-12节)[插图]
from math import pi
r = 20
hight = 30
area = pi * r * r
volume = area * hight
print('圆柱体积:单位是立方厘米')
print('圆柱的体积是%.2f'%volume)
'''
书上计算出来的是圆柱的体积是37699.080000,
用的Π是到3.14159
'''
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
圆柱体积:单位是立方厘米
圆柱的体积是37699.11
>>>
- 7.圆周率PI是一个数学常数,常常使用希腊字母表示,它的物理意义是圆的周长和直径的比率。历史上第一个无穷级数公式称为莱布尼茨公式,它的计算公式如下:(2-5~2-12节)
- 请分别计算下列级数的执行结果。
- (1)[插图]
- (2)[插图]
- (3)[插图]
PI1 = 4*(1-1/3+1/5-1/7+1/9)
PI2 = 4*(1-1/3+1/5-1/7+1/9-1/11)
PI3 = 4*(1-1/3+1/5-1/7+1/9-1/11+1/13)
print('PI的值4*(1-1/3+1/5-1/7+1/9)')
print(PI1)
print('PI的值4*(1-1/3+1/5-1/7+1/9-1/11)')
print(PI2)
print('PI的值4*(1-1/3+1/5-1/7+1/9-1/11+1/13)')
print(PI3)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
PI的值4*(1-1/3+1/5-1/7+1/9)
3.3396825396825403
PI的值4*(1-1/3+1/5-1/7+1/9-1/11)
2.9760461760461765
PI的值4*(1-1/3+1/5-1/7+1/9-1/11+1/13)
3.2837384837384844
>>>
- 注上述级数如果要收敛到我们熟知的3.14159需要相当长的级数计算。
- [插图]
- 莱布尼茨(Leibniz,1646—1716)是德国人,在世界数学舞台上占有一席之地,他本人另一个职业是律师,许多数学公式都是他在各大城市通勤期间完成的。数学历史上有一个两派说法的无解公案,有人认为他是微积分的发明人,也有人认为发明人是牛顿(Newton)。
- 8.尼拉卡莎级数也是应用于计算圆周率PI的级数,此级数收敛的速度比莱布尼茨级数更好,更适合于用来计算PI,它的计算公式如下:
- [插图]
- 请分别计算下列级数的执行结果。
- (a)[插图]
- (b)[插图]
- [插图]
PI1=3+4/(2*3*4)-4/(4*5*6)+4/(6*7*8)
PI2=3+4/(2*3*4)-4/(4*5*6)+4/(6*7*8)-4/(8*9*10)
print('PI的值3+4/(2*3*4)-4/(4*5*6)+4/(6*7*8)')
print(PI1)
print('PI的值3+4/(2*3*4)-4/(4*5*6)+4/(6*7*8)-4/(8*9*10)')
print(PI2)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
PI的值3+4/(2*3*4)-4/(4*5*6)+4/(6*7*8)
3.145238095238095
PI的值3+4/(2*3*4)-4/(4*5*6)+4/(6*7*8)4/(8*9*10)
3.1396825396825396
>>>
第三章 Python的基本数据类型
3-1 type( )函数
在正式介绍Python的数据类型前,笔者想介绍一下type( )函数,这个函数可以列出变量的数据类型类别。
这个函数在读者未来进入Python实战时非常重要,因为变量在使用前不需要声明,同时在程序设计过程中变量的数据类型会改变,我们常常需要使用此函数判断目前的变量数据类型。
或是在进阶Python应用中,会调用一些方法(method),这些方法会返回一些数据,可以使用type( )获得所返回的数据类型。
程序实例ch3_1.py:
列出数值变量的数据类型。
x = 10
y = x / 3
print(x)
print(type(x))
print(y)
print(type(y))
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10
<class 'int'>
3.3333333333333335
<class 'float'>
>>>
3-2 数值数据类型
3-2-3 基本数值数据的使用
Python在声明变量时可以不用设置这个变量的数据类型,未来如果这个变量内容是放整数,这个变量就是整数(int)数据类型,如果这个变量内容是放浮点数,这个变量就是浮点数数据类型。整数与浮点数最大的区别是,整数不含小数点,浮点数含小数点。
程序实例ch3_2.py:
测试浮点数。
x = 10.0
print(x)
print(type(x))
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10.0
<class 'float'>
>>>
3-2-4 整数与浮点数的运算
Python程序设计时不相同的数据类型也可以执行运算,程序设计时常常会发生整数与浮点数之间的数据运算,Python具有简单的自动转换能力,在计算时会将整数转换为浮点数再执行运算。
程序实例ch3_3.py:
不同数据类型的运算。
x = 10
y = x + 5.5
print(x)
print(type(x))
print(y)
print(type(y))
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10
<class 'int'>
15.5
<class 'float'>
>>>
上述变量y,由于是整数与浮点数的加法,所以结果是浮点数。此外,某一个变量如果是整数,但是如果最后所存储的值是浮点数,Python也会将此变量转成浮点数。
程序实例ch3_4.py:
整数转换成浮点数的应用。
x = 10
print(x)
print(type(x))
x = x + 5.5
print(x)
print(type(x))
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10
<class 'int'>
15.5
<class 'float'>
>>>
3-2-5 二进制整数与函数bin( )
可以用二进制方式代表整数,Python中定义凡是以0b开头的数字,代表这是二进制的整数。bin( )函数可以将一般整数数字转换为二进制。
程序实例ch3_5.py:
将十进制数值与二进制数值互转的应用。
x = 0b1101
print(x)
y = 13
print(bin(y))
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
13
0b1101
>>>
3-2-6 八进制整数与函数oct( )
可以用八进制方式代表整数,Python中定义凡是以0o开头的数字,代表这是八进制的整数。oct( )函数可以将一般数字转换为八进制。
程序实例ch3_6.py:
将十进制数值与八进制数值互转的应用。
x = 0o57
print(x)
y = 47
print(oct(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
47
0o57
>>>
3-2-7 十六进制整数与函数hex( )
可以用十六进制方式代表整数,Python中定义凡是以0x开头的数字,代表这是十六进制的整数。hex( )函数可以将一般数字转换为十六进制。
程序实例ch3_7.py:
将十进制数值与八进制数值互转的应用。
x = 0x5D
print(x)
y = 93
print(hex(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
93
0x5d
>>>
3-2-8 强制数据类型的转换
有时候设计程序时,可以自行强制使用下列函数,转换变量的数据类型。int( ):将数据类型强制转换为整数。float( ):将数据类型强制转换为浮点数。
程序实例ch3_8.py:
将浮点数强制转换为整数的运算。
x = 10.5
print(x)
print(type(x))
y = int(x) + 5
print(y)
print(type(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10.5
<class 'float'>
15
<class 'int'>
>>>
程序实例ch3_9.py:
将整数强制转换为浮点数的运算。
x = 10
print(x)
print(type(x))
y = float(x) + 10
print(y)
print(type(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
10
<class 'int'>
20.0
<class 'float'>
>>>
3-2-9 数值运算常用的函数
下列是数值运算时常用的函数。
abs( ):计算绝对值。
pow(x,y):返回x的y次方。
round( ):这是采用运算法则的Bankers Rounding概念,如果处理位数左边是奇数则使用四舍五入,如果处理位数左边是偶数则使用五舍六入,例如,round(1.5)=2,round(2.5)=2。
处理小数时,第2个参数代表取到小数第几位,1代表取到小数第1位。
根据保留小数位的后两位,采用"50"舍去,"51"进位,例如,round(2.15,1)=2.1,round(2.25,1)=2.2,round(2.151,1)=2.2,round(2.251,1)=2.3。
程序实例ch3_10.py:
abs( )、pow( )、round( )、round(x,n)函数的应用。
x = -10
print('以下输出 abs()函数的应用')
print(x)
print(abs(x))
x = 5
y = 3
print('以下输出pow()函数的应用')
print(pow(x,y))
x = 47.5
print('以下输出round(x)函数的应用')
print(x)
print(round(x))
x = 48.5
print(x)
print(round(x))
x = 49.5
print(x)
print(round(x))
print('以下输出round(x,n)函数的应用')
x = 2.15
print(x)
print(round(x,1))
x = 2.25
print(x)
print(round(x,1))
x = 2.151
print(x)
print(round(x,1))
x = 2.251
print(x)
print(round(x,1))
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
以下输出 abs()函数的应用
-10
10
以下输出pow()函数的应用
125
以下输出round(x)函数的应用
47.5
48
48.5
48
49.5
50
以下输出round(x,n)函数的应用
2.15
2.1
2.25
2.2
2.151
2.2
2.251
2.3
>>>
3-3 布尔值数据类型
Python的布尔值(Boolean)数据类型的值有两种,True(真)或False(伪)。它的数据类型代号是bool。布尔值一般应用在程序流程的控制中,特别是在条件表达式中,程序可以根据这个布尔值判断应该如何执行下一步工作。
程序实例ch3_11.py:
列出布尔值True与布尔值False的数据类型。
x = True
print(x)
print(type(x))
y = False
print(y)
print(type(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
True
<class 'bool'>
False
<class 'bool'>
>>>
程序实例ch3_12.py:
将布尔值强制转换为整数,同时列出转换的结果。
x = True
print(int(x))
print(type(x))
y = False
print(int(y))
print(type(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
1
<class 'bool'>
0
<class 'bool'>
>>>
程序实例ch3_13.py:
将布尔值与整数值相加,并观察最后变量数据类型,可以发现,最后的变量数据类型是整数值。
xt = True
x = 1 + xt
print(x)
print(type(x))
yt = False
y = 1 + yt
print(y)
print(type(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
2
<class 'int'>
1
<class 'int'>
>>>
3-4 字符串数据类型
程序实例ch3_14.py:
使用单引号与双引号设置与输出字符串数据的应用。
x = 'DeepStone means Deep Learning'
print(x)
print(type(x))
y = '深石数字-深度学习滴水穿石'
print(y)
print(type(y))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
DeepStone means Deep Learning
<class 'str'>
深石数字-深度学习滴水穿石
<class 'str'>
>>>
3-4-1 字符串的连接
程序实例ch3_15.py:
字符串连接的应用。
num1 = 222
num2 = 333
num3 = num1 + num2
print('以下是数值相加')
print(num3)
numstr1 = '222'
numstr2 = '333'
numstr3 = numstr1 + numstr2
print('以下是字符串组成的字符串相加')
print(numstr3)
numstr4 = numstr1 + ' ' + numstr2
print('以下是由数值组成的字符串相加,\
同时中间加上一些空格')
print(numstr4)
str1 = 'DeepStone '
str2 = 'Deep Learning'
str3 = str1 + str2
print('以下是一般的字符串相加')
print(str3)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
以下是数值相加
555
以下是字符串组成的字符串相加
222333
以下是由数值组成的字符串相加,同时中间加上一些空格
222 333
以下是一般的字符串相加
DeepStone Deep Learning
>>>
3-4-2 处理多于一行的字符串
程序设计时如果字符串长度多于一行,可以使用三个单引号(或是三个双引号)将字符串括起来即可。
程序实例ch3_16.py:
使用三个单引号处理多于一行的字符串。
str1 = '''Silicon Stone Education is an unbiased
organization concentrated on bridging the gap ...'''
print(str1)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
Silicon Stone Education is an unbiased
organization concentrated on bridging the gap ...
>>>
3-4-3 转义字符
程序实例ch3_17.py:
转义字符的应用,这个程序第9行增加了“\t”字符,所以“can’t”跳到下一个Tab键位置输出。同时有“\n”字符,这是换行符号,所以“loving”跳到下一行输出。
str1 = 'I can\'t stop loving you.'
print(str1)
str2 = "I can't stop loving you."
print(str2)
str3 = "I \tcan't stop \nloving you."
print(str3)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
I can't stop loving you.
I can't stop loving you.
I can't stop
loving you.
>>>
3-4-4 str( )函数
程序实例ch3_18.py:
使用str( )函数将数值数据强制转换为字符串的应用。
num1 = 222
num2 = 333
num3 = num1 + num2
print('这是数值相加')
print(num3)
str1 = str(num1) + str(num2)
print('强制转换为字符串相加')
print(str1)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
这是数值相加
555
强制转换为字符串相加
222333
>>>
3-4-5 将字符串转换为整数
程序实例ch3_19.py:
将字符串数据转换为整数数据的应用。
x1 = '22'
x2 = '33'
x3 = x1 + x2
print(x3)
x4 = int(x1) + int(x2)
print(x4)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
2233
55
>>>
3-4-6 字符串与整数相乘产生字符串复制效果
程序实例ch3_20.py:
字符串与整数相乘的应用。
x1 = 'A'
x2 = x1 * 10
print(x2)
x3 = 'ABC'
x4 = x3 * 5
print(x4)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
AAAAAAAAAA
ABCABCABCABCABC
>>>
3-4-7 聪明地使用字符串加法和换行字符\n
程序实例ch3_21.py:
将数据分行输出的应用。
str1 = '洪锦魁著作'
str2 = 'HTML5+CSS3王者归来'
str3 = 'Python数据科学零基础一本通'
str4 = str1 + '\n' + str2 + '\n' + str3
print(str4)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
洪锦魁著作
HTML5+CSS3王者归来
Python数据科学零基础一本通
>>>
3-4-8 字符串前加r
程序实例ch3_22.py:
字符串前加上r的应用。
str1 = 'Hello!\nPython'
print('不含r字符的输出')
print(str1)
str2 = r'Hello!\nPython'
print('含r字符的输出')
print(str2)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
不含r字符的输出
Hello!
Python
含r字符的输出
Hello!\nPython
>>>
3-5 字符串与字符
3-5-2 Unicode码
程序实例ch3_23.py:
这个程序首先会将整数97转换成英文字符‘a’,然后将字符‘a’转换成Unicode码值,最后将中文字‘魁’转成Unicode码值。
x1 = 97
x2 = chr(x1)
print(x2)
x3 = ord(x2)
print(x3)
x4 = '魁'
print(hex(ord(x4)))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
a
97
0x9b41
>>>
3-7 专题——地球到月球时间计算/计算坐标轴两点之 间的距离
3-7-1 计算地球到月球所需时间
程序实例ch3_24.py:
从地球到月球约384 400千米,假设火箭的速度是一马赫,设计一个程序计算需要多少天、多少小时才可抵达月球。这个程序省略分钟数。
dist = 384400
speed = 1225
total_hours = dist // speed
days = total_hours // 24
hours = total_hours % 24
print('总共需要天数',days)
print('小时数',hours)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
总共需要天数 13
小时数 1
>>>
程序实例ch3_25.py:
使用divmod( )函数重新设计ch3_24.py。
dist = 384400
speed = 1225
total_hours = dist // speed
days,hours = divmod(total_hours,24)
print('总共需要天数',days)
print('小时数',hours)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
总共需要天数 13
小时数 1
>>>
3-7-2 计算坐标轴两个点之间的距离
程序实例ch3_26.py:
有两个点坐标分别是(1, 8)与(3, 10),请计算这两个点之间的距离。
x1,y1 = 1,8
x2,y2 = 3,10
dist = ((x1 - x2)**2+((y1-y2)**2))**0.5
print('两点的距离是:',dist)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
两点的距离是: 2.8284271247461903
>>>
习题
- 假设a是10,b是 18,c是5,请计算下列执行结果,取整数结果。(3-2节)(书上的第一题好像有点错误)
(a) s = a + b – c
(b) s = 2 * a + 3 – c
(c) s = b * c + 20 / b
(d) s = a % c * b + 10
(e) s = a ** c – a * b * c
a,b,c = 10,18,5
sa = a + b - c
sb = 2 * a + 3 - c
sc = b * c + 20 / b
sd = a % c * b + 10
se = a ** c - a * b *c
print(sa)
print(sb)
print(int(sc))
print(sd)
print(se)
执行结果:
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
23
18
91
10
99100
(以后的练习题我就打一二三四了,他这个莫名其妙给你安排序列真的太累)
2.
money = 50000 * (0.015*5)
print('本金和是:',int(money))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
本金和是: 3750
>>>
money = 50000 * (0.015*5)
print('本金和是:',round(money))
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
本金和是: 3750
>>>
4.这题到底想要求什么???
如果是分开求,那分钟数只是37分钟就到达月球了???
这分明不可能
如果是合起来求,需要一天25个小时???
一天有二十四个小时那他为什么不和起来两天一个小时???
下面是我按照合起来求的算法。
earth_moon = 384400
rocket = 250
days = earth_moon / rocket//60//24
hours = earth_moon / rocket//60-days*24
minutes = earth_moon / rocket - \
(days*24*60+hours*60)
print('天总数:')
print(days)
print('小时数:')
print(hours)
print('分钟数:')
print('%d'%minutes)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
天总数:
1.0
小时数:
1.0
分钟数:
37
>>>
>>> ord('芽');ord('念')
33469
24565
>>>
>>> hex(33469);hex(24565)
'0x82bd'
'0x5ff5'
>>>
>>> 'Python王者归来'.encode()
b'Python\xe7\x8e\x8b\xe8\x80\x85\xe5\xbd\x92\xe6\x9d\xa5'
>>>
8
是我理解错了嘛这题???为什么我觉得分钟输出是这样的。
dist = 384400
speed = 1225
total_hours = dist / speed
days = total_hours // 24
hours = total_hours - (days*24)
minutes = (hours-1) * 100
seconds = (minutes-79) * 100
print(total_hours)
print('总共需要天数:',days)
print('小时数:',hours)
print('分钟:%.2f'%minutes)
print('秒钟:%.2f'%seconds)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
313.7959183673469
总共需要天数: 13.0
小时数: 1.7959183673469283
分钟:79.59
秒钟:59.18
>>>
x1,y1=1,8
x2,y2=0,0
dist = ((x1 - x2)**2+((y1-y2)**2))**0.5
print('(1,8)点与(0,0)点的距离是:',dist)
x3,y3=3,10
dist1 = ((x3 - x2)**2+((y3-y2)**2))**0.5
print('(3,10)点与(0,0)点的距离是:',dist1)
= RESTART: D:\BianChengKaiFaHuanJing\Python\PythonCode\orange.py
(1,8)点与(0,0)点的距离是: 8.06225774829855
(3,10)点与(0,0)点的距离是: 10.44030650891055
>>>
本来想都写在这个上面的结果太多了,现在差不多有两万的字了,我这章怕不是这上面最长的文章吧??
本文介绍了Python编程中的变量、基本数学运算和数据类型,包括变量的意义、注释的重要性、Python的断行规则、复利计算、圆周率计算以及布尔值和字符串的使用。通过示例程序和课后习题,深入浅出地讲解了Python的基础知识。

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



