標籤:
Python是動態類型語言 ,也是若類型語言這種 語言特性就決定了 他不會有多麼的複雜。。 #簡單的輸出列印#coding=utf-8import time; # This is required to include time module.word = 'word'sentence = "This is a sentence."paragraph = """This is a paragraph. It ismade up of multiple lines and sentences."""print paragrapha,b,c=1,2,"aaa"#List類似數組list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]if word=="word": print paragraph[1:10]*2list[1]="aaa"print list[1:2]#tuple類似 list 不可以賦值 只能readtuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )tinytuple = (123, 'john')print tuple + tinytuple#元數組操作dict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john','code':6734, 'dept': 'sales'}print tinydictprint dictprint chr(100)#運算子print 5**100if not (1>1 and 2<2): print "aaaa"else: print "hjjjjjjj"a = 20b = 20if ( a is b ): print "Line 1 - a and b have same identity"else: print "Line 1 - a and b do not have same identity"#for 迴圈 for letter in "abcdefghi": print letter #遍曆Listfruits = ['banana', 'apple', 'mango']for index in range(len(fruits)): print 'Current fruit :', fruits[index]#遍曆trupl 通過索引迴圈tp = ('banana', 'apple', 'mango')for index in range(len(tp)): print 'Current fruit :', tp[index]#遍曆元欄位dict={"a":1,"b":2} for index in dict: print index,':', dict[index] #字典長度print len(dict) #列出banana元素出現的次數print fruits.count('banana')#timeticks = time.time()print ticks#定義函數def printList(list): for i in range(len(list)): print list[i] print locals()printList(fruits)#異常處理try: print 2/0except Exception: print "ssssssss"else: print "no exception"#try finally#即使出現異常finally還是能執行的#raise觸發異常try: print 1/0 finally: print "finally"#元組進行格式化 #coding=utf-8class Student: name="" age =0 def showInfo(self): print "Name:%s,Age:%s" % (self.name,self.age) def __init__(self,name,age): self.name=name self.age=agestudent=Student("aaa",11) student.showInfo()#擷取類的屬性#coding=utf-8#!/usr/bin/pythonclass Employee: '所有員工的基類' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salaryprint "Employee.__doc__:", Employee.__doc__print "Employee.__name__:", Employee.__name__print "Employee.__module__:", Employee.__module__print "Employee.__bases__:", Employee.__bases__print "Employee.__dict__:", Employee.__dict__#動態類型操作 #codeing=utf-8class Data: name ="" def __init__(self,name): self.name=name print "__init__" def __del__(self): print "__del__" def __str__( self ): return self.namedata=Data("tom")def F(): print "aaaaaaa"data.fun=Fdata.fun()data.age=100print data.age#自訂__str__輸出print data
python基礎練習