python学习 第二章

1.0 二进制转换

由得的次方形式计算

256    128    64    32    16    8    4     2    1
0       0     0      0     1    1    1     1    0
#其10进制数字为32

python函数直接获取二进制

bin(10)
'0b1010'

2.0 字符编码的演化

assic-gb2312-gbk1.0(有简繁)-gb18030(包括少数民族语言)

assic-unicode-utf-8

3.0 python中的字符编码

python2.x默认assic

python2.x默认utf-8

 4.0 python中的数据类型

int 整型

long 长整型(Python2.X中,3.X中已取消)

float 浮点数(大概理解为小数但不完全是)

complex 复数

布尔型(一般表示0或1,对或错)

字符串(一般常用的汉字之类的)

5.0 字符串转换

二进制转换字符串用decode

字符串转换二进制用encode

>>>a='王八'
>>>print (a.encode('utf-8'))  #注意要加存储时的编码类型
b'\xe7\x8e\x8b\xe5\x85\xab'
>>>print (b'\xe7\x8e\x8b\xe5\x85\xab'.decode('utf-8'))
王八

 

python中默认使用UTF-8

6.0 列表

6.1 列表的使用和取值

list = ['张三''李四''王五','赵六','钱七','向八'] #列表list中存储名字
list[0]    #获取第一个人的名字,张三,在Python中下标从0开始
list[2]    #获取第三个人的名字,王五
list[-1]  #获取最后一个人的名字

 

6.2 列表的切片

list = ['张三''李四''王五','赵六','钱七','向八'] #列表list中存储名字
print(list[2:4])    #获取王五 赵六两人的名字
王五 赵六
#可以理解为左闭合右开,或顾头不顾尾
print(list[-3:])
钱七 向八
#注意切片顺序为从左往右    

 

6.3 列表的操作

list = ['张三''李四''王五','赵六','钱七','向八'] #列表list中存储名字
#
list.append('华仔')    #添加只会填到最后面
#插入
list.insert(1,'华仔')    #插入到谁前面就输谁的下标
'张三''华仔','李四','王五','赵六','钱七','向八'
#
list[2] = 哈哈    #将华仔替换为哈哈
#删除
list.remove('华仔')    #输入目标名
del list[1]    #输入删除目标的下标
list.pop()    #默认删除最后一个
list.pop(1)    #输入下标后删除目标下边内容
#查询目标位置
list.index('华仔')   #获取目标所在的位置
#如果未找到会报错
#统计数量
list.count('华仔')    #数列表中华仔的数量
#清空
list.clear()    
#反转
list.reverse()
#排序
list.sort()    #对值排序 特殊符号-大写-数字-小写
#扩展列表
list2 = ('asd','asdas')
list.extend(list2)    #将内容扩展到list的最后

 

6.4 列表的拷贝

list = ['张三','李四','王五','赵六',['钱七','向八']] #列表list中存储名字
list2 = list.copy()
#list和list2内容相同
#注意list内的第二个列表内容改变,则list2内容改变,外列表改变,list2外列表不变(浅COPY)

 

6.5 列表完全拷贝

import copy
list = ['张三','李四','王五','赵六','钱七','向八'] #列表list中存储名字
list2 = copy.deepcopy(list)
#该方法可以得到内存地址完全不同的两个内容相同的列表

 

6.6 跳着切片

list = ['张三','李四','王五','赵六','钱七','向八']
print(list[::2])
#以2的步长切片显示

 

7.0 元组

money = ('家庭',100,'fangzi'#元祖只有两个方法
money.index()
money.count()
#元祖的内容是不可改变的

 8.0  字典

name.capitalize()  首字母大写
name.casefold()   大写全部变小写
name.center(50,"-")  输出 '---------------------Alex Li----------------------'
name.count('ex') 统计 ex出现次数
name.encode()  将字符串编码成bytes格式
name.endswith("Li")  判断字符串是否以 Li结尾
 "Alex\tLi".expandtabs(10) 输出'Alex      Li', 将\t转换成多长的空格 
 name.find('A')  查找A,找到返回其索引, 找不到返回-1 

format :
    >>> msg = "my name is {}, and age is {}"
    >>> msg.format("alex",22)
    'my name is alex, and age is 22'
    >>> msg = "my name is {1}, and age is {0}"
    >>> msg.format("alex",22)
    'my name is 22, and age is alex'
    >>> msg = "my name is {name}, and age is {age}"
    >>> msg.format(age=22,name="ale")
    'my name is ale, and age is 22'
format_map
    >>> msg.format_map({'name':'alex','age':22})
    'my name is alex, and age is 22'


msg.index('a')  返回a所在字符串的索引
'9aA'.isalnum()   True

'9'.isdigit() 是否整数
name.isnumeric  
name.isprintable
name.isspace
name.istitle
name.isupper
 "|".join(['alex','jack','rain']) #用‘|’连接内容
'alex|jack|rain'


maketrans
    >>> intab = "aeiou"  #This is the string having actual characters. 
    >>> outtab = "12345" #This is the string having corresponding mapping character
    >>> trantab = str.maketrans(intab, outtab)
    >>> 
    >>> str = "this is string example....wow!!!"
    >>> str.translate(trantab)
    'th3s 3s str3ng 2x1mpl2....w4w!!!'

 msg.partition('is')   输出 ('my name ', 'is', ' {name}, and age is {age}') 

 >>> "alex li, chinese name is lijie".replace("li","LI",1)
     'alex LI, chinese name is lijie'

 msg.swapcase 大小写互换


 >>> msg.zfill(40)
'00000my name is {name}, and age is {age}'



>>> n4.ljust(40,"-")
'Hello 2orld-----------------------------'
>>> n4.rjust(40,"-")
'-----------------------------Hello 2orld'


>>> b="ddefdsdff_哈哈" 
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

 

posted @ 2018-03-21 16:31  summarys  阅读(57)  评论(0)    收藏  举报