varalex = {'name' : 'alex', 'age' : 25, 'workhistor' : ['god','rice bug','dreamwork'], 'demensition' : {'height' : 176, 'weight' : 66}}
print varalex
print varalex.keys()
print varalex.values()
print varalex.items()
# Result:
{'demensition': {'weight': 66, 'height': 176}, 'age': 25, 'name': 'alex', 'workhistor': ['god', 'rice bug', 'dreamwork']}
['demensition', 'age', 'name', 'workhistor']
[{'weight': 66, 'height': 176}, 25, 'alex', ['god', 'rice bug', 'dreamwork']]
[('demensition', {'weight': 66, 'height': 176}), ('age', 25), ('name', 'alex'), ('workhistor', ['god', 'rice bug', 'dreamwork'])]
{}更多的使用 #是單行註解
varalex = {'name' : 'alex', 'age' : 25, 'workhistory' : ['god','rice bug','dreamwork'], 'demensition' : {'height' : 176, 'weight' : 66}}
print varalex.keys()
print varalex.values()
print varalex.items()
varalex['favoritecolor'] = 'green' #add another keys
print varalex.keys()
varalex.pop('favoritecolor') # remove key
print varalex.keys()
print varalex['name'] in ['alex','wong','wu'] #check
varalex['name'] = 'awesome' #change name
print varalex['name']
varalex['workhistory'][0] = 'animation designer' #change job
print varalex['workhistory']
varalex['workhistory'].append('buesiness man') #add job
print varalex['workhistory']
# Result:
['demensition', 'age', 'name', 'workhistory']
[{'weight': 66, 'height': 176}, 25, 'alex', ['god', 'rice bug', 'dreamwork']]
[('demensition', {'weight': 66, 'height': 176}), ('age', 25), ('name', 'alex'), ('workhistory', ['god', 'rice bug', 'dreamwork'])]
['demensition', 'age', 'favoritecolor', 'name', 'workhistory']
['demensition', 'age', 'name', 'workhistory']
True
awesome
['animation designer', 'rice bug', 'dreamwork']
['animation designer', 'rice bug', 'dreamwork', 'buesiness man']
多行註解'''的使用
varalex = {'name' : 'alex', 'age' : 25, 'workhistory' : ['god','rice bug','dreamwork'], 'demensition' : {'height' : 176, 'weight' : 66}}
varalex['favoritecolor'] = 'green' #add another keys
varalex.pop('favoritecolor') # remove key
varalex['name'] = 'awesome' #change name
varalex['workhistory'][0] = 'animation designer' #change job
varalex['workhistory'].append('buesiness man') #add job
'''
print varalex ['name']
print varalex ['age']
'''
# Result:
變化 兩個功能之間運用#作切換
varalex = {'name' : 'alex', 'age' : 25, 'workhistory' : ['god','rice bug','dreamwork'], 'demensition' : {'height' : 176, 'weight' : 66}}
#'''
print varalex ['name'] #function one
'''
print varalex ['age'] #function two
#'''
# Result:
alex
varalex = {'name' : 'alex', 'age' : 25, 'workhistory' : ['god','rice bug','dreamwork'], 'demensition' : {'height' : 176, 'weight' : 66}}
'''
print varalex ['name'] #function one
'''
print varalex ['age'] #function two
#'''
# Result:
25
if 判斷
varalex = 'green'
varalexlist = ['green','blue','white','red']
varalexcolors =['black','green','white','red','orange']
if varalex in varalexlist:
print 'found %s' % varalex #add tab
# Result:
found green
else 應用
varalex = 'green'
varalexlist = ['green','blue','white','red']
varalexcolors =['black','green','white','red','orange']
varalex = 'alex'
if varalex in varalexlist:
print 'found %s' % varalex #add tab
else:
print 'the %s not a valid color' %varalex #add tab
# Result:
the alex not a valid color
elif 應用
varalex = 'green'
varalexlist = ['green','blue','white','red']
varalexcolors =['black','green','white','red','orange']
varalex = 'black'
if varalex in varalexlist:
print 'found %s' % varalex
elif varalex in varalexcolors:
varalexlist.append(varalex)
print 'adding %s to varalexlist' %varalex
print varalexlist
else:
print 'the %s not a valid color' %varalex
# Result:
adding black to varalexlist
['green', 'blue', 'white', 'red', 'black']
IF IF 應用
varalexlist = ['green','blue','white','red']
varalexcolors =['black','green','white','red','orange']
varalex = 'black'
if varalex in varalexcolors:
if varalex in varalexlist:
print 'OK, %s is work' %varalex
else:
print 'woops, %s is not a volid color' %varalex
else:
print 'the %s can not work' %varalex
# Result:
woops, black is not a volid color
for in應用
for item in range(0,10):
print item
# Result:
0
1
2
3
4
5
6
7
8
9
varalex = 'floccinaucinihilipilification'
for charalex in varalex:
print charalex
# Result:
f
l
o
c
c
i
n
a
u
c...太長跳過
varalex = 'floccinaucinihilipilification'
for charalex in varalex:
print charalex *3
# Result:
fff
lll
ooo
ccc
ccc
iii ...太長跳過
for in + if in 應用
alexlist = ['even','today','is','very','hot','but',"i'm",'still','have','fun']
varnumfor = 0
varnumif = 0
print ' '.join(alexlist)
alexcommon = []
for varalex in alexlist:
varnumfor += 1
if varalex in ['i','would','like','to','go','with','u','guys','because','we','will','have','fun']:
alexcommon.append(varalex)
varnumif += 1
print str(alexcommon) +' ifin runs %s times' %varnumif
print ' ifin runs %s times' %varnumif + ' and forin runs %s times' %varnumfor
if varnumif !=0:
print ' ifin totally runs %s times' %varnumif
print ' forin totally runs %s times' %varnumfor
# Result:
even today is very hot but i'm still have fun
ifin runs 0 times and forin runs 1 times
ifin runs 0 times and forin runs 2 times
ifin runs 0 times and forin runs 3 times
ifin runs 0 times and forin runs 4 times
ifin runs 0 times and forin runs 5 times
ifin runs 0 times and forin runs 6 times
ifin runs 0 times and forin runs 7 times
ifin runs 0 times and forin runs 8 times
['have'] ifin runs 1 times
ifin runs 1 times and forin runs 9 times
['have', 'fun'] ifin runs 2 times
ifin runs 2 times and forin runs 10 times
ifin totally runs 2 times
forin totally runs 10 times
數質計算
varalex = []
for alexitem in range(0,20):
if alexitem % 2 == 1:
varalex.append(alexitem)
print varalex
# Result:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
alexdict = {'x' : 100, 'y' : 200, 'z' : 300}
for wu in alexdict.keys():
alexdict[wu] += 500
print wu
print alexdict
# Result:
y
x
z
{'y': 700, 'x': 600, 'z': 800}
數質計算兩種寫法
簡單的
alexlist =[]
for alexitem in range(0,5):
alexlist.append(alexitem)
print alexlist
比較難的
varalex = [alexitem for alexitem in range(0,5)]
print varalex
# Result:
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
例2
較好理解,寫比較多
alexlist =[]
for alexitem in range(0,10):
if alexitem % 2 ==1:
alexlist.append(alexitem)
print alexlist
寫比較短,不好理解
varalex = [alexitem for alexitem in range(0,10) if alexitem % 2 == 1]
print varalex
# Result:
[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9]
例3
alexlist =[]
for alexitem in range(0,10):
if alexitem % 2 ==1:
alexlist.append((alexitem + 21) //7)
print alexlist
varalex = [(alexitem+21) //7 for alexitem in range(0,10) if alexitem % 2 == 1]
print varalex
# Result:
[3, 3, 3, 4, 4]
[3, 3, 3, 4, 4]
while 迴圈判斷式
while 的用法只要注意條件式,成立的條件即可否則會造成無窮迴圈
upalex = 10
numbers = range(2, upalex)
varalexs = []
print numbers
while numbers:
varalex =numbers.pop(0)
varalexs.append(varalex)
numbers = [n for n in numbers if n % varalex]
print '1numbers '+str(numbers)
print '2varalex '+str(varalex)
print '3varalexs '+str(varalexs)
print varalexs
# Result:
[2, 3, 4, 5, 6, 7, 8, 9]
1numbers [3, 5, 7, 9]
2varalex 2
3varalexs [2]
1numbers [5, 7]
2varalex 3
3varalexs [2, 3]
1numbers [7]
2varalex 5
3varalexs [2, 3, 5]
1numbers []
2varalex 7
3varalexs [2, 3, 5, 7]
[2, 3, 5, 7] <---結果
留言列表