1.整形int

定義方式:age=10 #age=int(10)

# 類型轉(zhuǎn)換
# print(int(3.1))
# res=int('1111111')
# print(res,type(res))


# res=float('111111.1')
# print(res,type(res))

*十進(jìn)制轉(zhuǎn)換

print(bin(13))#轉(zhuǎn)成2進(jìn)制

print(oct(13))#轉(zhuǎn)成8進(jìn)制

print(hex(13))#轉(zhuǎn)成16進(jìn)制

2.浮點型float

3定義方式:

salary=10.1# salary=float(10.1)

# 類型轉(zhuǎn)換
# print(float(10))
# print(float(1.1))
# print(float('1.1'))

3.字符串類型

定義方式:

msg='hello world' #msg=str('hello world')

# 類型轉(zhuǎn)換: 可以把任意類型專場字符串類型
# res1=str(10)
# res2=str(10.3)
# res3=str([1,2,3])
# res4=str({'x':1}) #res4="{'x':1}"
#
# print(type(res1))
# print(type(res2))
# print(type(res3))
# print(type(res4))

常用操作+內(nèi)置方法

1.按索引取值(正向+反向):只能取

msg='hello world'
print(type(msg[0]))
print(msg[-1])
#mag[0]='H' 不能修改

2.切片(顧頭不顧尾、步長)

msg='hello world'
print(msg[0]+msg[1]+msg[2])
print(msg[0:5])
print(msg[0:5:2]) #0 2 4
print(msg[0:]) 
print(msg[:])

print(msg[-1:-5:-1])#-1 -2 -3 -4
print(msg[::-1]) #取反

3.長度len:統(tǒng)計的是字符的個數(shù)

# msg='h你d'
# print(len(msg))

4.成員運算in和not in:判斷一個子字符是否存在于一個大字符串中

msg='hello world'
print('ho' in msg)
print('ho' not in msg)

5.移除空白strip:移除字符串左右兩邊的某些字符

msg='    hello  '
print(msg.strip(' '))
print(msg.strip())
print(msg)

#name=input('name>>>: ').strip()
#pwd=input('pssword>>>:').strip()

msg='***h**ello***'
print(msg.strip('*'))#中間的去除不了

#msg='*-=H/ello)('
#print(msg.strip('*-=/)('))

 

6.切分split:吧有規(guī)律的字符串切呈列表從而方便取值

info='egon:18:180:150'
res=info.split(':',1)
print(res)
print(res[1])
res1=info.spilt('')
print(res1)

s1=' '
for item in res:
    s1+=item
print(s1)

s1=':'.join(res)
print(s1)

#':'.join([1,2,3,4,5])

7.循環(huán)

for i in 'hello':
    print(i)
#1、strip,lstrip,rstrip
# msg='*****hello****'
# print(msg.strip('*'))
# print(msg.lstrip('*'))
# print(msg.rstrip('*'))

#2、lower,upper
# msg='AaBbCc123123123'
# print(msg.lower())
# print(msg.upper())

#3、startswith,endswith
# msg='alex is dsb'
# print(msg.startswith('alex'))
# print(msg.endswith('sb'))

#4、format的三種玩法
# msg='my name is %s my age is %s' %('egon',18)
# print(msg)

# msg='my name is {name} my age is {age}'.format(age=18,name='egon')
# print(msg)

# 了解
# msg='my name is {} my age is {}'.format(18,'egon')
# msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon')
# print(msg)


#5、split,rsplit
# cmd='get|a.txt|33333'
# print(cmd.split('|',1))
# print(cmd.rsplit('|',1))


#6、replace
# msg='kevin is sb kevin kevin'
# print(msg.replace('kevin','sb',2))

#7、isdigit #當(dāng)字符串內(nèi)為純數(shù)字時結(jié)果為True
# res='11111'
# print(res.isdigit())
# int(res)

# age_of_bk=18
# inp_age=input('your age: ').strip()
# if inp_age.isdigit():
#     inp_age=int(inp_age) #int('asdfasdfadfasdf')
#     if inp_age > 18:
#         print('too big')
#     elif inp_age < 18:
#         print('to small')
#     else:
#         print('you got it')
# else:
#     print('必須輸入純數(shù)字')



# 了解(**)
#1、find,rfind,index,rindex,count
# print('xxxkevin is sb kevin'.find('kevin'))
# print('xxxkevin is sb kevin'.index('kevin'))
# print('xxxkevin is sb kevin'.rfind('kevin'))
# print('xxxkevin is sb kevin'.rindex('kevin'))
#

# res='xxxkevin is sb kevin'.find('kevasdfsadfin')
# print(res)

# res='xxxkevin is sb kevin'.index('kevasdfsadfin')



# print('kevin is kevin is kevin is sb'.count('kevin'))

#2、center,ljust,rjust,zfill
# print('egon'.center(50,'*'))
# print('egon'.ljust(50,'*'))
# print('egon'.rjust(50,'*'))
# print('egon'.zfill(50))

#3、captalize,swapcase,title
# print('my name is kevin'.capitalize())
# print('AaBbCc'.swapcase())
# print('my name is kevin'.title())

#4、is其他
# name='egon123'
# print(name.isalnum()) #字符串由字母或數(shù)字組成
# print(name.isalpha()) #字符串只由字母組成

# print(name.islower())
# print(name.isupper())
# name='    '
# print(name.isspace())
msg='I Am Egon'
print(msg.istitle())
需要掌握的操作

 


 列表類型

定義方式:

在[]內(nèi)用逗號分隔開多個任意類型的值

l=['a','b','c'] #l=list(['a','b','c'])

l=list('hello')
l=list({'x':1,'y':2})
print(l)
類型轉(zhuǎn)換

常用操作+內(nèi)置方法

l=['a','b','c','d','e']
#1 按索引存取值
print(l[0])
print(l[-1])
print(id(l))
l[0]='A'
print(id(l))

#切變(顧頭不顧尾)
l=['a','b','c','d','e']
print(l[1:4])
print(l)

#長度
l=['a','b','c','d','e']
print(len(l))

#成員運算in 和not in
print('a' in l)
print('aaa' not in l)

#追加、插入
l=['a','b','c','d','e']
l.append(333)
l.append(4444)
print(l)

l.insert(0,111111)
print(l)

#刪除
l=['a','b','c','d','e']
del l[0]
res=l.remove('b')
print(l)
print(res)

res=l.pop(0)
print(l)
print(res)

#循環(huán)
l=['a','b','c','d','e']
for item in l:
    print(item)
l=['a','b','a','c','d','e']
print(l.count('a'))

l=['a','b','a','c','d','e']
items=[1,2,3,4,5]
#for item in items:
#    l.append(item)
l.extend(items)
print(l)
 
l=['a','b','a','c','d','e']
print(l.index('a',2,5))

l=['a','b','a','c','d','e']
l.reverse()
print(1)

l=[10,-1,3,11,9]
l.sort(reverse=True)
print(l)#從大往小排列



  
需要掌握的操作

該類型總結(jié):

有序 村多個值 可變

l.append('first')
l.append('second')
l.append('third')
print(l)

print(l.pop(0))
print(l.pop(0))
print(l.pop(0))
隊列
l.append('first')
l.append('second')
l.append('third')
print(l)

print(l.pop())
print(l.pop())
print(l.pop())
堆棧

 

元組:一個不可變的列表

定義:在()內(nèi)用逗號分隔開多個任意類型的元素

# t=(1,2.2,'aa',('b','c'),['a','b','c']) # t=tuple(...)
# print(type(t))
t=('a',)
print(type(t))
print(t)
t1=tuple('hello')
t2=tuple([1,2,3])
print(t1)
print(t2)
類型轉(zhuǎn)換
常用操作+內(nèi)置的方法
#按照索引取值(只能取)
t=(1,2.2,'aa',('b','c'),['a','b','c'])
print(t[0])
print(t(-1))

#切片
t=('a','b','c','e','f')
print(t[1:4])

#長度
print(len(t))

#成員運算
print('a' in t)

#循環(huán)
for item in t:
    print(item)
t=('a','b','c','e','a','f')
print(t.index('a',1,5))
print(t.count('a'))
需要掌握的操作
該類型總結(jié):存多個值 有序 不可變

字典類型dict
定義方式:在{}用逗號分隔開多個元素,每個元素都是key:value的形式,其中key可以不可變類型,通常是字符串類
d={1:'aaa',2.2:'bbb','xxx':'ccc',(1,2,3):'dddd'} #d=dict(...)
print(d[(1,2,3)])
d=dict(x=1,y=2,z=3)
print(d)

items=[('name','egon'),('age',18),('gender','male')]
d={}
for item in items:
    d[item[0]]=item[1]

d=dict(items)
print(d)
# 了解
keys=['name','age','gender','height','weight']
d={}
for key in keys:
    d[key]=None

d={}.fromkeys(keys,None)
print(d,type(d))
類型轉(zhuǎn)換
常用操作+內(nèi)置的方法
#按key存取值:可存可取
dic={'name':'egon','age':18}
dic['name']
print(dic.get('name'))

dic['name']='EGON'
dic['gender']='male'
print(dic)

#長度len
dic={'name':'egon','age':18}
print(len(dic))

#成員運算:是以字典的key為準(zhǔn)的
dic={'name':'egon','age':18}
print('name' in dic)
print('egon' in dic)

#刪除
dic={'name':'egon','age':18}
del dic['name']
print(dic)

res=dic.pop('name')
print(dic)
print(res)

res=dic.popitem()#隨機刪除
print(res)

#鍵keys(),值values(),鍵值對items()
# >>> dic={'name':'egon','age':18}
# >>>
# >>> dic.keys()
# dict_keys(['name', 'age'])
# >>> dic.values()
# dict_values(['egon', 18])
# >>> dic.items()
# dict_items([('name', 'egon'), ('age', 18)])

#循環(huán)
dic={'name':'egon','age':18}
for k in dic:
    print(k)
for k in dic.keys():
    print(k)
for k in dic.values():
   print(v)
for k,v in dic.items(): #k,v=('name', 'egon')
  print(k,v)
    

 

dic={'name':'egon','age':18}
dic.update({'age':19,'gender':'male'})
print(dic)

dic={'name':'egon','age':18}
#當(dāng)key存在時,不改原值,返回原值
res=dic.setdefault('name','EGON')
print(dic)
print(res)

# 當(dāng)key不存在時,增加新值
res=dic.setdefault('gender','male')
print(dic)
print(res)
需要掌握的操作
該類型總結(jié):
存多個值 無序 可變

pythons=['張鐵蛋','李銅淡','王金蛋','趙銀膽','alex','kevin']
linuxs=['oldboy','張鐵蛋','趙銀膽','alex','wxx']

res=[]
for stu in python:
    if stu in linuxs:
print(res)
找共同元素
集合類型set

定義方式:在{}內(nèi)用逗號分隔開多個元素,但是元素的特點是
集合內(nèi)的元素必須是不可變類型
集合內(nèi)元素?zé)o序
集合內(nèi)元素不能重復(fù)

s={1,'aaa',2,} #s=set(...)
print(s,type(s))
s=set()
print(s,type(s))
s={1,1,1,1,1,1,1,1,1,1,1,1,'a','a','a'}
print(s)
res=set('hello')
print(res)
# res=set([1,'a','b'])
# print(res)
類型轉(zhuǎn)換
常用操作+內(nèi)置的方法
#len長度
#成員運算

pythons={'張鐵蛋','李銅淡','王金蛋','趙銀膽','alex','kevin'}
linuxs={'oldboy','張鐵蛋','趙銀膽','alex','wxx'}
#3、|合集:求所有報名的學(xué)生
print(pythons | linuxs)
print(pythons.union(linuxs))

#4、&交集:求同時報名兩門課程的學(xué)生
print(pythons&linuxs)

#5、-差集: 求只報名python課程的學(xué)員
print(pythons-linuxs)
print(linuxs-pythons)

#6、^對稱差集:求沒有同時報名兩門課程的學(xué)生
#res=(pythons-linuxs) | (linuxs-pythons)
res=pythons^linuxs
print(res)

#7、==
s1={1,2,3}
s2={3,2,1}
print(s1 == s2)

# 注意:父子集描述的是一種包含與被包含的關(guān)系
#8、父集:>=
#9、子集:<=
s1={1,2,3}
s2={1,2,3,4}
print(s2>=s1)
print(s1<=s2)
s1={1,2,3}
s1.update({3,4,5,6})
print(s1)

s1={,'aa','bb',3}
print(s1.pop())

res=s1.remove('bbbbbb')
print(s1)
print(res)

s1.discard('bbb')
s1.add(4)
print(s1)
需要掌握的

該類型總結(jié) 存多個值 無序 可變

 

集合的去重

局限性
1. 只能針對不可變類型
2. 不能保證原來的順序
names=['egon','egon','egon','alex','alex','kevin']

new_names=list(set(names))
print(new_names)
l=[
    {'name':'egon','age':18,'sex':'male'},
    {'name':'alex','age':73,'sex':'male'},
    {'name':'kevin','age':20,'sex':'female'},
    {'name':'egon','age':18,'sex':'male'},
    {'name':'egon','age':18,'sex':'male'},
]
new_l=[]
for dic in l:
    if dic not in new_l;
        new_l.append(dic)
print(new_l)
去重