pythonプログラミング入門から実践第六章辞書

7478 ワード

学習ノート:
#        
alien_0={'color':'green','points':'5'}
print(alien_0['color'])
print(alien_0['points'])

#6.2     
##        —  ,           ,,,          , —         ,

#6.2.1        
print(alien_0['color'])
##          ,                ,,     alien_0  ‘color’  

new_points=alien_0['points']
print("you just earned "+ str(new_points)+" points!")

#6.2.2    —  
alien_0={'color':'green','points':'5'}
print(alien_0)
alien_0['x_position']=0
alien_0['y_position']=25
print(alien_0)
##       

#6.2.3         
alien_0={}
alien_0['color']='green'
alien_0['points']=6
print(alien_0)

#6.2.4        
alien_0={'color':'green'}
print(alien_0)
print("    ")
alien_0['color']='yello'
print(alien_0)

alien_0={'x_position':0,'y_position':25,'speed':'medium'}
print('original x_position is '+str(alien_0['x_position']))
if alien_0['speed']=='slow':
    x_increment=1
elif alien_0['speed']=='medium':
    x_increment=2
else:
    x_increment=3
alien_0['x_position']=alien_0['x_position']+x_increment
print("new x_position is "+str( alien_0['x_position']))
##           ,    x,y  ,   ,, if-elif-else            ,        x_increment,           x_position ,

#6.2.5    —  
alien_0={'color':'green','points':'5'}
print(alien_0)
del alien_0['points']
print(alien_0)
##del        —  

#6.2.6           
favorite_languages={
        'jen':'python',
        'sarah':'c',
        'daming':'java',
        'sam':'c++',
}
##              ,   —        ,       ,               
print("sam's favorite language is "+ favorite_languages['sam'].title()+' .')

#6.3     
#6.3.1        —  
user_0={
    'username':'luluhuang',
    'first':'lulu',
    'last':'huang',
}
for key,value in user_0.items():
    print("
key: "+key) print("value: "+value) ## for — , , ## ## item() (Dictionary) items() ( , ) #6.3.2 favorite_languages={ 'jen':'python', 'sarah':'c', 'daming':'java', 'sam':'c++', } for name in favorite_languages.keys(): print(name.title()) ## for , name , keys() for name in favorite_languages ## keys() friends=['phil','sarah'] for name in favorite_languages.keys(): print(name.title()) if name in friends: print(" hi "+name.title()+" , i see your favorite language is "+favorite_languages[name].title()+"!") ## keys() , if 'erin' not in favorite_languages.keys(): print("erin, please take your poil!") # 6.3.3 favorite_languages={ 'jen':'python', 'sarah':'c', 'daming':'java', 'sam':'c++', } for name in sorted(favorite_languages.keys()): print(name.title()+", thank you! ") ## for , sorted() , , # 6.3.4 print("the following languages have been mentioned:
") for language in favorite_languages.values(): print(language.title()) ## value(), , for language in set(favorite_languages.values()): print(language.title()) ## set , , #6.4 # 6.4.1 alien_0={'color':'green','points':'5'} alien_1={'color':'yellow','points':'10'} alien_2={'color':'red','points':'15'} aliens=[alien_1,alien_1,alien_2] for alien in aliens: print(alien) print("
") ## , , ,, aliens=[] ## for alien_number in range(30): ## 30 new_alien={'color':'green','points':'5','speed':'slow'} aliens.append(new_alien) ## for alien in aliens[:5]: print(alien) print('...') ## print("totle number of aliens : "+str(len(aliens))) ## range() , , , , aliens , ## 5 aliens=[] ## for alien_number in range(30): ## 30 new_alien={'color':'green','points':'5','speed':'slow'} aliens.append(new_alien) for alien in aliens[:3]: if alien ['color']=='green': alien['color']='yellow' alien['points']='5' alien['speed']='medium' ## for alien in aliens[:5]: print(alien) print('...') ## print("totle number of aliens : "+str(len(aliens))) aliens=[] ## for alien_number in range(30): ## 30 new_alien={'color':'green','points':'5','speed':'slow'} aliens.append(new_alien) for alien in aliens[0:3]: if alien['color']=='green': alien['color']='yellow' alien['points']='5' alien['speed']='medium' elif alien['color']=='yellow': alien['color'] = 'red' alien['points'] = '15' alien['speed'] = 'max' ## for alien in aliens[0:5]: print(alien) print('...') ## print("totle number of aliens : "+str(len(aliens))) # pizza={ 'crust':'thick', 'topping':['mushrooms','extra cheese'], } print("you ordered a "+pizza['crust']+"-crust pizza "+"with the following tippings: ") for topping in pizza['topping']: print("\t"+topping) ## , , for , 'topping' ## , favorite_languages={ 'jen':['python','ruby'], 'sarah':['c'], 'daming':['java','go'], 'sam':['c++','python'], } for name,languages in favorite_languages.items(): print("
"+name.title()+"'s favorite languages are: ") for language in languages: print('\t'+language.title()) # 6,4.3 users={ 'daming':{ 'first':'daming', 'last':'li', 'location':'princeton', }, "lingling":{ 'first': 'lingling', 'last': 'huang', 'location': 'paris', }, } for username,user_info in users.items(): print("
username: "+username) full_name=user_info['first']+" "+user_info['last'] location=user_info["location"] print("\tfull name: "+full_name.title()) print("\tlocation: "+location.title())
GOOD LUCK!!!