#!/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
时间: 2024-10-24 17:46:11