#Dictionary ''' dict{} dict={'keyword': value} ''' #!/usr/bin/python3 tel = {'jack': 4098, 'sape': 4139} tel['guido'] = 4127 print(tel) print(tel['jack']) del tel['sape'] print(tel) tel['irv'] = 4127 print(tel) print(list(tel.keys())) t = sorted(tel.keys()) print(t) x = dict([('sape',4139),('guido',4127),('jack',4098)]) print(x) y = dict(sape=4139,guido=4127,jack=4098) print(y) #遍历技巧 knights = {'gallahad': 'the pure', 'robin':'the brave'} print(knights.items()) for k,v in knights.items(): print(k,v) #不是很懂,为什么输出是robin the brave, gallahad the pure,因为knights的items里面没有k这个字母啊 #k,v代表的是key, value!!!!!! for i,v in enumerate(['tic','tac','toe']): print(i,v) #遍历两个或更多的序列,可以用zip()组合 #最后的what is your {0}, it is {1}, 必须加".format(q,a))这样{0}{1}才有对应的数值输出。 questions = ['name','quest','favorite color'] answers = ['lancelot','the holy grail','blue'] for q,a in zip(questions,answers): print('What is your {0}? It is {1}.'.format(q,a)) ##等同于用%s print('what is your %s? it is %s' %(q,a)) ##反向遍历一个序列,用reversed()函数 for i in reversed(range(1,10,2)): print(i) #列表中其实也是可以可以用的 list.reverse() ## 你还记得如果是bash的倒叙应该是怎么写吗? ## 找回来。。。死记硬背背下来啊。 #按顺序来遍历书序,使用sorted() basket = ['apple','orange','apple','pear','orange','banana'] for f in sorted(set(basket)): print(f) ''' [x*y for x in range[1,5] if x > 2 for y in range[1,4] if x < 3] 他的执行顺序是 for x in range[1,5] if x > 2 for y in range[1,4] if x < 3 x*y ''' #####################################################3 #Python3模块 #!/usr/bin/python3 #filename: using_sys.py import sys print('命令行参数如下:') for i in sys.argv: print(i) print('\n\nPython 路径为:',sys.path,'\n') ##import语句 #这个就是先自己自定义函数,保存为support.py,然后再新建一个test.py来引入support模块 #!/usr/bin/python3 #Filename:support.py def print_func(par): print("Hello:",par) return #!/usr/bin/python3 #Filename:test.py import support support.print_func("Runoob") ##最后结果为Hello:Runoob ############## import sys print("The path for python is:", sys.path) #The path for python is: ['F:\\python-learn', 'F:\\python-learn', 'F:\\python35.zip', 'F:\\DLLs', 'F:\\lib'] #############