# *************python OO ****************************** class person: country='China' def __init__(self): self.name='karl' self.id=100001 def say_id(self): print "%s's id is %d" % (self.name, self.id) def set_Country(self,cou): self.country=cou me=person() me.say_id() me.set_Country('England') print me.country # *************file write ***************************** b_file=file('b.txt','w') b_file.write('write a line in file') b_file.write('write a line in file'); b_file.write('enter\n') b_file.writelines(['billrice','ricerice']) b_file.close() b_file=file('b.txt','r') b_content=b_file.read() print b_content # *************file read ****************************** a_file=file('a.txt','r') a_content=a_file.read() print a_content b_file=file('a.txt','r') for a_line in b_file.readlines(): print 'Line:', a_line # ************* method defination ********************** def square(x): return x**2 print square(5) # print 25 def swap(a,b): return (b,a) print swap(3,5) # print (5,3) # ************* while ********************************** i=0 n=0 while i<10: if i%2==0: n=n+i i=i+1 print n # ************* input ********************************** your_name=raw_input("please input your name:") hint="welcome! %s" % your_name print hint inputed_num=0 while 1: inputed_num=input("input a number between 1 and 10\n") if inputed_num>=10: pass elif inputed_num<1: pass else: break print "OK" # ************* list and dict *************************** contact = {} contact_list = [] while 1: contact['name'] = raw_input("please input name: ") contact['phone'] = raw_input("please input phone number: ") contact_list.append(contact.copy()) go_on = raw_input("continue?\n") if go_on == "yes": pass elif go_on == "no": break else: print "you didn't say no\n" i = 1 for contact in contact_list: print "%d: name=%s" % (i, contact['name']) print "%d: phone=%s" % (i, contact['phone']) i = i + 1 # *********** input() and raw_input()******************** print input("please input 1+2: ") # when input 1+2, then print 3 print raw_input("please input 1+2: ") # when input 1+2, then print 1+2 # *********** Exception ********************************* try: print input() # when input 1/0, then will thow a exception except: print 'there is an error in your input'