赞
踩
基本数据类型「number数字类型:整数int、浮点数float、bool布尔类型、complex复数(用j表示);字符串str;列表;元组;字典」
type()方法判断数据类型
- >> type(1) #1为整数型
- <class 'int'>
- >>> type(1.0) #1.0为浮点数型
- <class 'float'>
- >>> type(1j) #1j为复数型
- <class 'complex'>
-
- >>> type(True) #数值为布尔类型
- <class 'bool'>
- >>> type(False)
- <class 'bool'>
-
- >>> type('1') #'1'为字符串类型
- <class 'str'>
- >>> type([1]) #[1]为列表类型
- <class 'list'>
- >>> type((1,)) #(1,)为元组类型
- <class 'tuple'>
-
- >>> type((1)) #注意(1)表示运算
- <class 'int'>
- >>> type({'a':1}) #{'a':1}为字典类型
- <class 'dict'>

序列:字符串、列表、元组「每个元素会被分配序号,即偏移量,从0开始」
- #字符串的拼接
- >>> 'hello'+'world'
- 'helloworld'
- >>>'hello'*3
- 'hellohellohello'
-
- #获取字符串中的字符
- >>>'hello world'[5] #获取字符串中的第5个字符「首个字符偏移量为0,即从0取起」
- ' '
- >>> 'hello world'[-5] #获取字符串中的倒数第5个字符
- 'w'
-
- >>> 'hello world'[0:5] #获取字符串中前5个字符,切片原则「左取右不取」
- 'hello'
- >>> 'hello world'[0:-5] #获取字符串中从头至倒数第五的字符段,切片原则「左取右不取」
- 'hello '
- >>> 'hello world'[:-5] #获取字符串中从头至倒数第五的字符段,切片原则「左取右不取」
- 'hello '
-
- >>> 'hello world'[5:] #获取字符串中从第5个字符段开始后的字符,切片原则「左取右不取」
- ' world'
- >>> 'hello world'

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。