Python全栈开发Day2

  第二章 python数据类型


 

目录


1.布尔型

真或假
 1 或 0
True或False

2.整型

   Python中整数属于int类型,默认十进制,支持二进制,八进制,十六进制的表示方式

>>> bin(10)#二进制
'0b1010'
>>> oct(10)#八进制
'0o12'
>>> hex(10)#十六进制
'0xa'
>>>

3.浮点型

   浮点数是属于·有理数中某特定子集的数的数字示,在计算机中用近似表示任意某个实数。Python默认是17位精度,就是小数点厚16位。

>>> a = 3.151592653513651054608317828332
>>> a
3.151592653513651
>>>

 

4.字符串

   字符串是一个有序的字符的集合

>>> s = 'hello'
>>> s[0]#索引
'h'
>>> s[1]
'e'
>>> s[-1]
'o'
>>> s.index('e')
1
>>> s.find('e')#查找
1
>>> a = '  hello,world   '
>>> a
'  hello,world   '
>>> a.strip()#移除空白
'hello,world'
>>> a.lstrip()#移除左空白
'hello,world   '
>>> a.rstrip()#移除右空白
'  hello,world'
>>> b = 'hello,world'
>>> len(b)#长度
11
>>> b.replace('h','H')#替换
'Hello,world'
>>> s = 'helloworld'
>>> s[0:5]#切片
'hello'
>>> s[:5]#取前5位
'hello'
>>> s[5:]#取后所有
'world'
>>> s[:]#取全部
'helloworld'
>>>
>>> s[0:5:2]
'hlo'
>>> s[::2]#从h开始隔一个取一个
'hlool'
>>> s[::-1]#取反
'dlrowolleh'
>>>

5.列表

list: 列表, 有序的项目, 通过索引进行查找, 使用方括号"[]";

>>> l = ['alex','cat','dog','pig']#查找
>>> l[0]
'alex'
>>> l[0:2]#切片
['alex', 'cat']
>>> l[::2]
['alex', 'dog']
>>> l.append('rabbit')#添加
>>> l
['alex', 'cat', 'dog', 'pig', 'rabbit']
>>> l.remove('rabbit')#删除
>>> l
['alex', 'cat', 'dog', 'pig']
>>> l.pop()#从最后一个开始删除
'pig'
>>> l
['alex', 'cat', 'dog']
>>> len(l)#长度
3
>>> 'cat' in l#包含
True
>>> for i in l:#循环
>>>   print(1)
['alex', 'cat', 'dog']
>>>

6.元祖

tuple: 元组, 元组将多样的对象集合到一起, 不能修改, 通过索引进行查找, 使用括号"()";

>>> ages = (11,22,33,44,55)
>>> ages[0]#查找
11
>>> ages[-1]
55
>>> for i in ages:#循环
...   print(i)
...
11
22
33
44
55
>>> len(ages)#长度
5
>>> 11 in ages#包含
True
>>>

7.可变、不可变数据类型和hash

>>> l = [1,2,3,4]#列表
>>> id(l)
1466396532232
>>>
>>> a=1#数字
>>> id(a)
1661187136
>>>
>>> s = 'hello'#字符串
>>> id(s)
1466396546584
>>>
>>> t = (1,2,3,4)#元组
>>> id(t)
1466396477864
>>>
>>> hash("alex")
-909141948193867673

 

8.字典

dict: 字典, 字典是一组键(key)和值(value)的组合, 通过键(key)进行查找, 没有顺序, 使用大括号"{}";

键、值、键值对
                  1、 dic.keys() 返回一个包含字典所有KEY的列表
                  2、 dic.values() 返回一个包含字典所有value的列表
                  3、 dic.items() 返回一个包含所有(键、值)元组的列表
                  4、 dic.iteritems()、dic.iterkeys()、dic.itervaluses()  与它们对应的非迭代方法一样,
不同的是它们返回一个迭代子,而不是一个列表 新增 1、dic['new_key'] = 'new_value'; 2、dic.setdefault(key, none) 如果字典中不存在Key键,由dic[key] = default 为它赋值; 删除 1、dic.pop(key[,default]) 和get方法相似,如果字典存在key,删除并返回key对应的valus;如果key不存在,
且没有给default的值,则引发keyerror异常; 2、dic.cler() 删除字典中的所有项或元素; 修改 1、dic['key'] = 'new_value'如果key在字典中存在,'new_value'将会替换原来的value值; 2、dic.update(dic2) 将字典dic2的键值对添加到字典dic中 查看 1、dic['key'],返回字典中key对应的值,若key不存在字典中,则报错; 2、dic.get(key , default = none) 返回字典中key对应的值,若key不存在字典中,则返回default的值(default默认为none) 循环 1、for k in dic.keys() 2、for k,v in dic.items() 3、for k in dic 长度 1、len(dic)

  

9.集合

set: 集合,无序, 元素只出现一次, 自动去重, 使用"set([])";

 set.isdisjoint(s):判断两个集合是不是相交

 set.issuperset(s):判断集合是不是包含其他集合,等同 a>=b

 set.issubset(s): 判断集合是不是被其他集合包含,等同 a<=b

  add(): 单个元素的添加,类似列表中的append

 update(): 对序列的添加,类似extend方法,支持同时添加多个参数

 set. discard(s):不会抛出异常

 set.remove(s):会抛出KeyError错误

 pop:由于集合无序的,pop返回的结果不能确定,当集合为空会抛出KeyError错误

 clear:清除集合

>>> a = {1,2}
>>> a.update([3,4],[1,2,7])
>>> a
{1, 2, 3, 4, 7}
>>> a.update("hello")
>>> a
{1, 2, 3, 4, 'h', 7, 'o', 'e', 'l'}
>>> a.add("hello")
>>> a
{1, 2, 3, 4, 'h', 7, 'o', 'e', 'hello', 'l'}
>>> a.discard(1)
>>> a
{2, 3, 4, 'h', 7, 'o', 'e', 'hello', 'l'}
>>> a.discard(1)
>>> a
{2, 3, 4, 'h', 7, 'o', 'e', 'hello', 'l'}
>>> a.remove(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> a.pop()
2
>>> a
{3, 4, 'h', 7, 'o', 'e', 'hello', 'l'}
>>> a.clear()
>>> a
set()
>>> a.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'
>>>

  

猜你喜欢

转载自www.cnblogs.com/xiaoqianbook/p/8885881.html