Python变量、数据类型、数据类型转换相关函数用法实例详解

发布时间:2020-10-22 06:32:08 作者:随风行云
来源:脚本之家 阅读:174

本文实例讲述了Python变量、数据类型、数据类型转换相关函数用法。分享给大家供大家参考,具体如下:

python变量的使用不需要进行类型声明(类型名 变量名),给一个变量名赋什么值就是什么类型。

Python变量、数据类型、数据类型转换相关函数用法实例详解

说明:虽然python声明变量时没有一个类型来圈注,但它并不是弱类型语言,相反,它是一门强类型语言。

为什么说 Python 是强类型语言


Python变量命名规则:

Python变量、数据类型、数据类型转换相关函数用法实例详解


数据类型

整数int类型:

python3中移除了python2中的long

Python3中没有限制int数值的大小

Python变量、数据类型、数据类型转换相关函数用法实例详解

>>> 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', 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']

元组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, 2, 3, 4, 5)
>>> t1=('a',i)
>>> t1
('a', ['b', 100, 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>", 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 dict10
True
>>> 3 in dict10
False
>>> 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'>

Python变量、数据类型、数据类型转换相关函数用法实例详解

Python变量、数据类型、数据类型转换相关函数用法实例详解

>>> s3.add(['cbd'])
Traceback (most recent call last):
 File "<pyshell#37>", line 1, 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', '2', '3')

注:转换是有规则的,要符合规则才能转化,比如int()不能转换"abc"

Python变量、数据类型、数据类型转换相关函数用法实例详解

关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

推荐阅读:
  1. C#的变量、数据类型转换、转义符
  2. python isinstance函数用法详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 变量 数据类型

上一篇:海量账户大并发实时查询解决方案

下一篇:Go使用defer函数要注意的几个点

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》