#!/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()
时间: 2024-11-09 10:35:33