看了人工智能基础教程:Python篇(青少版)第二章总结
1.基本数据类型
1.1分数
from fractions import Fraction #导入fractions.Fraction
f = Fraction(1,2)
print("print Fraction({0}):{0}".format(f))
Run:
print Fraction(1,2):1/2
f = Fraction(20,40)
print("print Fraction({0}):{0}".format(f))
Run:
print Fraction(1,2):1/2 #说明带有自动约分
f = Fraction(0.5)
print("print Fraction({0}):{0}".format(f))
Run:
print Fraction(1,2):1/2 #带小数点的一个数自动转为分数
f = Fraction('0.5')
print("print Fraction({0}):{0}".format(f))
Run:
print Fraction(1,2):1/2 #说明数字不受引号限制
1.2复数
在python中,用j来定义复数的虚部
a = 2 + 3j
b = complex(4,9)
print(type (a))
print(type (b))
Run:
<class 'complex'> #complex为复数
<class 'complex'>
#复数的加减乘除(有兴趣可以复习下高中数学,interesting)
print(a * b)
Run:(-19 + 30j) #只写一个
1.2字符串
Python中有两种字符串列表取值顺序:从左到右索引,从右到左索引
没错,你没听错
从左到右索引:将第一个字符串标为0,依次增加下标.
从右到左索引:将最后一个字符串标为-1,往左依次减1.
#字符串感受代码,:为此之后的所有数据,*为乘,字符串类型为:String
str = "hello world"
print(str)
print(str[0])
print(str[3:7])
print(str[1:])
print(str*4)
print(str+"type:String")
Run:
hello world
h
lo w
ello world
hello worldhello worldhello worldhello world
hello worldtype:String
1.3布尔型
布尔型只有 True 和 False 两种值
Python将0、空字符””和None看成False,其他数值和非空字符串都看成True
print(True)
print(False)
print(True and "second")
print(False and "second")
print(True or "second")
Run:
True
False
second #and:串联,必须前后都满足才输出true,所以当第二项满足才输出
False
True #or:并联,满足一项就输出,所有满足第一项就会输出
1.4趣味练习(这里懂了,前面不用看了,🙊)
from fractions import Fraction
number = eval(input("您需要购买多少本此书?")) #eval函数将输入的字符串当成有效的表达式来求值并返回计算结果
cost = eval(input("此书单价是多少?"))
total = number * cost
discount = Fraction(3/4)
finally_total = total * discount
print("这批书的总价是{}".format(total))
print("打折力度是{}".format(discount))
print("实际您需要付费{}".format(finally_total))
print("您节约的金额为{}".format(total-finally_total))
PS:
第二章结束,
在学校没好好学,
现在算是开始学python。
记录下一些python基础数据知识
(也有些所有语言通用的知识)
路上热闹了许多
不怕了,还是???