Python变量、数据类型、数据类型转换相关函数用法实例详解
本篇章节讲解Python变量、数据类型、数据类型转换相关函数用法。分享给大家供大家参考,具体如下: python变量的使用不需要进行类型声明(类型名 变量名),给一个变量名赋什么值就是什么类型。
说明:虽然python声明变量时没有一个类型来圈注,但它并不是弱类型语言,相反,它是一门强类型语言。
为什么说 Python 是强类型语言Python变量命名规则:
数据类型整数int类型: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 小数float类型:
>>> 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 注:对于太小的数,会自动转化成科学计数法表示,太大的数不会自动转化 布尔bool类型:True、False
>>> type(True) <class 'bool'> >>> type(False) <class 'bool'> >>> a=bool(2) >>> a True >>> a=int(True) >>> a 1 >>> print(int(False)) 0 字符串str类型:
>>> 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("""hahah haha hahah""") <class 'str'> >>> type('''第一行 第二行 第三行''') <class 'str'>
列表list类型:
>>> i=['a',100,True] >>> type(i) <class 'list'>
>>> list("abcd") ['a','b','c','d']
>>> i ['a',True] >>> i[0]='b' >>> i ['b',True]
>>> i ['b',True] >>> l=[i,"helloworld"] >>> l [['b',True],'helloworld'] >>> l[0][0] 'b'
>>> l2=i*2 >>> l2 ['b',True,True] >>> l3=i+l >>> l3 ['b',['b','helloworld']
元组tuple类型:
>>> 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,5)
>>> t1=('a',i) >>> t1 ('a',True]) >>> id(t1[1]) 1673650817160 >>> id(i) 1673650817160
字典dict类型:
>>> 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>",in <module> d1['d'] KeyError: 'd'
>>> d1 {'a': 'apple',): 'c'} >>> d1['a']='apple pen' >>> d1 {'a': 'apple pen',): 'c'}
>>> d1 {'a': 'apple pen',): 'c'} >>> d1['ai']='python' >>> d1 {'a': 'apple pen',): 'c','ai': 'python'}
>>> dict10={1:"苹果","雪碧":"雪梨"} >>> >>> for i in dict10: print(i) 1 雪碧
>>> dict10 {1: '苹果','雪碧': '雪梨'} >>> 1 in dict10 True >>> 3 in dict10 False >>> print(dict10.get(3)) None >>> print(dict10.get(1)) 苹果
集合类型:
>>> s1={'a','c'} >>> s1 {'b','a'} >>> type(s1) <class 'set'>
>>> s2=set() >>> type(s2) <class 'set'> >>> s3=set(['a','c']) >>> type(s3) <class 'set'>
>>> s3.add(['cbd']) Traceback (most recent call last): File "<pyshell#37>",in <module> s3.add(['cbd']) TypeError: unhashable type: 'list'
数据类型转换相关函数:
>>> 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','3') 注:转换是有规则的,要符合规则才能转化,比如int()不能转换"abc" 关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》 希望本文所述对大家Python程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |