1. 程式人生 > >Python記錄2:資料型別

Python記錄2:資料型別

一Python的資料型別可以分為可變與不可變兩種:

可變型別:值改變,但是id不變,證明就是在改變原值,就是可變型別           

                 如list   dict 列表和字典都是可變型別

不可變型別:值改變,id也跟著改變了,證明就是不可變型別,入

                 Int  float  str都是不可變型別

一,字串型別

 1、用途: 性別\愛好等描述性質的狀態
 2、定義方式
# s1="hello" # s1=str('hello')
#str 可以將任意其他型別都轉成str型別
# res=str({'a':1}) #res="{'a':1}"
# 3、常用操作+內建的方法
#優先掌握的操作:(*****)
#1、按索引取值(正向取+反向取) :只能取

取值正向取標號是從0開始遞增,反向取標號是從-1開始遞減

 s1="hello world"
# print(s1[0])
# print(s1[-1])
# print(s1[-3])
# s1[0]='H'
# print(s1)
#2、切片(顧頭不顧尾,步長):從大字串中切出一個子字串
# s1="hello world"
# res=s1[1:5]
# print(s1)
# print(res)
# print(s1[0:7:1]) #0 1 2 3 4 5 6
# print(s1[0:7:2]) #0 2 4 6
# 取值之後用加號運算子也可以實現切片的功能
# print(s1[-1]+s1[-2]+s1[-3]+s1[-4]+s1[-5])
# print(s1[-1::-1]) # -1 -2
# print(s1[::-1]) # -1 -2

#3、長度len
# s1="hello world"
# print(len(s1)) # 字元的個數

#4、成員運算in和not in:判斷一個子字串是否存在於一個大字串中
# msg='my name is alex,alex is dsb'
# print('alex' in msg)
# print('egon' not in msg)


#5、移除空白strip: 移除字串左右兩邊的字元空格
# name=input('username>>>: ').strip() #name='egon '
# name=name.strip()
# if name == 'egon':
#     print('認證成功')

# msg='    he    llo      '
# res=msg.strip(' ')
# print(msg)
# print(res)

# msg='******hello*************'
# res=msg.strip('*')
# print(res)

# msg='***&^#***hello***=-/?**'
# print(msg.strip('*&^$/-?#='))

#6、切分split: 把一個有規律的字串按照某個字元進行切分,切成列表
# info='root:x:0:0::/root:/bin/bash'
# res=info.split(':',maxsplit=-1)
# print(res)
# cmd='get|a.txt|3333'
# res=cmd.split('|')
# print(res)

# info=''
# userinfo=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# for item in userinfo:
#     item+=':'
#     info+=item
# info=info.strip(':')
# print(info,type(info))

# userinfo=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# res=':'.join(userinfo)
# print(res,type(res))
#7、迴圈
# msg='hello'
# for item in msg:
#     print(item)

# 需要掌握的操作(****)
#1、strip,lstrip,rstrip
# print('****egon****'.strip('*'))
# print('****egon****'.lstrip('*'))
# print('****egon****'.rstrip('*'))

#2、lower,upper
# x='ABBBBddd1231'
# print(x.lower())
# print('ABBBBddd2123'.upper())

#3、startswith,endswith
# print('alex is sb'.startswith('alex'))
# print('alex is sb'.startswith('al'))
# print('alex is sb'.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')
# print(msg)
# msg='my name is {0} my age is {0}{1}{1}'.format(18,'egon')
# print(msg)

# x1='egon'
# x2=('egon111')
# print(x1,x2,type(x1),type(x2))

#5、split,rsplit
# print('a:b:c:d:e'.split(':',maxsplit=1))
# print('a:b:c:d:e'.rsplit(':',maxsplit=1))

#6、join

#7、replace
# msg='alex is alex hahahah alex'
# res=msg.replace('alex','SB',1)
# print(msg)
# print(res)

#8、isdigit
# print('1010101'.isdigit())
# age=input('>>>: ')
# if age.isdigit():
#     age=int(age)
#     if age > 10:
#         print('too Big')
#     elif age < 10:
#         print('too small')
#     else:
#         print('you got it')
# else:
#     print('必須輸入數字')


# 其他操作(瞭解即可)
#1、find,rfind,index,rindex,count
# print("abcdefg".find('de',0,3))
# print("abcdefg".index('de'))
# print("abcdefg".index('de',0,3))

# print('alex is alex'.find('alex'))
# print('alex is alex'.rfind('alex'))

# print('alex is alex'.count('alex'))

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

#3、expandtabs
# print('abc\tdef'.expandtabs(8))

#4、captalize,swapcase,title
# print('i am egon'.capitalize())
# print('aAbB'.swapcase())
# print('i am egon'.title())

#5、is數字系列
num1=b'4' #bytes
num2=u'4' #unicode,python3中無需加u就是unicode
num3='壹' #中文數字
num4='Ⅳ' #羅馬數字

# ''.isdigit() # bytes,unicode
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())


# ''.isdecimal():unicode
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
# ''.isnumeric():unicode,羅馬,中文
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())



#6、is其他
# name='egon123'
# print(name.isalnum()) #字串由字母或數字組成
# name='egon'
# print(name.isalpha()) #字串只由字母組成
# print(name.islower())
# print(name.isupper())
# print(name.isspace())
# print(name.istitle()