#!/usr/bin/python#coding=utf-8 ' sum = 0 cycles 100 times {sum = sum + current number of cycles} print the sum and then write out the idea in code. import mathfor i in (1,100 + 1): i = iprint idef num (): num = 0 for i in range ( 1,101): num = num + i return numprint (num ()) def sum (): sum = 0 for n in range (1,101): sum = sum + n return sumprint (SUM ()) "#for循环 # Basic constructs are: #for elements in sequences: # statementfor a in[3,4,5,6, ' A ', ' B ', ' Who are you ']: print am = range (5) Print mfor a in range (Ten): The usage of the print a**a#while loop #while is: #while condition:# statementi = 0while i < 10: print i i = i + 1print i# Interrupt Loop For i in range (Ten): if i == 2: continue# met continue, so skip print ifor i in range (10): if i == 2: break# triggering break , Loop Stop print i# function Def square_sum (A, b): c = a**2 + b**2 return cprint square_sum ( -3,9) a = 1def change_integer (a): a = a + 1 return aprint change_integer (a) #注意观察结果print a #注意观察结果 #=== (PyThon "#" followed by comments, do not execute ) b = [1,2,3]def change_list (b): b [0] = b[0] + 1 return bprint change_list (b) #注意观察结果print b #注意观察结果 # Object-oriented basic concepts # Similar objects, classified as class bird (object): have_feather = true way_of_ reproduction = ' egg ' summer = bird () print summer.have_featherprint summer.way_of_reproduction# action Class bird (object): have_feather = true way_of_reproduction = ' egg ' def move (self, DX,&NBSP;DY,DZ): position = [0, 0, 0] position[0] = position[0] + dx pOSITION[1]&NBSP;=&NBSP;POSITION[1]&NBSP;+&NBSP;DY&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;POSITION[2] = position[2] + dz return position# Subclass Class chicken (Bird): way_of_move= ' walk ' possible_in_kfc = trueclass oriole (Bird): way_of_move = ' Fly ' possible_in_kfc = falsesummer = bird () Summer = chicken () print Summer.have_featherprint summer.move (5, 8, 9) Wind = bird () Wind = Oriole () Print wind.have_featherprint 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. definitionprint jhon.way_of_move# Object-Oriented expansion # Other information for calling classes Class human (object): laugh = ' hahaha ' def show_laugh (self): print self.laugh def laugh_100th (self): for i in range (: ) self.show_laugh () if i == 3: returnjhon = human () jhon.laugh_100th () #初始化class happybird (Bird): &NBSP;&NBSP;DEF&NBSP;__INIT__ (Self,more_words): print ' we are happy birds. ' , More_wordssummer = happybird (' happy,happy! ') #对象的性质class human (object): def __init__ (Self, input_gender): self.gender = input_gender def Printgender (self): print self.genderjhon = human (' Male ') Print jhon.genderjhon.printgender () Class human (object): def __ Init__ (self, input_gender): self.gender = input_ Gender def printgender (self): print self.genderli_lei = human (' male ') # here, ' Male ' is passed as a parameter to the Input_gender variable of the __init__ () method. 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 (+) print Nl.pop () Nl.sort () nl.append (999) print nlclass superlist (list): def __sub_ _ (Self, b): a = self[:] # here, self is the object of Supelist. Since Superlist inherits from list, it can use the same reference method as list[:] to represent the entire object. b = b[:] while len (b) > 0: element_b = b.pop () if element_b in a: a.remove (Element_b) &Nbsp; return aprint superlist ([1,2,3,4]) - superList ([3,4]) Def year (year): year = int (year) if (year % 4) == 0: print ' True ' else: print ' False ' Return yearprint year (Import datetimedate_ = raw_input) (' input three number like 2011 2 31: ') try: year , month , day = date_.split () print year , month , day print datetime.date (int.) , int (month) , int (day)) except Exception ,e: print e
This article from "Little Mo Growth Road" blog, declined reprint!
The PythonBasic2 of Python learning