#!/usr/bin/python #coding=utf-8 #詞典 ''' nl = [1, 3, 8] nl.append(15) print nl bl = [2, 3, 5] print nl + bl dic = {'tom': 11, 'sam': 57, 'lily': 100} print dic['sam'] dic['tom']=59 print dic dic = {} print dic dic['liwei'] = 89 print dic dic = {'lilei': 90, 'lily': 100, 'sam': 57, 'tom':89} for key in dic: print dic[key] #詞典的經常使用方法 print dic.keys() print dic.values() print dic.values() del dic['lilei'] dic.clear() print len(dic) #文本文件的輸入輸出 f = open("test.txt", "r") content = f.readline() print content #輸入 #f.write('I like apple!') content = f.readlines() print content f.close() #引入模塊 import first for i in range(10): first.laugh() #經過 模塊.對象 的方式來調用 import a as b # 引入模塊a,並將模塊a重命名爲b from a import function1 # 從模塊a中引入function1對象。調用a中對象時,咱們不用再說明模塊,即直接使用function1,而不是a.function1。 from a import * # 從模塊a中引入全部對象。調用a中對象時,咱們不用再說明模塊,即直接使用對象,而不是a.對象 ''' #import My_Modle.module def g(a, b, c): return a+b+c print(g(1, 2, 3)) #關鍵字傳遞 print(g(c=3, b=2, a=1)) print(g(1, c=3, b=2)) #參數默認值 def h(d, e, f=10): return d+e+f print(h(3,2)) print(h(3,2,1)) #包裹傳遞 def func(*name): print type(name) print name func(1, 4, 6) func(5, 6, 7, 8, 1, 2) def func(**dict): print type(dict) print dict func(a=1, b=9) func(m=2, n=1, c=11) #解包裹 def func2(a, b, c): print a, b, c args = (1, 2, 5) func2(*args) dict = {'a': 1, 'b': 2, 'c': 3} func(**dict) f = open('My_Module/record.txt', 'a+') f.write('\n I like apple!') f = open('My_Module/record.txt', 'r') for i in range(10): content = f.readline() print content f.close()