1. 程式人生 > >python的遊戲之旅--( 數字 字串 列表 元組 字典 即為遊戲職業)

python的遊戲之旅--( 數字 字串 列表 元組 字典 即為遊戲職業)

 

 

 

 

---恢復內容開始---

一,個人心得

我覺得學習python就想玩一款遊戲,python的基本資料型別(字串 數字 列表 元組 字典)可以看做一個遊戲職業,每個職業都有相應的技能。

玩好這些職業,讓我們的python之旅更加容易。

 

二,數字(職業:初級劍士,技能較少)

在python語言中,數字型別主要包括整形,浮點數,和複數。

整形 int

浮點數 float

複數  complex(real [,image])

數字的技能:

 

hex(x)  將一個整形x轉化為一個十六進位制字串

 

oct(x)  將一個整形x轉化為一個八進位制字串

 

x . bit_length()     整形x對應的二進位制的佔位個數

 

 

三,字串(職業:三轉劍士(劍神),技能較多)

字串就是連續的字元序列,可以是計算機所能表示的一切字元的合集。

 

字串的技能:

字串的拼接:字串與字串之間可以通過 + 來實現拼接。

str1 = '我今天一共走了'
num = 12098
str2 = ''
print(str1 + str(num)+ str2 )

 

len.(x)  字串x的長度

str1 = '我今天一共走了'
c = len(str1)
print(c)

 

x.[:]  字串的擷取

#!/D:\python
# -*- coding:utf-8 -*-
str1 = '人生苦短,我用python!'
s1 = str1[1]
s2 = str1[5:]
s3 = str1[:5]
s4 = str1[2:5]
print('原字串',str1)
print(s1+'\n'+ s2 + '\n'+s3 +'\n'+s4)

 

 x.split()  分割字串

 

 

#!/D:\python
# -*- coding:utf-8 -*-
str1 = '人生苦短,我用python!>>> www.lll.ccv'
print('原字串:',str1)
a = str1.split()
b = str1.split('>>>')
c = str1.split('.')
print(a)
print(b)
print(c)

 

x.join()     合併字串

#!/D:\python
# -*- coding:utf-8 -*-
l = ['小明','小王','小李','小張']
a = ' @'.join(l)
# b = '@' + a
print('你要@的好友:',a)

 

x.count()      count 方法用於檢索指定字串在另一個字串中出現的次數

 

#!/D:\python
# -*- coding:utf-8 -*-
l = '@小明 @小王 @小號 '
print('字串',l ,'包括',l.count('@'),'個@字元')

 

 x.find()   用於檢索是否含有指定的子字串。如果檢索的字串不存在,返回 -1,否則返回首次出現子字串時索引。

 

#!/D:\python
# -*- coding:utf-8 -*-
l = '@小明 @小王 @小號 '
print('字串',l,'中@字串首次出現的位置索引為',l.find('@'))

另外 x。index('@')

 

x.startswith()    檢索字串是否以指定子字串開頭

 

#!/D:\python
# -*- coding:utf-8 -*-
l = '@小明 @小王 @小號 '
print('字串',l,'是否以@開頭',l.startswith('@'))

 

x.endswith()   檢索字串是否以指定子字串結尾 

 

#!/D:\python
# -*- coding:utf-8 -*-
l = '@小明 @小王 @小號 '
print('字串',l,'是否以@結尾',l.endswith('@'))

 

 

字母大寫小寫轉化  x.upper()   x.lower()

 

 

 x.strip()   用於去掉字串左右兩側的空格和特殊字元。

 

#!/D:\python
# -*- coding:utf-8 -*-
s1 = '  http://www.baidu.com \t\n\r '
print('原字串:'+ s1 +'.')
print('字串:'+ s1.strip()+'')
s2 = '@摩天營救@'
print('原字串s2;'+ s2 + '')
print('字串:'+ s2.strip('@') + '')

 

 

 %格式化字串  ‘%[-][+][0][m][.n]格式化字元’%exp

#!/D:\python
# -*- coding:utf-8 -*-
t = '編號: %09d\t 公司名稱 : %s \t 官網: http://www.%s.com'
c = (7,'百度','baidu')
print(t%c)

 

 

format() 格式化字串

#!/D:\python
# -*- coding:utf-8 -*-
t = '編號: {}\t 公司名稱 : {} \t 官網: http://www.{}.com'
print(t.format('7','百度','baidu'))

 

字串編碼轉換

#!/D:\python
# -*- coding:utf-8 -*-
v = '野渡無人舟自橫'
b = v.encode('gbk')
a = v.encode('utf-8')
print('原字串:',v)
print('gbk轉換後:',b)
print('utf-8轉換後',a)

 

decode ()  方法解碼

#!/D:\python
# -*- coding:utf-8 -*-
v = b'\xd2\xb0\xb6\xc9\xce\xde\xc8\xcb\xd6\xdb\xd7\xd4\xba\xe1'
c = b'\xe9\x87\x8e\xe6\xb8\xa1\xe6\x97\xa0\xe4\xba\xba\xe8\x88\x9f\xe8\x87\xaa\xe6\xa8\xaa'
b = v.decode('gbk')
a = c.decode('utf-8')
print('原字碼:',v)
print('原字碼:',c)
print('gbk轉換後:',b)
print('utf-8轉換後',a)

 

 

四,列表(弓箭手,技能較多)

列表切片

#!/D:\python
# -*- coding:utf-8 -*-
List = ['小明','小張','alx','zhangsan']
a = List[1]
b = List[2:3]
c = List[0:3:2]
print(a)
print(b)
print(c)

 

 列表的建立

emptylist = []

 

建立數值列表

list(data)

例如; list (rrnge(10,20,2))

 

刪除列表中的元素   del []

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen']
del team
print(team)

 

遍歷列表     直接使用for迴圈實現

 

 

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
for i in team:
    print(i)

 

使用for迴圈和enumerate()函式實現

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
for index,i in enumerate(team):
    print(index+1,i)

 

 

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
for index,i in enumerate(team):
    if index%2 == 0:
        print(i + "\t\t",end='')
    else:
        print(i + "\n")

 

新增,修改和刪除列表元素

x.append()

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
a = len(team)
print(a)
team.append("小學")
c = len(team)
print(c)
print(team)

 

 x.extend()

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
l = ['1','58','888']
a = len(team)
print(a)
team.extend(l)
c = len(team)
print(c)
print(team)

 

 修改元素

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
print(team)
team[3] = '小法'
print(team)

 

 

刪除元素

x.remove(' c')   c表示要刪除的元素

 

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王']
print(team)
team.remove('小李')
print(team)

 

x.count()     獲得指定元素出現的次數

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王','小王']
c = team.count('小王')
print(c)

 

x.index()  獲取指定元素首次出現的下標

#!/D:\python
# -*- coding:utf-8 -*-
team = ['huama','louam','bairen','小明','小李','小王','小王']
c = team.index('小王')
print(c)

 

 

 

 統計數值列表的元素和

 

#!/D:\python
# -*- coding:utf-8 -*-
team = [2,5,7,9,5,14,15]
c = sum(team[:])
a = sum(team[1:5])
print(c)
print(a)

 

對列表進行排序

 

 

#!/D:\python
# -*- coding:utf-8 -*-
grade = [58,5,12,9,5,14,15]
print(grade)
a = sorted(grade)
print('升序',a)
c = sorted(grade,reverse=True)
print('降序',c)

 

 

列表推導式

LIst = [Expression for var in range]

#!/D:\python
# -*- coding:utf-8 -*-
import random
randomnumber = [random.randint(10,100) for i in range(10)]
print("生成的隨機數為:",randomnumber)

 

 

二維列表的使用

使用巢狀的for迴圈建立

 

五,元組

元組的建立和刪除

emptytuple = ()

#!/D:\python
# -*- coding:utf-8 -*-
c = tuple(range(10,20,2))
print(c)

 

 刪除元組   del list

 元組不可以修改

 

六,字典

字典,每個元素都包含兩部分‘鍵’和‘值’

dictionary = {'key1':'value1','key2':'value2',.........,'keyn':'valuen'}

通過對映函式建立字典

dictionaryd = dict(zip(list1,list2))

#!/D:\python
# -*- coding:utf-8 -*-
name = ['幽夢','冷血','帶藍']
sign = ['水瓶座','射手座','雙魚座']
dictionary = dict(zip(name,sign))
print(dictionary)

 

 

 通過給定“鍵”-值對"建立字典

dictonary = dict.fromkeys(list1)

 

#!/D:\python
# -*- coding:utf-8 -*-
name = ['幽夢','冷血','帶藍']
dictionary = dict.fromkeys(name)
print(dictionary)

 

通過鍵值對訪問字典

#!/D:\python
# -*- coding:utf-8 -*-
name = {'1':'幽夢','2':'冷血','3':'帶藍'}
print(name['1'])

 

 

 字典的遍歷

#!/D:\python
# -*- coding:utf-8 -*-
name = {'1':'幽夢','2':'冷血','3':'帶藍'}
for key,value in name.items():
    print(key,value)

 

 

 

新增,修改和刪除字典元素

 

 

#!/D:\python
# -*- coding:utf-8 -*-
name =dict((('1','幽夢'),('2','冷血'),('3','帶藍')))
print(name)
name['4']='重新'
print(name)

 

修改

#!/D:\python
# -*- coding:utf-8 -*-
name =dict((('1','幽夢'),('2','冷血'),('3','帶藍')))
print(name)
name['1']='重新'
print(name)

 

 

刪除

#!/D:\python
# -*- coding:utf-8 -*-
name =dict((('1','幽夢'),('2','冷血'),('3','帶藍')))
print(name)
del name['2']
print(name)

 

字典推導式

 

#!/D:\python
# -*- coding:utf-8 -*-
name = ['按時','等等','次數','問問']
sign =['1分鐘','','2次','']
dictionary = {i:j+'hh' for i,j in zip(name,sign)}
print(dictionary)

 

 2018-11-19     15:07:32

 

---恢復內容結束---