今天看啥
热点:

python之数据类型&字符编码,python数据类型


一.数字

整形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])
# 长度len
print(len(name))
# 成员运算in和not in
msg='hellochantao'
print('hantao'in msg)
# 移除空白strip
passwd='  hantao123    '
print(passwd.strip())
passwd1='****hantao***'
print(passwd1.strip('*'))
print(passwd.lstrip())
print(passwd.rstrip())
# 切分split
user='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' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='' #中文数字
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 in
print('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 in
print(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)
# 长度len
print(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'))  #取出value
print(info.get('na1me'))   #返回None
print(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|s2
s1.union(s2)
# & 交集
s1&s2
s1.intersection(s2)
# - 差集
s1-s2
s1.difference(s2)
# ^ 对称差集
s1^s2
s1.symmetric_difference(s2)
#父集
s1>=s2
s1.issuperset(s2)
#子集
s1<=s2
s1.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)

 

www.513bk.comtruehttp://www.513bk.com/ruby/1301439.htmlTechArticlepython之数据类型字符编码,python数据类型 一.数字 整形int x=10 # x=int(10) print (id(x),type(x),x) 浮点型float salary=3.1 # salary=float(3.1) print (id(salary)...

相关文章

    暂无相关文章
相关搜索:

帮客评论

视觉看点