Python变量、数据类型、数据类型转换相关函数用法实例详解
Python  /  管理员 发布于 7年前   200
本文实例讲述了Python变量、数据类型、数据类型转换相关函数用法。分享给大家供大家参考,具体如下:
说明:虽然python声明变量时没有一个类型来圈注,但它并不是弱类型语言,相反,它是一门强类型语言。
python3中移除了python2中的long
Python3中没有限制int数值的大小
>>> i=0b1111>>> print(i)15>>> i=0x0010>>> print(i)16>>> i=0o0010>>> print(i)8>>> i=0O0010>>> print(i)8
1.2e-5)
>>> a=1.5>>> print(a)1.5>>> a=-9999.5>>> print(a)-9999.5>>> a=1.5e5>>> print(a)150000.0>>> a=1.5e-10>>> print(a)1.5e-10>>> a=0.0000000000000001>>> print(a)1e-16
注:对于太小的数,会自动转化成科学计数法表示,太大的数不会自动转化
>>> type(True)<class 'bool'>>>> type(False)<class 'bool'>>>> a=bool(2)>>> aTrue>>> a=int(True)>>> a1>>> print(int(False))0
1
的字符串>>> type("aaaa")<class 'str'>>>> type('aaaa')<class 'str'>
>>> str1="123">>> str1[0]'1'>>> str1[-1]'3'
【 [:]代表完全切片,[:右下标]代表从零开始,[左下边:]代表结尾是最后一位,-1下标代表最后一位 】
【切片也可以有间隔,用法字符串名[左下标:右下标:间隔] 】
>>> hello="hello world!">>> new_hello=hello[:]>>> new_hello'hello world!'>>> hello[:2]'he'>>> hello[1:3]'el'>>> hello[1:-1]'ello world'
>>> hello[1:-1:1]'ello world'>>> hello[1:-1:2]'el ol'
>>> type("""hahahhahahahah""")<class 'str'>>>> type('''第一行第二行第三行''')<class 'str'>
>>> i=['a',100,True]>>> type(i)<class 'list'>
>>> list("abcd")['a', 'b', 'c', 'd']
>>> i['a', 100, True]>>> i[0]='b'>>> i['b', 100, True]
>>> i['b', 100, True]>>> l=[i,"helloworld"]>>> l[['b', 100, True], 'helloworld']>>> l[0][0]'b'
>>> l2=i*2>>> l2['b', 100, True, 'b', 100, True]>>> l3=i+l>>> l3['b', 100, True, ['b', 100, True], 'helloworld']
>>> t1=('a',1,True)>>> type(t1)<class 'tuple'>>>> t2=('a')>>> type(t2)<class 'str'>>>> ####注意上面的类型###>>> t3=('a',)>>> type(t3)<class 'tuple'>
>>> tuple2=1,2,3,4,5>>> type(tuple2)<class 'tuple'>>>> tuple2(1, 2, 3, 4, 5)
>>> t1=('a',i)>>> t1('a', ['b', 100, True])>>> id(t1[1])1673650817160>>> id(i)1673650817160
tuple一旦初始化就不能修改,所以它没有append()、insert(),也没有pop()等能增删元素的方法
>>> tuple1=(1,2,3,4)>>> print(tuple1.index(2))#查找指定元素的下标1>>> print(tuple1.count(2))#查找某元素存在的数量1
>>> d1={'a':'apple','b':'banana','c':'cabbage'}>>> type(d1)<class 'dict'>
>>> l1=['a']>>> d1[l1]='c'Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> d1[l1]='c'TypeError: unhashable type: 'list'
>>> d1{'a': 'apple', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'}>>> d1['a']'apple'>>> d1['d']Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> d1['d']KeyError: 'd'
>>> d1{'a': 'apple', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'}>>> d1['a']='apple pen'>>> d1{'a': 'apple pen', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'}
>>> d1{'a': 'apple pen', 'b': 'banana', 'c': 'cabbage', ('a',): 'c'}>>> d1['ai']='python'>>> d1{'a': 'apple pen', 'b': 'banana', 'c': 'cabbage', ('a',): 'c', 'ai': 'python'}
>>> dict10={1:"苹果","雪碧":"雪梨"}>>> >>> for i in dict10: print(i)1雪碧
>>> dict10{1: '苹果', '雪碧': '雪梨'}>>> 1 in dict10True>>> 3 in dict10False>>> print(dict10.get(3))None>>> print(dict10.get(1))苹果
>>> s1={'a','b','c'}>>> s1{'b', 'c', 'a'}>>> type(s1)<class 'set'>
>>> s2=set()>>> type(s2)<class 'set'>>>> s3=set(['a','b','c'])>>> type(s3)<class 'set'>
>>> s3.add(['cbd'])Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> s3.add(['cbd'])TypeError: unhashable type: 'list'
int()
函数可以把其他数据类型转换为整数:
>>> print(type(int("123")))<class 'int'>>>> print(type(float("123")))<class 'float'>>>> float("123")123.0>>> str(123)'123'>>> bool(2)True>>> list("123")['1', '2', '3']>>> tuple("123")('1', '2', '3')
注:转换是有规则的,要符合规则才能转化,比如int()不能转换"abc"
关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号