python之数据类型&字符编码

2018-02-06 20:03:29来源:cnblogs.com作者:Hantaozi人点击

分享

一.数字

整形int

x=10 #x=int(10)print(id(x),type(x),x)

浮点型float

salary=3.1 #salary=float(3.1)print(id(salary),type(salary),salary)

二.字符串

name='hantao'
#按索引取值(正向取+反向取) :只能取print(name[0],type(name[0]))print((name[-1]))
# 切片(顾头不顾尾,步长)print(name[1:3])print(name[0:5:2])print(name[5::-1])
# 长度lenprint(len(name))
# 成员运算in和not inmsg='hellochantao'print('hantao'in msg)
# 移除空白strippasswd='  hantao123    'print(passwd.strip())passwd1='****hantao***'print(passwd1.strip('*'))
print(passwd.lstrip())
print(passwd.rstrip())
# 切分splituser='hantao:liangchaowei:liudehua'print(user.split(':'))
#判断开头结尾msg='hantao_is_cool'print(msg.startswith('han'))print(msg.endswith('cool'))
#用replace替换msg='alex say i have a tesla,my name is alex'print(msg.replace('alex','sb'))
#format格式化print('my name is %s,my age is %s'%('hantao',18))print('my name is {},my age is {}'.format('hantao',18))print('my name is {0},my age is {1}'.format('hantao',18))print('my name is {a},my age is {b}'.format(a='hantao',b=18))
#查找msg='hello world'print(msg.find('hel'))print(msg.index('o'))print(msg.count('l',0,4))
#join连接print(':'.join(['hantao', 'liangchaowei', 'liudehua']))
#填充print('info'.center(30,'='))print('info'.rjust(30,'='))print('info'.ljust(30,'='))print('info'.zfill(30))
#is判断数字num1=b'4' #bytesnum2=u'4' #unicode,python3中无需加u就是unicodenum3='四' #中文数字num4='Ⅳ' #罗马数字print(num1.isdigit())print(num2.isdigit())print(num3.isdigit())print(num4.isdigit())

四.列表

# 按索引存取值(正向存取+反向存取):即可存也可以取print(my_familly[2])print(my_familly[-1])
# 切片(顾头不顾尾,步长)print(my_familly[0:3])print(my_familly[0:5:2])print(my_familly[5::-1])
# 长度print(len(my_familly))
# 成员运算in和not inprint('nep'in my_familly)
# 追加my_familly.append('Corgi')print(my_familly)
# 删除goods=['apple','banana','pear']del goods[-1]print(goods.remove('apple'))   #不返回删除值print(goods.pop(1))  #按照索引删,默认末尾开始删,返回删除值print(goods)
#其他goods=['apple','banana','pear','banana']# goods.insert(0,'sb')# goods.extend(['meat','eggs'])# print(goods.count('banana'))# goods.reverse()    #反转# l=[2,4,6,1,-3]# l.sort(reverse=True)# print(l)print(goods)

五.元祖

ages=(23,34,23,12)# 按索引取值(正向取+反向取):只能取print(ages[1])# 切片(顾头不顾尾,步长)print(ages[0:2])# 长度print(len(ages))# 成员运算in和not inprint(23 in ages)
ages=(23,34,23,12)print(ages.index(12))  #查找索引print(ages.count(23))  #查找个数

 小练习

'''简单购物车,要求如下:实现打印商品详细信息,用户输入商品名和购买个数,则将商品名,价格,购买个数加入购物列表,如果输入为空或其他非法输入则要求用户重新输入'''msg_dic={'apple':10,'tesla':100000,'mac':3000,'lenovo':30000,'chicken':10,}l = []while True:    for key in msg_dic:        print(key,msg_dic[key])    goods=input('input your good:').strip()    if goods not in msg_dic:continue    while True:        counts=input('input the number:').strip()        if counts.isdigit():break    l.append((goods,msg_dic[goods],counts))    print(l)

六.词典

info={'name':'egon','age':18,'sex':'male'}# 按key存取值:可存可取print(info['name'])info['hobbies']=['eat','drink','sleep']print(info)# 长度lenprint(len(info))# 删除print(info.pop('na1me',None))# 键keys(),值values(),键值对items()print(info.keys())print(info.values())print(info.items())for item in info.items():    print(item)
info={'name':'egon','age':18,'sex':'male'}print(info.get('name'))  #取出valueprint(info.get('na1me'))   #返回Noneprint(info.popitem())   #随机删t=['23','23','45','45']a,*_,d=t   #压缩赋值print(a,d)for a,b in info.items():    print(a,b)info_new={'a':1,'b':3,'name':'hantao'}info.update(info_new)    #没有则新加,有则更新print(info)dic={}.fromkeys(['name','age','hobbies'])  #初始字典print(dic)print(info.setdefault('age','20'))    #

 七.集合

#作用:去重,关系运算s={1,2,'a'}  #s=set({1,2,'a'})s1={1,2,3}s2={2,3,4}# | 并集s1|s2s1.union(s2)# & 交集s1&s2s1.intersection(s2)# - 差集s1-s2s1.difference(s2)# ^ 对称差集s1^s2s1.symmetric_difference(s2)#父集s1>=s2s1.issuperset(s2)#子集s1<=s2s1.issubset(s2)
s1={1,2,3,4,'a'}# print(s1.pop())  #随机删,返回结果# print(s1.remove(1))  #删元素,不返回值,元素不存在,报错# s1.discard(1)   #删元素,不返回值,元素不存在,不报错s2={5,6}print(s1.isdisjoint(s2))    #s1和s2没有交集,则返回True
# 有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序l=[    {'name':'egon','age':18,'sex':'male'},    {'name':'alex','age':73,'sex':'male'},    {'name':'egon','age':20,'sex':'female'},    {'name':'egon','age':18,'sex':'male'},    {'name':'egon','age':18,'sex':'male'},]l1=[]for i in l:    if i not in l1:l1.append(i)print(l1)

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台