1. 程式人生 > >python_數據類型

python_數據類型

copy pca 二進制 額外 ont pac 從大到小 enc 生成

字符串——str

capitalize()--->首個字母大寫,自身不變會生成一個新的值
語法:str.capitalize()
例子:name=‘abc‘
v = name.capitalize()
  print(v)===Abc


casefold()--->將所有大小變成小寫
語法:str.casefold()
例子:name = ‘ABCjk‘
   v = name.casefold()
   print(v)=====abcjk




# 3. 將所有大小變小寫
# name = ‘AleX‘
# v = name.lower()
# print(v)


# 4. 文本居中
# 參數1: 表示總長度
# 參數2:空白處填充的字符(長度為1)
# name = ‘alex‘
# v = name.center(20)
# print(v)
# v = name.center(20,‘行‘)
# print(v)


# 5. 表示傳入之在字符串中出現的次數
# 參數1: 要查找的值(子序列)
# 參數2: 起始位置(索引)
# 參數3: 結束位置(索引)
# name = "alexasdfdsafsdfasdfaaaaaaaa"
# v = name.count(‘a‘)
# print(v)
# v = name.count(‘df‘)
# print(v)


# v = name.count(‘df‘,12)
# print(v)
# v = name.count(‘df‘,0,15)
# print(v)


# 6. 是否以xx結尾
# name = ‘alex‘
# v1 = name.endswith(‘ex‘)
# print(v1)


# 7. 是否以xx開頭
# name = ‘alex‘
# v2 = name.startswith(‘al‘)
# print(v2)


# 8. encode欠


# 9. 找到制表符\t,進行替換(包含前面的值)
# PS: \n
# name = "al\te\tx\nalex\tuu\tkkk"
# v = name.expandtabs(20)
# print(v)


# 10. 找到指定子序列的索引位置:不存在返回-1
# name = ‘alex‘
# v = name.find(‘o‘)
# print(v)
# v = name.index(‘o‘)
# print(v)


# 11.字符串格式化


# tpl = "我是:%s;年齡:%s;性別:%s"


# tpl = "我是:{0};年齡:{1};性別:{2}"
# v = tpl.format("李傑",19,‘都行‘)
# print(v)


# tpl = "我是:{name};年齡:{age};性別:{gender}"
# v = tpl.format(name=‘李傑‘,age=19,gender=‘隨意‘)
# print(v)


# tpl = "我是:{name};年齡:{age};性別:{gender}"
# v = tpl.format_map({‘name‘:"李傑",‘age‘:19,‘gender‘:‘中‘})
# print(v)



# 12. 是否是數字、漢子.
# name = ‘alex8漢子‘
# v = name.isalnum() # 字,數字
# print(v) # True
# v2 = name.isalpha()#
# print(v2)# False


# 13. 判斷是否是數字
# num = ‘②‘
# v1 = num.isdecimal() # ‘123‘
# v2 = num.isdigit() # ‘123‘,‘②‘
# v3 = num.isnumeric() # ‘123‘,‘二‘,‘②‘
# print(v1,v2,v3)



# 14. 是否是表示符
# n = ‘name‘
# v = n.isidentifier()
# print(v)


# 15.是否全部是小寫
# name = "ALEX"
# v = name.islower()
# print(v)
# v = name.isupper()
# print(v)


# 16,.全部變大寫,
# name = ‘alex‘
# v = name.upper() # lower()
# print(v)


# 17.是否包含隱含的xx
# name = "釣魚要釣刀魚,\n刀魚要到島上釣"
# v = name.isprintable()
# print(v)



# 18.是否全部是空格
# name = ‘ ‘
# v = name.isspace()
# print(v)



# 19.元素拼接(元素字符串) *****
# name = ‘alex‘


# v = "_".join(name) # 內部循環每個元素
# print(v)


# name_list = [‘海峰‘,‘杠娘‘,‘李傑‘,‘李泉‘]
# v = "搞".join(name_list)
# print(v)


# 20. 左右填充
# center,rjust,ljust
# name = ‘alex‘
# v = name.rjust(20,‘*‘)
# print(v)



# 21. 對應關系 + 翻譯
# m = str.maketrans(‘aeiou‘,‘12345‘) # 對應關系
#
# name = "akpsojfasdufasdlkfj8ausdfakjsdfl;kjer09asdf"
# v = name.translate(m)
# print(v)


# 22. 分割,保留分割的元素
# content = "李泉SB劉康SB劉一"
# v = content.partition(‘SB‘) # partition
# print(v)


# 23. 替換
# content = "李泉SB劉康SB劉浩SB劉一"
# v = content.replace(‘SB‘,‘Love‘)
# print(v)
# v = content.replace(‘SB‘,‘Love‘,1)
# print(v)


# 24,移除空白,\n,\t,自定義
# name = ‘alex\t‘
# v = name.strip() # 空白,\n,\t
# print(v)


# 25. 大小寫轉換
# name = "Alex"
# v = name.swapcase()
# print(v)


# 26. 填充0
# name = "alex"
# v = name.zfill(20)
# print(v)


# v1 = ‘alex‘
# v2 = ‘eric‘
#
# v = v1 + v2 # 執行v1的__add__功能
# print(v)



###### 字符串功能總結:
# name = ‘alex‘
# name.upper()
# name.lower()
# name.split()
# name.find()
# name.strip()
# name.startswith()
# name.format()
# name.replace()
# "alex".join(["aa",‘bb‘])



###### 額外功能:
# name = "alex"
# name[0]
# name[0:3]
# name[0:3:2]
# len(name)
# for循環,每個元素是字符



# 練習題
# name = "aleX"
# a
# v = name.strip()
# print(v)
# b
# v = name.startswith(‘al‘)
# print(v)
# v = name.replace(‘l‘,‘p‘)
# print(v)


# v = name.split(‘l‘)
# print(v) # 列表


# **** 轉換成字節 ****
# name = "李傑"
# v1 = name.encode(encoding=‘utf-8‘) # 字節類型
# print(v1)
# v2 = name.encode(encoding=‘gbk‘) # 字節類型
# print(v2)



# ########################################## int 整數 ##########################################


# 1. 當前整數的二進制表示,最少位數
# age = 4 # 100
# print(age.bit_length())


# 2. 獲取當前數據的字節表示
# age = 15
# v = age.to_bytes(10,byteorder=‘big‘)
# v = age.to_bytes(10,byteorder=‘little‘)
# print(v)
# 00000000 00001111 -> 15


# ########################################## bool 布爾值 ##########################################
# v = 0 # 1,-1
# v = ""
# v = []
# --> 空內容:False


# ########################################## list 列表 ##########################################
# ## int=xx; str=‘xxx‘ list=‘xx‘
# user_list = [‘李泉‘,‘劉一‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# PS:
# name = ‘alex‘
# 執行功能;
# 1.追加
# user_list = [‘李泉‘,‘劉一‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# user_list.append(‘劉銘‘)
# print(user_list)
# 2. 清空
# user_list = [‘李泉‘,‘劉一‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# user_list.clear()
# print(user_list)


# 3. 拷貝(淺拷貝)
# user_list = [‘李泉‘,‘劉一‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# v = user_list.copy()
# print(v)
# print(user_list)


# 4. 計數
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# v = user_list.count(‘李泉‘)
# print(v)


# 5. 擴展原列表
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# user_list.extend([‘郭少龍‘,‘郭少霞‘])
# print(user_list)


# 6. 查找元素索引,沒有報錯
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# v = user_list.index(‘李海‘)
# print(v)


# 7. 刪除並且獲取元素 - 索引
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# v = user_list.pop(1)
# print(v)
# print(user_list)


# 8. 刪除 - 值
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# user_list.remove(‘劉一‘)
# print(user_list)


# 9. 翻轉
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘] # 可變類型
# user_list.reverse()
# print(user_list)


# 10. 排序: 欠參數
# nums = [11,22,3,3,9,88]
# print(nums)
# 排序,從小到大
# nums.sort()
# print(nums)
# 從大到小
# nums.sort(reverse=True)
# print(nums)


###### 額外:
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,‘小龍‘]
# user_list[0]
# user_list[1:5:2]
# del user_list[3]
# for i in user_list:
# print(i)
# user_list[1] = ‘姜日天‘
# user_list = [‘李泉‘,‘劉一‘,‘李泉‘,‘劉康‘,‘豆豆‘,[‘日天‘,‘日地‘,‘泰迪‘],‘小龍‘]



# li = [‘eric‘,‘alex‘,‘tony‘]
#
# v = len(li)
# print(v)
#
# li.append(‘seven‘)
# print(li)
#
# li.insert(0,‘Tony‘)
# print(li)
#
# li[1] = ‘Kelly‘
#
# li.remove(‘eric‘)
# print(list)
#
# v = li.pop(1)
# print(v)
# print(li)
#
# del li[2]



# del li[0:2] # 0 =<x < 2
# print(li)


# li.reverse()
# print(li)


# for i in li:
# print(i)


# ######################################### 強插:range,enumrate #########################################
# 1. 請輸出1-10
# 2.7: 立即生成所有數字
# range(1,11) # 生成 1,23,,4,56.10


# 3.x: 不會立即生成,只有循環叠代時,才一個一個生成
# for i in range(1,11): #
# print(i)


# for i in range(1,11,2): #
# print(i)


# for i in range(10,0,-1): #
# print(i)


# 1. 3.x 不會立生成,叠代之後才一個一個創建;
"""
- 2.7:
range()
xrange() 不會立生成,叠代之後才一個一個創建;
- 3.x
range() 不會立生成,叠代之後才一個一個創建;
"""
# 2. range: 三個參數
#
# li = [‘eric‘,‘alex‘,‘tony‘]
# # range,len,li循環
# for i in range(0,len(li)):
# ele = li[i]
# print(ele)



# li = [‘eric‘,‘alex‘,‘tony‘]
# for i in li:
# print(i)


# for i in range(0,len(li)):
# print(i+1,li[i])



# enumerate額外生成一列有序的數字
# li = [‘eric‘,‘alex‘,‘tony‘]
# for i,ele in enumerate(li,1):
# print(i,ele)
#
# v = input(‘請輸入商品序號:‘)
# v = int(v)
# item = li[v-1]
# print(item)


# ######################################### tuple:元組,不可被修改的列表;不可變類型 #########################################
# user_tuple = (‘alex‘,‘eric‘,‘seven‘,‘alex‘)
# 1. 獲取個數
# v = user_tuple.count(‘alex‘)
# print(v)
# 2.獲取值的第一個索引位置
# v = user_tuple.index(‘alex‘)
# print(v)


####### 額外:
# user_tuple = (‘alex‘,‘eric‘,‘seven‘,‘alex‘)
# for i in user_tuple:
# print(i)


# v = user_tuple[0]


# v = user_tuple[0:2]
# print(v)


# user_tuple = (‘alex‘,‘eric‘,‘seven‘,[‘陳濤‘,‘劉浩‘,‘趙芬芬‘],‘alex‘)
# user_tuple[0] = 123 x
# user_tuple[3] = [11,22,33] x
# user_tuple[3][1] = ‘劉一‘
# print(user_tuple)


# li = [‘陳濤‘,‘劉浩‘,(‘alex‘,‘eric‘,‘seven‘),‘趙芬芬‘]
# ****** 元組最後,加逗號 ******
# li = (‘alex‘,)
# print(li)


# ######################################### dict:字典: 可變類型 #########################################


# 1. 清空、
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.clear()
# print(dic)


# 2. 淺拷貝
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# v = dic.copy()
# print(v)


# 3. 根據key獲取指定的value;不存在不報錯
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# v = dic.get(‘k1111‘,1111)
# print(v)
# v = dic[‘k1111‘]
# print(v)


# 4. 刪除並獲取對應的value值
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# v = dic.pop(‘k1‘)
# print(dic)
# print(v)


# 5. 隨機刪除鍵值對,並獲取到刪除的鍵值
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# v = dic.popitem()
# print(dic)
# print(v)


# k,v = dic.popitem() # (‘k2‘, ‘v2‘)
# print(dic)
# print(k,v)


# v = dic.popitem() # (‘k2‘, ‘v2‘)
# print(dic)
# print(v[0],v[1])


# 6. 增加,如果存在則不做操作
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.setdefault(‘k3‘,‘v3‘)
# print(dic)
# dic.setdefault(‘k1‘,‘1111111‘)
# print(dic)
# 7. 批量增加或修改
# dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
# dic.update({‘k3‘:‘v3‘,‘k1‘:‘v24‘})
# print(dic)



# dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],123)
# print(dic)
# dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],123)
# dic[‘k1‘] = ‘asdfjasldkf‘
# print(dic)


# dic = dict.fromkeys([‘k1‘,‘k2‘,‘k3‘],[1,])
# {
# k1: 123123213, # [1,2]
# k2: 123123213, # [1,]
# k3: 123123213, # [1,]
# }
# dic[‘k1‘].append(222)
# print(dic)
# ########## 額外:
# - 字典可以嵌套
# - 字典key: 必須是不可變類型
# dic = {
# ‘k1‘: ‘v1‘,
# ‘k2‘: [1,2,3,],
# (1,2): ‘lllll‘,
# 1: ‘fffffffff‘,
# 111: ‘asdf‘,
# }
# print(dic)
# key:
# - 不可變
# - True,1


# dic = {‘k1‘:‘v1‘}
# del dic[‘k1‘]


# 布爾值:
# 1 True
# 0 False
#
# bool(1111)



# ##################################### set,集合,不可重復的列表;可變類型 #####################################
# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
# s2 = {"alex",‘eric‘,‘tony‘,‘劉一‘}


# 1.s1中存在,s2中不存在
# v = s1.difference(s2)
# print(v)
# #### s1中存在,s2中不存在,然後對s1清空,然後在重新復制
# s1.difference_update(s2)
# print(s1)


# 2.s2中存在,s1中不存在
# v = s2.difference(s1)
# print(v)


# 3.s2中存在,s1中不存在
# s1中存在,s2中不存在
# v = s1.symmetric_difference(s2)
# print(v)
# 4. 交集
# v = s1.intersection(s2)
# print(v)
# 5. 並集
# v = s1.union(s2)
# print(v)


# 6. 移除
# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
# s1.discard(‘alex‘)
# print(s1)


# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
# s1.update({‘alex‘,‘123123‘,‘fff‘})
# print(s1)
# ##### 額外:


# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘}
# for i in s1:
# print(i)


# s1 = {"alex",‘eric‘,‘tony‘,‘李泉‘,‘李泉11‘,(11,22,33)}
# for i in s1:
# print(i)

 

python_數據類型