Python学習ノート整理(八)Python文の概要



       
      
       
          
Python  
  		  		  
  		    	a,b,c='good','bad','ok'
  		    	log.write('diege,test
') print print 'the shell is python!' if/elif/else if "python" in text: print text for/else for x in mylist: print x while/else while X > Y: print 'hello' pass while Ture: pass break,continue while True: if not line: break try/except/finally try: action() except: print 'action error' raise raise endSearch,location import,from import sys from sys import stdin def,return,yield def f(a,b,c=1,*d): return a+b+c+d[0] def gen(n): for i in n,yield i*2 class class subclass(Superclass) staticData=[] global def function(): global x,y x='new' del del data[k] del data[i:j] del obj:attr del variable del exec exec "import sys" exec cod in gdict,ldict assert assert X > Y with/as (2.6) with open('data') as myfile process(myfile) 、 1、 () ; {} 2、 ? , tabn 。 , 。 3、 1) ; >>> x=2 >>> y=1 >>> if x>y:print x ; >>> a=1;b=2;print a+b 2) {},[],() >>> mylist=[ ... [1,2,3], ... [4,5,6], ... [7,8,9]] >>> a,b,c=1,2,3 >>> d=(a+ ... b ... +c) >>> d 6 3、 while True: reply=raw_input('Enter text:') if reply == 'stop':break print reply.upper() 4、 raw_input , while True: reply=raw_input("Enter text:") if reply=='stop':break print int(reply) ** 2 5、 , while True: reply=raw_input("Enter text:") if reply=='stop': break elif not reply.isdigit(): print "Bad!"*8 else: print int(reply) ** 2 print "Bye!" bad 6、 try try , 。 while True: reply=raw_input("Enter text:") if reply=='stop':break try: num=int(reply) except: print "Bad!"*8 else: print int(reply) ** 2 print "Bye!" try ( ), except , 。 else , try , else 。 Python try , except ( ), else ( )