基础python知识(基础数据类型)——入门砖头里最外砖

看了人工智能基础教程: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基础数据知识
(也有些所有语言通用的知识)

路上热闹了许多
不怕了,还是???

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值