最近一直在想一个好办法来写文章,想来想去还是用使用案例的方式来写这些文章,这样就不是干巴巴的一些知识点,没多大意思,从今天开始,我们就进来细学Python的基础知识,这是第一篇文章。 小伙伴们可是迫不及待了啊,虽然很是基础,学过的小伙伴就巩固一下咯,没学过的小伙伴可要认真学习啦,
a, b, c, d = 200, 3.5, False, 5+6j
print(type(a))
>>> <class 'int'>表示整数类型
print(type(b)
>>> <class 'float'>表示浮点数类型
print(type(c))
>>> <class 'bool'>表示布尔类型
print(type(d))
>>> <class 'complex'>表示复数类型
g = 1.17e+18
print(g)
>>> 1.17e+18
print(bin(26))
>>> 0b11010
print(oct(26))
>>> 0o32
print(hex(26))
>>> 0x1a
print(int(35.8))
>>> 35
print(float(23))
>>> 23.0
print(oct(0x26))
>>> 0o46
print(int(0x26))
>>> 38
print(bin(0x26))
>>> 0b100110
print(isinstance(24,float))
>>> False
print(complex(5))
>>> 5+0j
print(complex(3,4))
>>> 3+4j
1. 通过调用float()函数,可以显示的将int类型强制转换成float类型数据
2. 通过调用int()函数,可以float()类型数据强制转换为int类型数据,取整
3. 通过调用type()函数可以得到任何数据的数据流类型
4. 通过isinstance()函数可以判断数据的类型
5. complex()将数据转换为复数形式
还是老样子,来个整体的例子
mystr = 'I\'am a student'
print(mystr,type(mystr),len(mystr))
>>> I'am a student <class 'str'> 14
print("c:\\address\name")
>>> c:\address
>>> ame
print(r"c:\\address\name")
>>> c:\\address\name
print('hello, '+mystr,mystr*2)
>>> hello, I'am a student I'am a studentI'am a student
print(mystr[3:5])
>>> m
print(mystr+'\My major is computer')
>>> I'am a student\My major is computer
print(mystr.find('am'))
>>> 2
print(mystr.lower(),mystr.upper())
>>> i'am a student I'AM A STUDENT
print(mystr.replace('student','teacher'))
>>> i'am a teacher
在Python中的字符串使用单引号('),双引号("),或者三引号(""",''')括起来的, 同时使用反斜杠()转义字符的一段文字。字符串是一个有序字符的集合, 用于储存和表示基本的文本信息,但是它只能存放一个值,一经定义,不可改变。
\' 单引号 \" 双引号 \a 发出系统想铃声 \b 退格符 \n 换行符 \t 横向制表符 \v 纵向制表符 \r 回车符 \f 换页符 \o 八进制 \x 十六进制 \000 终止符
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有