#!/usr/bin/python #coding=utf-8 ''' 總和=0 循環100次 { 總和 = 總和+當前循環數 } 打印總和 而後再把這個思路用代碼寫出來便可。 import math for i in (1,100 + 1): i = i print i def num(): num = 0 for i in range(1,101): num = num + i return num print(num()) def sum(): sum = 0 for n in range(1,101): sum = sum + n return sum print(sum()) ''' #for循環 #基本構造是: #for 元素 in 序列: # statement for a in[3,4,5,6,'a','b','who are you']: print a m = range(5) print m for a in range(10): print a**a #while循環 #while的用法是: #while 條件: # statement i = 0 while i < 10: print i i = i + 1 print i #中斷循環 for i in range(10): if i == 2: continue#遇到continue, 那麼跳過 print i for i in range(10): if i == 2: break#觸發break, 循環中止 print i #函數 def square_sum(a,b): c = a**2 + b**2 return c print square_sum(-3,9) a = 1 def change_integer(a): a = a + 1 return a print change_integer(a) #注意觀察結果 print a #注意觀察結果 #===(Python中 "#" 後面跟的內容是註釋,不執行 ) b = [1,2,3] def change_list(b): b[0] = b[0] + 1 return b print change_list(b) #注意觀察結果 print b #注意觀察結果 #面向對象的基本概念 #相近對象,歸爲類 class Bird(object): have_feather = True way_of_reproduction = 'egg' summer = Bird() print summer.have_feather print summer.way_of_reproduction #動做 class Bird(object): have_feather = True way_of_reproduction = 'egg' def move(self, dx, dy,dz): position = [0, 0, 0] position[0] = position[0] + dx position[1] = position[1] + dy position[2] = position[2] + dz return position #子類 class Chicken(Bird): way_of_move='walk' possible_in_KFC = True class Oriole(Bird): way_of_move = 'fly' possible_in_KFC = False summer = Bird() summer = Chicken() print summer.have_feather print summer.move(5, 8, 9) wind = Bird() wind = Oriole() print wind.have_feather print wind.move(9, 9, 8) class Human(object): Definition = 'Toolusing' way_of_move = 'Bipedalism' class Child(Human): way_of_move = 'Bipedalism' class Adult(Human): way_of_move = 'Bipedalism' ability = 'Toolusing' class Old(Human): way_of_move = 'Bipedalism' jhon = Child() print jhon.Definition print jhon.way_of_move #面向對象的拓展 #調用類的其它信息 class Human(object): laugh = 'hahaha' def show_laugh(self): print self.laugh def laugh_100th(self): for i in range(100): self.show_laugh() if i == 3: return jhon = Human() jhon.laugh_100th() #初始化 class happyBird(Bird): def __init__(self,more_words): print 'We are happy birds.',more_words summer = happyBird('Happy,Happy!') #對象的性質 class Human(object): def __init__(self, input_gender): self.gender = input_gender def printGender(self): print self.gender jhon = Human('male') print jhon.gender jhon.printGender() class Human(object): def __init__(self, input_gender): self.gender = input_gender def printGender(self): print self.gender li_lei = Human('male') # 這裏,'male'做爲參數傳遞給__init__()方法的input_gender變量。 print li_lei.gender #這一行結果與下一行對比 li_lei.printGender() #這一行結果與上一行對比 nl = [1.1, 2, 3, 4 ,5 ,5 , 55, 6, 7, 5] print nl.count(5) print nl.index(5) nl.insert(0, 999) nl.remove(55) print nl.pop() nl.sort() nl.append(999) print nl class superList(list): def __sub__(self, b): a = self[:] # 這裏,self是supeList的對象。因爲superList繼承於list,它能夠利用和list[:]相同的引用方法來表示整個對象。 b = b[:] while len(b) > 0: element_b = b.pop() if element_b in a: a.remove(element_b) return a print superList([1,2,3,4]) - superList([3,4]) def Year(year): year = int(year) if (year % 4) == 0: print 'True' else: print 'False' return year print Year(2008) import datetime date_ = raw_input('input three number like 2011 2 31:') try: year , month , day = date_.split() print year , month , day print datetime.date(int(year) , int(month) , int(day)) except Exception ,e: print e