Python基础数据类型(一)
2019独角兽企业重金招聘Python工程师标准>>>
Python2标准的数据类型:
Number(数字)
String(字符串)
List(列表)
Tuple(元组)
Sets(集合)
Dictionary(字典)
数Number,字符串str ,列表 list ,元组tuple
可变:list
不可变:Number /str /tuple
可修改内部值:list
不可修改内部值:Number str tuple
切片:str list tuple
不能切片:数字
3.1.1Number(数字)
Python2 支持 int、float、bool、complex(复数) long
Int long
>>> var1 =1
>>> type(var1)
<type 'int'>
>>> var2 = 1111111111111111111111111111111111111
>>> type(var2)
<type 'long'>
Float浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,如,1.23x109和12.3x108是完全相等的。
但是对于很大或很小的浮点数,就必须用科学计数法表示,把10用e替代,1.23x109就是1.23e9
>>> var3 =1.32
>>>type(var3)
<type'float'>
>>> var4 =1.23e9
>>> var4
1230000000.0
Bool
布尔值和布尔代数的表示完全一致,一个布尔值只有True、False两种值,要么是True,要么是False
False: 0 空 None
True(非0,非空,notnone) False (0)
复数complex:
复数complex:2+3j(J): a +bj complex(a,b)
>>> 1+2j
(1+2j)
>>> a = 2+3j
>>> type(a)
<type 'complex'>
>>> 1j*2j
(-2+0j)
>>> a
(2+3j)
>>> a.real
2.0
>>> a.imag
3.0
3.2.1字符串
字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程语言中表示文本的数据类型。
两个字符串相等的充要条件是:长度相等,并且各个对应位置上的字符都相等。
Python中,用引号括起来的都是字符串,其中的引号可以是单引号也可以是双引号
甚至可以是三引号:
' abc ' 单引号
"abc" 双引号
''' abc ''' 三个单引号
""" abc""" 三个双引号
字符串替换
>>> a = 'hello world'
>>> b =a.replace('world','python')
>>> b
'hello python'
cmp方法比较两个对象,并根据结果返回一个整数。cmp(x,y)如果X< Y,返回值是负数 如果X>Y 返回的值为正数。
>>> cmp(a,b)
1
>>> cmp(b,a)
-1
字符串相加
>>> a +b
'hello worldhello python'
>>> a + ' '+b
'hello world hello python'
>>> a - b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for-: 'str' and 'str'
>>> a
'hello world'
>>> a*3
'hello worldhello worldhello world'
字符串查找
1 find()方法:
>>> a = 'range'
>>> a.find(r)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'r' is not defined
>>> r = 'r'
>>> a.find(r)
0
>>> a.index(r)
0
>>> a.find('b')
-1
Rfind:
>>> a.rfind(r)
0
>>> a = 'abca'
>>> a.find('a')
0
>>> a.rfind('a')
3
>>> a.find('a',1)
3
2index()方法:
>>> a.index(r)
0
>>> a.find('b')
-1
>>> a.index('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
区别:
python的index方法是在字符串里查找子串第一次出现的位置,类似字符串的find方法,不过比find方法更好的是,如果查找不到子串,会抛出异常,而不是返回-1
字符串分割
>>> a = 'name:haha,age:20$name:python,age:30$name:fef,age:55'
>>> b = a.split('[color=red][font="][size=9.0pt]字符串翻转
[/size][/font][/color][code]>>> b = a[::-1]
>>> a = 'range'
>>> b = a[::-1]
>>> b
'egnar'
字符串翻转
>>> a = 'range'
>>> b = a[::-1]
>>> b
'egnar'
复制代码
Count 计数
a = 'abcdabbbb'
a.count('a')
2
Expandtabs 把S中的tab字符扩展为空格,每个tab扩展为tabsize个空格,默认是8个
>>> a = 'my \tname'
>>> a.expandtabs(3)
'my name'
>>> a.expandtabs(4)
'my name'
>>> a.expandtabs(5)
'my name'
>>> a.expandtabs(6)
'my name'
>>> a.expandtabs(7)
'my name'
>>> a.expandtabs(8)
'my name'
>>> a.expandtabs(2)
'my name'
# isalnum 方法检查判断字符串是否包含字母数字字符。
>>> a = 'my name is python'
>>> b = 'today123'
>>> a.isalnum()
False
>>> b.isalnum()
True
# isalpha 检查字符串是否仅由字母组成,如果是返会True。
>>> a
'my name is python'
>>> b.isalpha()
False
>>> c = "range"
>>> c.isalpha()
True
# isdigit 检查字符串是否仅由数字组成,如果是返会True。
>>> b.isdigit()
False
>>> d = '123'
>>> d.isdigit()
True
Lstrip 删除字符串中的所有前导空格
>>> a = ' abd'
>>> a.lstrip()
'abd'
Rstrip 删除字符串的所有行尾空白
>>> a = ' abd '
>>> a.rstrip()
' abd'
dir(str) :查询字符串有什么内置方法
>>> help(str.strip)# 查询内置方法的具体办法
Help on method_descriptor:
strip(...)
S.strip([chars]) -> string or unicode
Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
List(列表)
List:是一个种有序的集合,可以随时添加或者删除其中的某个元素
列表是写在方括号[]之间、用逗号分隔开的元素列表
>>> L = [1,2]
>>> type(L)
<type 'list'>
>>> L = [1,2,'range',[1,3],(1,3)]
>>> type(L)
<type 'list'>
例如:
>>> list = ['abcd',786,2.23,'xuegod',70.2]
>>> list[0] = 1234
>>> print(list)
[1234, 786, 2.23, 'xuegod', 70.2]
1、List写在方括号之间,元素用逗号隔开。
2、和字符串一样,list可以被索引和切片。
3、List可以使用+操作符进行拼接。
4、List中的元素是可以改变的。
Python列表脚本操作符:列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。长度:len()组合:list1 + list2重复:list1 * number元素是否存在列表中: 3 in list1迭代: for x in [1,2,3]: print x,
列表操作包含以下函数:1.cmp(list1,list2):比较两个列表的元组
>>> list1 = [1,2,3]
>>> list2 = [1,3,4]
>>> cmp(list1,list2)
-1
>>> cmp(list2,list1)
1
>>> cmp(list1,list1)
0
2.len(list):列表元素个数
>>> list1
[1, 2, 3]
>>> len(list1)
3
>>> list3 = [1,2,3,[4,5]]
>>> len(list3)
4
3.max(list):返回列表元素的最大值
>>> list3
[1, 2, 3, [4, 5]]
>>> max(list3)
[4, 5]
>>> list4 = [2,3,4,[1,2]]
>>> max(list4)
[1, 2]
4.min(list):返回列表元素的最小值
>>> list3
[1, 2, 3, [4, 5]]
>>> min(list3)
1
5.list(seq):将元组转换为列表
>>> tuple1 = (1,2,3,[4,5])
>>> type(tuple1)
<type 'tuple'>
>>> list5 = list(tuple1)
>>> list5
[1, 2, 3, [4, 5]]
>>> type(list5)
<type 'list'>
List内置了有很多方法:
append()在列表的末尾添加新的对象
>>> list = [1,2,3,4,5]
>>> list.append(6)
>>> list
[1, 2, 3, 4, 5, 6]
>>> list
[1, 2, 3, 4, 5, 6]
>>> list2 = [7,8,9]
>>> list.append(list2)
>>> list
[1, 2, 3, 4, 5, 6, [7, 8, 9]]
L.extend(iterable) -- extend list by appendingelements from the iterable
extend() 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
>>> list
[1, 2, 3, 4, 5, 6, [7, 8, 9]]
>>> list
[1, 2, 3, 4, 5, 6, [7, 8, 9]]
>>> list2
[7, 8, 9]
>>> list.extend(list2)
>>> list
[1, 2, 3, 4, 5, 6, [7, 8, 9], 7, 8, 9]
转载于//my.oschina.net/54188zz/blog/3019120
还没有评论,来说两句吧...