python兒童編程-課件_第1頁
python兒童編程-課件_第2頁
python兒童編程-課件_第3頁
python兒童編程-課件_第4頁
python兒童編程-課件_第5頁
已閱讀5頁,還剩137頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

python兒童編程2018/3/17并非所有的蛇都會爬行python兒童編程2018/3/17并非所有的蛇都會爬行2python兒童編程你將了解

什么是python

在計算機上安裝并使用python2python兒童編程你將了解3python兒童編程一種計算機語言高級語言(Java,Vb,Ruby,Python,C等多達上百種)和人類一樣,計算機使用多種語言進行交流。一個編程語言只是一種與計算機對話的特殊方式。人類和計算機都能理解的指令。3python兒童編程一種計算機語言和人類一樣,計算機使用多4python兒童編程獲取安裝程序(下載)

https:///

注意根據(jù)操作系統(tǒng)選擇下載64或32位版本(可執(zhí)行文件)在windows下執(zhí)行安裝程序4python兒童編程獲取安裝程序(下載)5python兒童編程啟動pythonshell(IDLE)這就是PythonShellPythonShell就是在計算機上解釋執(zhí)行python語言的控制臺。相當(dāng)于你的大腦負責(zé)解釋你和別人所說的話,并按照要求進行動作。5python兒童編程啟動pythonshell(IDLE6python兒童編程你告訴計算機的第一句話>>>print("HelloWorld")HelloWorld>>>

讓計算機做幾道數(shù)學(xué)題

>>>3*52156>>>3670-1563514

SymbolOperation+Addition(加)-Subtraction(減)*Multiplication(乘)/Division(除)6python兒童編程你告訴計算機的第一句話讓計算機做幾道數(shù)7python兒童編程你將了解

什么是變量?

它能干什么?

如何使用它7python兒童編程你將了解8python兒童編程變量(variable)編程中的變量描述了存儲信息的地方。比如數(shù)字、文本、數(shù)字和文本等等。從另一方面看,變量就像一個標簽。>>>fred=100#定義一個變量,并給變量賦值>>>print(fred)#告訴計算機把變量表示的內(nèi)容顯示出來100>>>fred=200#定義一個變量,并給變量賦值>>>john=fred#定義另一個變量,并把fred的值賦值給它>>>print(john)200>>>found_coins=20>>>magic_coins=10>>>stolen_coins=3>>>found_coins+magic_coins*2-stolen_coins*3318python兒童編程變量(variable)>>>fre9python兒童編程你將了解STRINGS-----字符串LISTS-----列表TUPLES-----元組MAPS-----地圖9python兒童編程你將了解10python兒童編程String(字符串)在編程術(shù)語中,我們通常稱文本為字符串。你可以把一個字符串看作字母的集合,本資料里所有的字母、數(shù)字和符號都是一串字符。>>>fred='Whatispinkandfluffy?Pinkfluff!!'>>>print(fred)Whatispinkandfluffy?Pinkfluff!!創(chuàng)造一個字符串,把它放在變量里,讓計算機顯示出來說明字符串用”或者‘來定義字符串轉(zhuǎn)義符號\,試著頂一個I’AMCOMPUTER10python兒童編程String(字符串)>>>fre11python兒童編程在字符串種嵌入值>>>myscore=1000>>>message='Iscored%spoints'>>>print(message%myscore)Iscored1000points>>>nums='Whatdidthenumber%ssaytothenumber%s?Nicebelt!!'>>>print(nums%(0,8))Whatdidthenumber0saytothenumber8?Nicebelt!!字符串乘法>>>print(10*'a')Aaaaaaaaaa試試下面的輸出結(jié)果spaces=''*25print('%s12ButtsWynd'%spaces)11python兒童編程在字符串種嵌入值>>>myscor12python兒童編程LIST(列表)

很多變量的集合,用[]進行定義>>>some_numbers=[1,2,5,10,20]>>>some_strings=['Which','Witch','Is','Which']定義一個list你可以對list進行如下操作>>>some_some_strings.append(‘bearburp’)#追加項目>>>delsome_strings[2]#刪除第3項>>>print(some_strings[2:3])#顯示第3-4項>>>print(some_strings)#顯示所有項>>>print(some_numbers+some_strings)#可以做加法>>>print(some_numbers*5)#可以做乘法除法,減法不行哦!考慮一下為什么12python兒童編程LIST(列表)>>>some_n13python兒童編程TUPLE(元祖)元組類似于使用圓括號的列表,用()進行定義,區(qū)別是創(chuàng)建后不能更改>>>fibs=(0,1,1,2,3)>>>print(fibs[3])定義一個tuple你不可以改變tuple的內(nèi)容否則計算機給給你報錯>>>fibs[0]=4Traceback(mostrecentcalllast):File"<pyshell>",line1,in<module>fibs[0]=4TypeError:'tuple'objectdoesnotsupportitemassignment13python兒童編程TUPLE(元祖)>>>fibs14python兒童編程MAP(字典)字典中的每一項都有一個鍵和一個對應(yīng)的值。你可以根據(jù)鍵找到值。>>>favorite_sports={'RalphWilliams':'Football','MichaelTippett':'Basketball','EdwardElgar':'Baseball','RebeccaClarke':'Netball','EthelSmyth':'Badminton','FrankBridge':'Rugby'}定義一個map你可以對字典做如下操作>>>print(favorite_sports[‘RebeccaClarke’])#找到RebeccaClarke喜歡的運動>>>delfavorite_sports[‘EthelSmyth’]#從字典中刪除EthelSmyth數(shù)據(jù)>>>favorite_sports[‘EthelSmyth’]=‘IceHockey‘#修改EthelSmyth喜歡的運動>>>favorite_sports[‘CanCan’]=‘tennis’#追加cancan喜歡的項目14python兒童編程MAP(字典)>>>favorit15python兒童編程你可以畫出絢麗的圖案15python兒童編程你可以畫出絢麗的圖案16python兒童編程Turbles是一個畫板模塊,你可以利用它繪圖。正如你寫字并不需要你去制造鉛筆和紙張,你可以利用turtle去繪畫16python兒童編程Turbles是一個畫板模塊,你可以17python兒童編程importturtle#引進海龜,你可以開始使用它turtle.pencolor("red")#設(shè)置畫筆顏色(紅色)turtle.pensize(1)#設(shè)置畫筆粗細turtle.forward(100)#讓海龜前進50個像素turtle.left(90)#左轉(zhuǎn)90度turtle.forward(100)#讓海龜繼續(xù)前進50個像素turtle.left(90)#左轉(zhuǎn)90度turtle.forward(100)#讓海龜繼續(xù)前進50個像素turtle.left(90)#左轉(zhuǎn)90度turtle.forward(100)#讓海龜繼續(xù)前進50個像素turtle.up()#讓海龜抬起筆turtle.left(90)#左轉(zhuǎn)90度turtle.forward(50)#讓海龜繼續(xù)前進25個像素turtle.down()#讓海龜放下筆turtle.pencolor("green")#設(shè)置畫筆顏色(綠色)turtle.pensize(3)#設(shè)置畫筆粗細turtle.circle(50)#畫一個半徑50的圓17python兒童編程importturtle18python兒童編程importturtle#引進海龜,你可以開始使用它myColor=["red","green","brown"]index=0forxinrange(250):turtle.pencolor(myColor[index])index+=1ifindex==3:index=0turtle.forward(x*2)turtle.left(92)右邊的圖怎么畫出來的?看看下面的代碼讓計算機干了什么18python兒童編程importturtle19python兒童編程用IFELSE判斷邏輯19python兒童編程用IFELSE判斷邏輯20python兒童編程age=10ifage>=20:print("oh!youareyong")Elifage>20andage<50print("oh!youareold")else:print("oh!youaretooold")20python兒童編程age=1021python兒童編程條件符號邏輯塊21python兒童編程條件符號邏輯塊22python兒童編程ifage>=10andage<=13:多個條件同時滿足任何一個條件滿足即可ifage==10orage==11orage==12orage==13:復(fù)合型條件ifsex==“femal”and(age==10orage==11orage==12orage==13):22python兒童編程ifage>=10anda23python兒童編程>>>myval=None>>>ifmyval==None:print("Thevariablemyvaldoesn'thaveavalue")什么都沒有保存的空值>>>age=10>>>ifage==10:print("Thevariablemyvaldoesn'thaveavalue")數(shù)值是字符串還是數(shù)字???>>>age=’10’>>>ifage==10:print("Thevariablemyvaldoesn'thaveavalue")>>>age='10'>>>converted_age=int(age)>>>age=10>>>converted_age=str(age)>>>age='10.5'>>>converted_age=int(age)>>>ifage==10:print("Thevariablemyvaldoesn'thaveavalue")結(jié)果如何23python兒童編程>>>myval=None什么24python兒童編程24python兒童編程25python兒童編程作業(yè)要抄寫100遍???NO!

print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)…………..print(“homework”)print(“homework”)print(“homework”)print(“homework”)soeasy!!forxinrange(0,99):print(‘homework')forxinrange(0,99):print('hello%s'%x)試試這個25python兒童編程作業(yè)要抄寫100遍???NO!p26python兒童編程>>>print(list(range(10,20)))[10,11,12,13,14,15,16,17,18,19]簡單的列表打印class_list=["class1","class2","class3","class4","class5"]forxinrange(0,4):print('hello%s'%class_list[x])①循環(huán)方式的列表打?、谘h(huán)方式的遍歷列表>>>wizard_list=['spiderlegs','toeoffrog','snailtongue','batwing','slugbutter','bearburp']>>>foriinwizard_list:print(i)左邊的1和2實現(xiàn)方式有什么區(qū)別?hugehairypants=['huge','hairy','pants']foriinhugehairypants:print(i)forjinhugehairypants:print(j)推測一下下面的結(jié)果26python兒童編程>>>print(list(ran27python兒童編程問題

寶箱里有20枚金幣,每天會增加10枚,但是烏鴉每周會偷走3枚,請計算一年53周每周寶箱內(nèi)會剩余多少金幣>>>found_coins=20>>>magic_coins=70>>>stolen_coins=3u>>>coins=found_coinsv>>>forweekinrange(1,53):wcoins=coins+magic_coins-stolen_coinsxprint('Week%s=%s'%(week,coins))27python兒童編程問題>>>found_coins28python兒童編程forstepinrange(0,20):print(step)FOR循環(huán)x=45y=80whilex<50andy<100:x=x+1y=y+1print(x,y)WHILE循環(huán)forxinrange(0,20):print('hello%s'%x)ifx<9:breakBreak可以提前退出循環(huán)28python兒童編程forstepinrange(29python兒童編程函數(shù)是一些處理邏輯的集合模塊是函數(shù),變量的集合擁有更強大的功能海龜就是一個繪圖模塊29python兒童編程函數(shù)是一些處理邏輯的集合模塊是函數(shù),30python兒童編程deftestfunc(myname):

print('hello%s'%myname)函數(shù)名,參數(shù),處理testfunc('Mary')print(savings(10,10,5))執(zhí)行函數(shù)deftestfunc(fname,lname):print('Hello%s%s'%(fname,lname))函數(shù)可以有多個參數(shù)函數(shù)可以有返回值defsavings(pocket_money,paper_route,spending):returnpocket_money+paper_route–spending30python兒童編程deftestfunc(mynam31python兒童編程每周生產(chǎn)X個罐子,計算出一年中每周位置總共生產(chǎn)的罐子。defspaceship_building(cans):total_cans=0forweekinrange(1,53):total_cans=total_cans+cansprint('Week%s=%scans'%(week,total_cans))函數(shù)調(diào)用spaceship_building(2)#A工廠每周只能生產(chǎn)2個spaceship_building(10)#B工廠每周只能生產(chǎn)10個考慮一下使用函數(shù)的好處31python兒童編程每周生產(chǎn)X個罐子,計算出一年中每周位323.模塊(moudle)如何導(dǎo)入模塊importsys#導(dǎo)入系統(tǒng)模塊Importturtle#導(dǎo)入海龜繪圖模塊只有導(dǎo)入模塊后,才可以使用它323.模塊(moudle)如何導(dǎo)入模塊importsy334.使用sys模塊sys模塊內(nèi)部有一個特殊的對象稱為stdin(標準輸入),它提供了一個相當(dāng)有用的函數(shù)readline。ReadLine函數(shù)用于讀取一行文本類型在鍵盤上,直到按回車鍵。Standardinput的略稱importsysdefageEV():print('Howoldareyou?')age=int(sys.stdin.readline())ifage<=15:print('youareachild!')elifage>15andage<40:print('youareayoung!')else:print('youareold!')ageEV()334.使用sys模塊sys模塊內(nèi)部有一個特殊的對象稱為s34python兒童編程一切皆對象對象的定義被稱作類34python兒童編程一切皆對象對象的定義被稱作類351.類的實際概念351.類的實際概念362.類的實際概念-2主類classThings:passThings為類名,pass表示類里面為空如果東西為父類的一部分,那么可以定義為子類ClassInanimate(Things):passInanimate為類名,括號中的Things表示父類classAnimate(Things):pass同樣我們可以定義東西的另一個子類—生物可以接著往下定義其他子類classSidewalks(Inanimate):pass定義無生命東西的子類—人行道以此類推classAnimals(Animate):passclassMammals(Animals):passclassGiraffes(Mammals):pass362.類的實際概念-2主類classThings:Th373.類的使用classGiraffes(Mammals):pass你有一只長頸鹿,我們給它名字叫reginald(對象)reginald=Giraffes()定義了長頸鹿類對象的使用你的類定義空空如野,嘗試加些特征(函數(shù))吧classAnimals(Animate):defbreathe(self):#呼吸passdefmove(self):#移動passdefeat_food(self):#食物passclassMammals(Animals):deffeed_young_with_milk(self):passclassGiraffes(Mammals):defeat_leaves_from_trees(self):pass373.類的使用classGiraffes(Mammal384.為什么要使用類和對象reginald=Giraffes()#名字為reginald的長頸鹿對象reginald.move()#讓長頸鹿reginald移動reginald.eat_leaves_from_trees()#讓長頸鹿reginald吃樹葉你有一只長頸鹿,我們給它名字叫reginaldharold=Giraffes()#名字為harold的長頸鹿對象reginald.move()#讓長頸鹿harold移動思考reginald.move()為什么長頸鹿可以調(diào)用move()函數(shù)進行移動子類繼承父類的函數(shù)以及屬性384.為什么要使用類和對象reginald=Gira395.類和對象的例子classAnimals(Animate):defbreathe(self):print('breathing')defmove(self):print('moving')defeat_food(self):print('eatingfood')classMammals(Animals):deffeed_young_with_milk(self):print('feedingyoung')classGiraffes(Mammals):defeat_leaves_from_trees(self):print('eatingleaves')reginald=Giraffes()harold=Giraffes()reginald.move()harold.eat_leaves_from_trees()豐富你的類使用你的類和對象類的函數(shù)都有一個參數(shù)叫self,它是干什么的?395.類和對象的例子classAnimals(Anim406.Self的作用classGiraffes(Mammals):deffind_food(self):self.move()print("I'vefoundfood!")self.eat_food()defeat_leaves_from_trees(self):self.eat_food()defdance_a_jig(self):self.move()self.move()self.move()self.move()Self代表類自己的對象調(diào)用函數(shù)時這個參數(shù)不是必須的一個函數(shù)可以調(diào)用另外一個函數(shù)406.Self的作用classGiraffes(Mam416.類的特殊函數(shù)__self__()__self__()是一個特殊函數(shù),它在定義對象時被調(diào)用,用于通過傳遞參數(shù)初期化一些對象的屬性classGiraffes:def__init__(self,spots):self.giraffe_spots=spots>>>ozwald=Giraffes(100)>>>gertrude=Giraffes(150)>>>print(ozwald.giraffe_spots)100>>>print(gertrude.giraffe_spots)150初期化函數(shù)的例子初期化函數(shù)的使用實例416.類的特殊函數(shù)__self__()__self__(42python兒童編程42python兒童編程431.Python自帶函數(shù)-1獲得絕對值abs()>>>print(abs(10))10布爾變量bool()>>>print(bool(0))False>>>print(bool(1))True>>>print(bool('a'))Dir函數(shù)>>>print(bool(0))False>>>print(bool(1))True>>>print(bool('a'))#用它來計算絕對值#用它來取得邏輯真假,可進行IF判斷

還記得條件語法嗎ifelifelse#它的參數(shù)是任意類型,執(zhí)行結(jié)果可以告訴你,可以處理這種類型所有的函數(shù)。你需要從一堆結(jié)果中找出自己有用的信息??纯聪旅娴挠涍^,對于整數(shù)你可以利用那些函數(shù)。>>>print(dir(1))['__abs__','__add__','__and__','__bool__','__ceil__','__class__','__delattr__','__dir__','__divmod__','__doc__','__eq__','__float__','__floor__','__floordiv__','__format__','__ge__','__getattribute__','__getnewargs__','__gt__','__hash__','__index__','__init__','__init_subclass__','__int__','__invert__','__le__','__lshift__','__lt__','__mod__','__mul__','__ne__','__neg__','__new__','__or__','__pos__','__pow__','__radd__','__rand__','__rdivmod__','__reduce__','__reduce_ex__','__repr__','__rfloordiv__','__rlshift__','__rmod__','__rmul__','__ror__','__round__','__rpow__','__rrshift__','__rshift__','__rsub__','__rtruediv__','__rxor__','__setattr__','__sizeof__','__str__','__sub__','__subclasshook__','__truediv__','__trunc__','__xor__','bit_length','conjugate','denominator','from_bytes','imag','numerator','real','to_bytes']431.Python自帶函數(shù)-1獲得絕對值abs()442.Python自帶函數(shù)-2獲得幫助help>>>help(abs)Helponbuilt-infunctionabsinmodulebuiltins:abs(x,/)Returntheabsolutevalueoftheargument.執(zhí)行命令函數(shù)eval>>>your_calculation=input('Enteracalculation:')Enteracalculation:12*52>>>eval(your_calculation)624#用它讓Python告訴你函數(shù)的使用方法,不過都是英文哦!執(zhí)行命令函數(shù)eval>>>my_small_program='''print('ham')print('sandwich')'''>>>exec(my_small_program)hamsandwich區(qū)別eval可以有返回值exec無返回值442.Python自帶函數(shù)-2獲得幫助help>>>h453.Python自帶函數(shù)-3浮點值float()>>>print(abs(10))10整數(shù)int()>>>float('123.456789')123.456789>>>your_age=input('Enteryourage:')Enteryourage:20>>>age=float(your_age)>>>ifage>13:print('Youare%syearstooold'%(age-13))Youare7.0yearstooold#帶很多位小數(shù)的值>>>int(123.456)123>>>int('123')123>>>int('123.456')Traceback(mostrecentcalllast):File"<pyshell>",line1,in<module>int('123.456')ValueError:invalidliteralforint()withbase10:'123.456'出錯了!字符串’123.456’不可以453.Python自帶函數(shù)-3浮點值float()>464.Python自帶函數(shù)-4取得長度len>>>len('thisisateststring')21>>>creature_list=['unicorn','cyclops','fairy','elf','dragon','troll']>>>print(len(creature_list))6取得最大數(shù),最小值maxmin>>>numbers=[5,4,10,30,22]>>>print(max(numbers))30>>>strings='s,t,r,i,n,g,S,T,R,I,N,G'>>>print(max(strings))t范圍函數(shù)range>>>forxinrange(0,5):print(x)>>>count_by_twos=list(range(0,30,2))>>>print(count_by_twos)[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28]>>>count_down_by_twos=list(range(40,10,-2))>>>print(count_down_by_twos)[40,38,36,34,32,30,28,26,24,22,20,18,16,14,12]464.Python自帶函數(shù)-4取得長度len>>>475.Python自帶函數(shù)-5計算和文件訪問>>>test_file=open('c:\\test.txt')>>>text=test_()>>>print(text)文件內(nèi)容xxxxxxxxx>>>my_list_of_numbers=list(range(0,500,50))>>>print(my_list_of_numbers)[0,50,100,150,200,250,300,350,400,450]>>>print(sum(my_list_of_numbers))2250>>>test_file=open('c:\\my','w')>>>test_('Whatisgreenandloud?Afroghorn!')>>>test_()讀取文件寫入文件475.Python自帶函數(shù)-5計算和文件訪問>>>te48python兒童編程Python模塊是函數(shù)、類和變量的集合。為了使它們更容易使用。Python使用模塊來分組函數(shù)和類。例如,海龜模塊,我們在前幾章使用它,用它創(chuàng)建的畫布在屏幕上畫畫。48python兒童編程Python模塊是函數(shù)、類和變量的集491.復(fù)制模塊copy-1導(dǎo)入復(fù)制模塊復(fù)制模塊的使用實例>>>classAnimal:def__init__(self,species,number_of_legs,color):self.species=speciesself.number_of_legs=number_of_legsself.color=colorimportcopy>>>importcopy#導(dǎo)入復(fù)制模塊>>>harry=Animal(‘hippogriff’,6,‘pink’)#創(chuàng)建harry對象>>>harriet=copy.copy(harry)#把harry復(fù)制到harriet>>>print(harry.species)#輸出harry的species屬性hippogriff>>>print(harriet.species)#輸出hariet的species屬性hippogriff作用把一個對象復(fù)制給另一個對象就像你在復(fù)印機上復(fù)印資料一樣寫入文件創(chuàng)建一個動物類491.復(fù)制模塊copy-1導(dǎo)入復(fù)制模塊復(fù)制模塊的使用502.復(fù)制模塊copy-2Copy和deepcopy>>>harry=Animal('hippogriff',6,'pink')>>>carrie=Animal('chimera',4,'greenpolkadots')>>>billy=Animal('bogill',0,'paisley')>>>my_animals=[harry,carrie,billy]>>>more_animals=copy.copy(my_animals)>>>print(more_animals[0].species)hippogriff>>>print(more_animals[1].species)Chimera>>>my_animals[0].species='ghoul'>>>print(my_animals[0].species)ghoul>>>print(more_animals[0].species)ghoul>>>more_animals=copy.deepcopy(my_animals)>>>my_animals[0].species='wyrm'>>>print(my_animals[0].species)Wyrm>>>print(more_animals[0].species)ghoul502.復(fù)制模塊copy-2Copy和deepcop513.Python的關(guān)鍵字模塊關(guān)鍵字keyword>>>importkeyword>>>print(keyword.iskeyword('if'))True>>>print(keyword.iskeyword('ozwald'))False>>>print(keyword.kwlist)['False','None','True','and','as','assert','break','class','continue','def','del','elif','else','except','finally','for','from','global','if','import','in','is','lambda','nonlocal','not','or','pass','raise','return','try','while','with','yield']通過關(guān)鍵字模塊輸出python關(guān)鍵字,幫助我們認識到python語言中那些單詞是有特殊意義的,我們定義變量和函數(shù)時需要避開重名。513.Python的關(guān)鍵字模塊關(guān)鍵字keyword524.隨機函數(shù)模塊randomrandom返回制定范圍的隨機值>>>importrandom>>>print(random.randint(1,100))58>>>print(random.randint(100,1000))861choice從列表隨機取出一個項目>>>importrandom>>>desserts=['icecream','pancakes','brownies','cookies','candy']>>>print(random.choice(desserts))browniesShuffle把列表洗牌重新排序>>>importrandom>>>desserts=['icecream','pancakes','brownies','cookies','candy']>>>random.shuffle(desserts)>>>print(desserts)['pancakes','icecream','candy','brownies','cookies']524.隨機函數(shù)模塊randomrandom535.系統(tǒng)模塊對控制臺進行操作sysexit關(guān)閉控制帶>>>importsys>>>sys.exit()stdin.readline

從控制臺讀入輸入信息>>>importsys>>>v=sys.stdin.readline()Hewholaughslastthinksslowest>>>print(v)Hewholaughslastthinkssloweststdout.write把內(nèi)容輸出到控制臺>>>importsys>>>sys.stdout.write("Whatdoesafishsaywhenitswimsintoawall?Dam.")Whatdoesafishsaywhenitswimsintoawall?Dam.52>>>importsys>>>print(sys.version)3.1.2(r312:79149,Mar212013,00:41:52)[MSCv.150032bit(Intel)]version顯示系統(tǒng)版本535.系統(tǒng)模塊對控制臺進行操作sysexit關(guān)閉546.時間模塊time-1time取得現(xiàn)在時間>>>importtime>>>print(time.time())1300139149.34>>>deflots_of_numbers(max):ut1=time.time()vforxinrange(0,max):print(x)wt2=time.time()xprint('ittook%sseconds'%(t2-t1))>>>lots_of_numbers(1000)January1,1970,at00:00:00計算經(jīng)過的時間time.asctime取得可讀的時間>>>importtime>>>print(time.asctime())MonMar1122:03:412013>>>importtime>>>t=(2020,2,23,10,30,48,6,0,0)>>>print(time.asctime(t))SunFeb2310:30:482020time.asctime自己定義一個時間546.時間模塊time-1time取得現(xiàn)在時間>>557.時間模塊time-2time.localtime取得現(xiàn)在時間的列表>>>importtime>>>print(time.localtime())time.struct_time(tm_year=2020,tm_mon=2,tm_mday=23,tm_hour=22,tm_min=18,tm_sec=39,tm_wday=0,tm_yday=73,tm_isdst=0)>>>t=time.localtime()>>>year=t[0]>>>month=t[1]>>>print(year)2020>>>print(month)2time.sleep讓計算機休息一會兒>>>forxinrange(1,61):print(x)time.sleep(1)557.時間模塊time-2time.localtime568.保存信息模塊pickle保存map信息到文件>>>importpicklev>>>game_data={'player-position':'N23E45','pockets':['keys','pocketknife','polishedstone'],'backpack':['rope','hammer','apple'],'money':158.50}w>>>save_file=open('save.dat','wb')x>>>pickle.dump(game_data,save_file)y>>>save_()從文件讀取保存的信息>>>load_file=open('save.dat','rb')>>>loaded_game_data=pickle.load(load_file)>>>load_()>>>print(loaded_game_data){'money':158.5,'backpack':['rope','hammer','apple'],'player-position':'N23E45','pockets':['keys','pocketknife','polishedstone']}568.保存信息模塊pickle保存map信息到文件>>57python兒童編程57python兒童編程581.進階海龜繪圖運用學(xué)到的知識試試海龜畫出下面的圖581.進階海龜繪圖運用學(xué)到的知識試試海龜畫出下面的圖59python兒童編程59python兒童編程601.什么是圖形界面你現(xiàn)在使用的計算機就是圖形界面(例如)601.什么是圖形界面你現(xiàn)在使用的計算機就是圖形界面(例如612.Python的圖形界面Python的圖形包Importtkinter要開發(fā)圖形界面,首先要導(dǎo)入圖形包Python的圖形接口tkniter.Tk()創(chuàng)建基本的窗口Python的窗口控件tkniter.Button()按鍵tkniter.Canvas()用來在窗口畫圖的畫布等等。。。。。Python的窗口更新顯示xxxx.Pack()當(dāng)你畫了控件xxxx后需要用執(zhí)行Pack來讓它顯示612.Python的圖形界面Python的圖形包Impo623.Python的圖形界面Python的標準圖形控件控件描述Button按鈕控件;在程序中顯示按鈕。Canvas畫布控件;顯示圖形元素如線條或文本Checkbutton多選框控件;用于在程序中提供多項選擇框Entry輸入控件;用于顯示簡單的文本內(nèi)容Frame框架控件;在屏幕上顯示一個矩形區(qū)域,多用來作為容器Label標簽控件;可以顯示文本和位圖Listbox列表框控件;在Listbox窗口小部件是用來顯示一個字符串列表給用戶Menubutton菜單按鈕控件,由于顯示菜單項。Menu菜單控件;顯示菜單欄,下拉菜單和彈出菜單Message消息控件;用來顯示多行文本,與label比較類似Radiobutton單選按鈕控件;顯示一個單選的按鈕狀態(tài)Scale范圍控件;顯示一個數(shù)值刻度,為輸出限定范圍的數(shù)字區(qū)間Scrollbar滾動條控件,當(dāng)內(nèi)容超過可視化區(qū)域時使用,如列表框。.Text文本控件;用于顯示多行文本Toplevel容器控件;用來提供一個單獨的對話框,和Frame比較類似Spinbox輸入控件;與Entry類似,但是可以指定輸入范圍值PanedWindowPanedWindow是一個窗口布局管理的插件,可以包含一個或者多個子控件。LabelFramelabelframe是一個簡單的容器控件。常用與復(fù)雜的窗口布局。tkMessageBox用于顯示你應(yīng)用程序的消息框。623.Python的圖形界面Python的標準圖形控件控634.實現(xiàn)你的第一個圖形界面importtkinterdefhello():print('hellothere')tk=tkinter.Tk()btn=tkinter.Button(tk,text="clickme",command=hello,width=8,height=1)

btn.pack()canvas=tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_line(0,0,500,500)導(dǎo)入tkinter定義一個函數(shù),在控制臺輸出hellothere創(chuàng)建窗口在窗口加入按鍵,尺寸為8,1顯示click按下按鍵后執(zhí)行hello函數(shù)顯示按鍵創(chuàng)建畫布尺寸為500,500顯示畫布在畫布尺上畫一條線這是執(zhí)行結(jié)果634.實現(xiàn)你的第一個圖形界面importtkinter645.常用的繪圖方法-1繪制盒子importtkinterimportrandomtk=tkinter.Tk()canvas=tkinter.Canvas(tk,width=500,height=500)canvas.pack()defrandom_rectangle(width,height,fill_color):x1=random.randrange(width)y1=random.randrange(height)x2=x1+random.randrange(width)y2=y1+random.randrange(height)canvas.create_rectangle(x1,y1,x2,y2,fill=fill_color)forxinrange(0,100):random_rectangle(400,400,'#eb5699')645.常用的繪圖方法-1繪制盒子importtkint655.常用的繪圖方法-2繪制圓弧importtkintertk=tkinter.Tk()canvas=tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_arc(10,10,200,80,extent=359,style=tkinter.ARC)canvas.create_arc(100,100,200,200,extent=359,style=tkinter.ARC)參數(shù)的意義1.圖形左上角坐標2.圖形右下角坐標3.繪制角度4.繪制圓弧常量655.常用的繪圖方法-2繪制圓弧importtkint666.常用的繪圖方法-3繪制多邊形importtkintertk=tkinter.Tk()canvas=tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_polygon(1,1,100,10,100,110,fill="",outline="black")canvas.create_polygon(200,10,240,30,120,100,140,120,fill="",outline="black")參數(shù)的意義1.給出所有頂點的坐標666.常用的繪圖方法-3繪制多邊形importtkin677.常用的繪圖方法-4顯示文字importtkintertk=tkinter.Tk()canvas=tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_text(150,150,text='Hesaid,"It\'smycurse,',font=('Times',15))canvas.create_text(200,200,text='Butitcouldbeworse,',font=('Helvetica',20))canvas.create_text(220,250,text='Mycousinridesround',font=('Courier',22))canvas.create_text(220,300,text='onagoose."',font=('Courier',30))677.常用的繪圖方法-4顯示文字importtkint688.常用的繪圖方法-5顯示背景圖片importtkintertk=tkinter.Tk()canvas=tkinter.Canvas(tk,width=800,height=500)canvas.pack()my_image=tkinter.PhotoImage(file='5414231.gif')canvas.create_image(0,0,anchor=tkinter.NW,image=my_image)注意Tkinter只能處理gif圖片,如果要處理其他圖片需要用到python圖形庫注意NW告訴tkinter圖片左上角是原點,否則將以圖片中心作為原點688.常用的繪圖方法-5顯示背景圖片importtki699.常用的繪圖方法-6創(chuàng)作你的蒙太奇importtkinterimporttimetk=tkinter.Tk()canvas=tkinter.Canvas(tk,width=800,height=500)canvas.pack()mytriangle=canvas.create_polygon(10,10,10,60,50,35)forxinrange(0,60):canvas.move(1,5,0)tk.update()time.sleep(0.01)defmovetriangle(event):ifevent.keysym=='Up':canvas.move(1,0,-3)canvas.itemconfig(mytriangle,fill='blue')elifevent.keysym=='Down':canvas.move(1,0,3)canvas.itemconfig(mytriangle,fill='red')elifevent.keysym=='Left':canvas.move(1,-3,0)canvas.itemconfig(mytriangle,fill='green')else:canvas.move(1,3,0)canvas.itemconfig(mytriangle,fill='black')canvas.bind_all('<KeyPress-Up>',movetriangle)canvas.bind_all('<KeyPress-Down>',movetriangle)canvas.bind_all('<KeyPress-Left>',movetriangle)canvas.bind_all('<KeyPress-Right>',movetriangle)畫三角蒙太奇動作鍵盤事件動作鍵盤事件綁定Move三個參數(shù)1.畫布上圖片代號2.X坐標移動距離3.Y坐標移動距離699.常用的繪圖方法-6創(chuàng)作你的蒙太奇importtk70python兒童編程恭喜你學(xué)完python基本知識,下面可以完成2個簡單的游戲70python兒童編程恭喜你學(xué)完python基本知識,下面71python兒童編程71python兒童編程python兒童編程2018/3/17并非所有的蛇都會爬行python兒童編程2018/3/17并非所有的蛇都會爬行73python兒童編程你將了解

什么是python

在計算機上安裝并使用python2python兒童編程你將了解74python兒童編程一種計算機語言高級語言(Java,Vb,Ruby,Python,C等多達上百種)和人類一樣,計算機使用多種語言進行交流。一個編程語言只是一種與計算機對話的特殊方式。人類和計算機都能理解的指令。3python兒童編程一種計算機語言和人類一樣,計算機使用多75python兒童編程獲取安裝程序(下載)

https:///

注意根據(jù)操作系統(tǒng)選擇下載64或32位版本(可執(zhí)行文件)在windows下執(zhí)行安裝程序4python兒童編程獲取安裝程序(下載)76python兒童編程啟動pythonshell(IDLE)這就是PythonShellPythonShell就是在計算機上解釋執(zhí)行python語言的控制臺。相當(dāng)于你的大腦負責(zé)解釋你和別人所說的話,并按照要求進行動作。5python兒童編程啟動pythonshell(IDLE77python兒童編程你告訴計算機的第一句話>>>print("HelloWorld")HelloWorld>>>

讓計算機做幾道數(shù)學(xué)題

>>>3*52156>>>3670-1563514

SymbolOperation+Addition(加)-Subtraction(減)*Multiplication(乘)/Division(除)6python兒童編程你告訴計算機的第一句話讓計算機做幾道數(shù)78python兒童編程你將了解

什么是變量?

它能干什么?

如何使用它7python兒童編程你將了解79python兒童編程變量(variable)編程中的變量描述了存儲信息的地方。比如數(shù)字、文本、數(shù)字和文本等等。從另一方面看,變量就像一個標簽。>>>fred=100#定義一個變量,并給變量賦值>>>print(fred)#告訴計算機把變量表示的內(nèi)容顯示出來100>>>fred=200#定義一個變量,并給變量賦值>>>john=fred#定義另一個變量,并把fred的值賦值給它>>>print(john)200>>>found_coins=20>>>magic_coins=10>>>stolen_coins=3>>>found_coins+magic_coins*2-stolen_coins*3318python兒童編程變量(variable)>>>fre80python兒童編程你將了解STRINGS-----字符串LISTS-----列表TUPLES-----元組MAPS-----地圖9python兒童編程你將了解81python兒童編程String(字符串)在編程術(shù)語中,我們通常稱文本為字符串。你可以把一個字符串看作字母的集合,本資料里所有的字母、數(shù)字和符號都是一串字符。>>>fred='Whatispinkandfluffy?Pinkfluff!!'>>>print(fred)Whatispinkandfluffy?Pinkfluff!!創(chuàng)造一個字符串,把它放在變量里,讓計算機顯示出來說明字符串用”或者‘來定義字符串轉(zhuǎn)義符號\,試著頂一個I’AMCOMPUTER10python兒童編程String(字符串)>>>fre82python兒童編程在字符串種嵌入值>>>myscore=1000>>>message='Iscored%spoints'>>>print(message%myscore)Iscored1000points>>>nums='Whatdidthenumber%ssaytothenumber%s?Nicebelt!!'>>>print(nums%(0,8))Whatdidthenumber0saytothenumber8?Nicebelt!!字符串乘法>>>print(10*'a')Aaaaaaaaaa試試下面的輸出結(jié)果spaces=''*25print('%s12ButtsWynd'%spaces)11python兒童編程在字符串種嵌入值>>>myscor83python兒童編程LIST(列表)

很多變量的集合,用[]進行定義>>>some_numbers=[1,2,5,10,20]>>>some_strings=['Which','Witch','Is','Which']定義一個list你可以對list進行如下操作>>>some_some_strings.append(‘bearburp’)#追加項目>>>delsome_strings[2]#刪除第3項>>>print(some_strings[2:3])#顯示第3-4項>>>print(some_strings)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論