python儿童编程

上传人:cel****303 文档编号:243691074 上传时间:2024-09-28 格式:PPTX 页数:72 大小:4.68MB
返回 下载 相关 举报
python儿童编程_第1页
第1页 / 共72页
python儿童编程_第2页
第2页 / 共72页
python儿童编程_第3页
第3页 / 共72页
点击查看更多>>
资源描述
, 書式設定, 書式設定,第 2 ,第 3 ,第 4 ,第 5 ,*,*,初级编程,2018/3/17,并非所有的蛇都会爬行,第一章 开始,你将了解,什么是,python,在计算机上安装并使用,python,2,介绍,一种计算机语言,高级语言(,Java,,,Vb,,,Ruby,,,Python,C,等多达上百种),和人类一样,计算机使用多种语言进行交流。一个编程语言只是一种与计算机对话的特殊方式。人类和计算机都能理解的指令。,3,2,.,安装,Python,-1,获取安装程序(下载),https:/,注意根据操作系统选择下载,64,或,32,位版本(可执行文件),在,windows,下执行安装程序,4,2,.,安装,Python,-2,启动,python shell(,IDLE,),这就是,Python Shell,Python Shell,就是在计算机上解释执行,python,语言的控制台。,相当于你的大脑负责解释你和别人所说的话,并按照要求进行动作。,5,3,.,和计算机交流吧,你告诉计算机的第一句话, print(Hello World),Hello World,让计算机做几道数学题, 3 * 52,156, 3670 - 156,3514,Symbol Operation,+ Addition,(加),- Subtraction,(减),* Multiplication,(乘),/ Division,(除),6,第二章 编程第一步(变量),你将了解,什么是变量?,它能干什么?,如何使用它,7,4,.,什么是变量,变量(,variable,),编程中的变量描述了存储信息的地方。比如数字、文本、数字和文本等等。从另一方面看,变量就像一个标签。, fred = 100 #定义一个变量,并给变量赋值, print(fred) #告诉计算机把变量表示的内容显示出来,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 * 3,31,8,第三章 编程第二步(常用数据),你将了解,STRINGS -,字符串,LISTS -,列表,TUPLES -,元组,MAPS -,地图,9,1.,字符串,String,String,(字符串),在编程术语中,我们通常称文本为字符串。你可以把一个字符串看作字母的集合,本资料里所有的字母、数字和符号都是一串字符。, fred = What is pink and fluffy? Pink fluff!, print(fred),What is pink and fluffy? Pink fluff!,创造一个字符串,把它放在变量里,让计算机显示出来,说明,字符串用,”,或者,来定义,字符串转义符号, ,试着顶一个,IAM COMPUTER,10,1.,字符串,String,在字符串种嵌入值, myscore = 1000, message = I scored %s points, print(message % myscore),I scored 1000 points, nums = What did the number %s say to the number %s? Nice belt!, print(nums % (0, 8),What did the number 0 say to the number 8? Nice belt!,字符串乘法, print(10 * a),Aaaaaaaaaa,试试下面的输出结果,spaces = * 25,print(%s 12 Butts Wynd % spaces),11,2,.,比字符串更强大的,列表(,list,),LIST,(列表),很多变量的集合,用,进行定义, some_numbers = 1, 2, 5, 10, 20, some_strings = Which, Witch, Is, Which,定义一个,list,你可以对,list,进行如下操作, some_ some_strings.append(bear burp),#,追加项目, del some_strings2 #,删除第,3,项, print(some_strings2:3),#,显示第,3-4,项, print(some_strings),#,显示所有项, print(some_numbers + some_strings) #,可以做加法, print(some_numbers * 5) #,可以做乘法,除法,减法不行哦!,考虑一下为什么,12,2,.,另一种,列表元祖(,tuples,),TUPLE,(元祖),元组类似于使用圆括号的列表,用,(),进行定义,区别是创建后不能更改, fibs = (0, 1, 1, 2, 3), print(fibs3),定义一个,tuple,你不可以改变,tuple,的内容否则计算机给给你报错, fibs0 = 4,Traceback (most recent call last):,File , line 1, in ,fibs0 = 4,TypeError: tuple object does not support item assignment,13,2,.,帮你找到你想要的,(字典,),MAP,(字典),字典中的每一项都有一个键和一个对应的值。你可以根据键找到值。, favorite_sports = Ralph Williams : Football,Michael Tippett :Basketball,Edward Elgar : Baseball,Rebecca Clarke : Netball,Ethel Smyth : Badminton,Frank Bridge : Rugby,定义一个,map,你可以对字典做如下操作, print(favorite_sportsRebecca Clarke),#,找到,RebeccaClarke,喜欢的运动,del favorite_sportsEthel Smyth #,从字典中删除,EthelSmyth,数据,favorite_sportsEthel Smyth = Ice Hockey #,修改,Ethel Smyth,喜欢的运动,favorite_sportsCan Can=tennis #,追加,cancan,喜欢的项目,14,第四章 海龟画图,你可以画出绚丽的图案,15,1.,什么是海龟,Turbles,是一个画板模块,你可以利用它绘图。,正如你写字并不需要你去制造铅笔和纸张,,你可以利用,turtle,去绘画,16,2.,海龟绘图,import turtle #引进海龟,你可以开始使用它,turtle.pencolor(red) #设置画笔颜色(红色),turtle.pensize(1) #设置画笔粗细,turtle.forward(100) #让海龟前进50个像素,turtle.left(90) #左转90度,turtle.forward(100) #让海龟继续前进50个像素,turtle.left(90) #左转90度,turtle.forward(100) #让海龟继续前进50个像素,turtle.left(90) #左转90度,turtle.forward(100) #让海龟继续前进50个像素,turtle.up() #让海龟抬起笔,turtle.left(90) #左转90度,turtle.forward(50) #让海龟继续前进25个像素,turtle.down() #让海龟放下笔,turtle.pencolor(green) #设置画笔颜色(绿色),turtle.pensize(3) #设置画笔粗细,turtle.circle(50) #画一个半径50的圆,17,3.,运用技巧,import turtle #,引进海龟,你可以开始使用它,myColor=red,green,brown,index =0,for x in range(250):,turtle.pencolor(myColorindex),index +=1,if index = 3:,index = 0,turtle.forward(x*2),turtle.left(92),右边的图怎么画出来的?,看看下面的代码让计算机干了什么,18,第五章 逻辑判断,用,IF ELSE,判断逻辑,19,1.,逻辑判断,age = 10,if age = 20:,print(oh!you are yong),Elif age 20 and age = 10 and age myval = None, if myval = None:,print(The variable myval doesnt have a value),什么都没有保存的空值, age=10, if age = 10:,print(The variable myval doesnt have a value),数值是字符串还是数字?, age=10, if age = 10:,print(The variable myval doesnt have a value), age = 10, converted_age = int(age), age = 10, converted_age = str(age), age = 10.5, converted_age = int(age), if age = 10:,print(The variable myval doesnt have a value),结果如何,23,第六章 重复事件处理,24,1.,循环,作业要抄写,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”),so,easy!,for x in range(0, 99):,print(homework),for x in range(0, 99):,print(hello %s % x),试试这个,25,2.,列表(,list),的循环, print(list(range(10, 20),10, 11, 12, 13, 14, 15, 16, 17, 18, 19,简单的列表打印,class_list = class1,class2,class3,class4,class5,for x in range(0, 4):,print(hello %s % class_listx),循环方式的列表打印,循环方式的遍历列表, wizard_list = spider legs, toe of frog, snail tongue,bat wing, slug butter, bear burp, for i in wizard_list:,print(i),左边的,1,和,2,实现方式有什么区别?,hugehairypants = huge, hairy, pants,for i in hugehairypants:,print(i),for j in hugehairypants:,print(j),推测一下下面的结果,26,3,.,一道循环的计算题,问题,宝箱里有,20,枚金币,每天会增加,10,枚,但是乌鸦每周会偷走,3,枚,请计算一年,53,周每周宝箱内会剩余多少金币, found_coins = 20, magic_coins = 70, stolen_coins = 3,u coins = found_coins,v for week in range(1, 53):,w coins = coins + magic_coins - stolen_coins,x print(Week %s = %s % (week, coins),27,4,.,循环处理的几种语法,for step in range(0, 20):,print(step),FOR,循环,x = 45,y = 80,while x 50 and y 100:,x = x + 1,y = y + 1,print(x, y),WHILE,循环,for x in range(0, 20):,print(hello %s % x),if x 9:,break,Break,可以提前退出循环,28,第,七,章 模块和函数,函数是一些处理逻辑的集合,模块是函数,变量的集合拥有更强大的功能,海龟就是一个绘图模块,29,1.,函数构成,def,testfunc,(,myname,):,print(hello %s % myname),函数名,,参数,,,处理,testfunc(Mary),print(savings(10, 10, 5),执行函数,def testfunc(,fname, lname,):,print(Hello %s %s % (fname, lname),函数可以有多个,参数,函数可以有返回值,def savings(pocket_money, paper_route, spending):,return pocket_money + paper_route spending,30,2,.,一个函数的例子,每周生产,X,个罐子,计算出一年中每周位置总共生产的罐子。,def spaceship_building(cans):,total_cans = 0,for week in range(1, 53):,total_cans = total_cans + cans,print(Week %s = %s cans % (week, total_cans),函数调用,spaceship_building(2) #,A,工厂每周只能生产,2,个,spaceship_building(10) #,B,工厂每周只能生产,10,个,考虑一下使用函数的好处,31,3.,模块(,moudle),如何导入模块,import sys,#,导入系统模块,Import turtle,#,导入海龟绘图模块,只有导入模块后,才可以使用它,32,4.,使用,sys,模块,sys模块内部有一个特殊的对象称为stdin(标准输入),它提供了一个相当有用的函数readline。ReadLine函数用于读取一行文本类型在键盘上,直到按回车键。,Standard,input,的略称,import sys,def ageEV():,print(How old are you?),age = int(sys.stdin.readline(),if age 15 and age ozwald = Giraffes(100), gertrude = Giraffes(150), print(ozwald.giraffe_spots),100, print(gertrude.giraffe_spots),150,初期化函数的例子,初期化函数的使用实例,41,第,九,章,python,自带的常用函数,42,1. Python,自带函数,-1,获得绝对值,abs(), print(abs(10),10,布尔变量,bool(), print(bool(0),False, print(bool(1),True, print(bool(a),Dir,函数, print(bool(0),False, print(bool(1),True, print(bool(a),#,用它来计算绝对值,#,用它来取得逻辑真假,可进行,IF,判断,还记得条件语法吗,if elif else,#,它的参数是任意类型,执行结果可以告诉你,可以处理这种类型所有的函数。你需要从一堆结果中找出自己有用 的信息。看看下面的记过,对于整数你可以利用那些函数。, 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,43,2. Python,自带函数,-2,获得帮助,help, help(abs),Help on built-in function abs in module builtins:,abs(x, /),Return the absolute value of the argument.,执行命令函数,eval, your_calculation = input(Enter a calculation: ),Enter a calculation: 12*52, eval(your_calculation),624,#,用它让,Python,告诉你函数的使用方法,不过都是英文哦!,执行命令函数,eval, my_small_program = print(ham),print(sandwich), exec(my_small_program),ham,sandwich,区别,eval,可以有返回值,exec,无返回值,44,3. Python,自带函数,-3,浮点值,float(), print(abs(10),10,整数,int(), float(123.456789), your_age = input(Enter your age: ),Enter your age: 20, age = float(your_age), if age 13:,print(You are %s years too old % (age - 13),You are 7.0 years too old,#,带很多位小数的值, int(123.456),123, int(123),123, int(123.456),Traceback (most recent call last):,File , line 1, in ,int(123.456),ValueError: invalid literal for int() with base 10: 123.456,出错了!,字符串,123.456,不可以,45,4. Python,自带函数,-4,取得长度,len, len(this is a test string),21, creature_list = unicorn, cyclops, fairy, elf, dragon,troll, print(len(creature_list),6,取得最大数,最小值,max min, 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,范围函数,range, for x in range(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,46,5. Python,自带函数,-5,计算和,文件访问, test_file = open(c:test.txt), text = test_(), print(text),文件内容,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_(What is green and loud? A froghorn!), test_(),读取文件,写入文件,47,第,十,章,python,常用的模块,Python模块是函数、类和变量的集合。为了使它们更容易使用。 Python使用模块来分组函数和类。例如,海龟模块,我们在前几章使用它,用它创建的画布在屏幕上画画。,48,1.,复制模块,copy -1,导入复制模块,复制模块的使用实例, class Animal:,def _init_(self, species, number_of_legs, color):,self.species = species,self.number_of_legs = number_of_legs,self.color = color,import copy, import copy,#,导入复制模块, harry = Animal(hippogriff, 6, pink),#,创建,harry,对象, harriet = copy.copy(harry),#,把,harry,复制到,harriet, print(harry.species) #,输出,harry,的,species,属性,hippogriff, print(harriet.species) #,输出,hariet,的,species,属性,hippogriff,作用 把一个对象复制给另一个对象,就像你在复印机上复印资料一样,写入文件,创建一个动物类,49,2.,复制模块,copy-2,Copy,和,deepcopy, harry = Animal(hippogriff, 6, pink), carrie = Animal(chimera, 4, green polka dots), billy = Animal(bogill, 0, paisley), my_animals = harry, carrie, billy, more_animals = copy.copy(my_animals), print(more_animals0.species),hippogriff, print(more_animals1.species),Chimera, my_animals0.species = ghoul, print(my_animals0.species),ghoul, print(more_animals0.species),ghoul, more_animals = copy.deepcopy(my_animals), my_animals0.species = wyrm, print(my_animals0.species),Wyrm, print(more_animals0.species),ghoul,50,3. Python,的关键字模块,关键字,keyword, import keyword, 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,通过关键字模块输出,python,关键字,帮助我们认识到,python,语言中那些单词是有特殊意义的,我们定义变量和函数时需要避开重名。,51,4.,随机函数模块,random,random,返回制定范围的随机值, import random, print(random.randint(1, 100),58, print(random.randint(100, 1000),861,choice,从列表随机取出一个项目, import random, desserts = ice cream, pancakes, brownies, cookies,candy, print(random.choice(desserts),brownies,Shuffle,把列表洗牌重新排序, import random, desserts = ice cream, pancakes, brownies, cookies,candy, random.shuffle(desserts), print(desserts),pancakes, ice cream, candy, brownies, cookies,52,5.,系统模块对控制台进行操作,sys,exit,关闭控制带, import sys, sys.exit(),s,从控制台读入输入信息, import sys, v = sys.stdin.readline(),He who laughs last thinks slowest, print(v),He who laughs last thinks slowest,stdout.write,把内容输出到控制台, import sys, sys.stdout.write(What does a fish say when it swims into a wall?,Dam.), import sys, print(sys.version),3.1.2 (r312:79149, Mar 21 2013, 00:41:52) MSC v.1500 32 bit (Intel),version,显示系统版本,53,6.,时间模块,time-1,time,取得现在时间, import time, print(time.time(), def lots_of_numbers(max):,u t1 = time.time(),v for x in range(0, max):,print(x),w t2 = time.time(),x print(it took %s seconds % (t2-t1), lots_of_numbers(1000),January 1, 1970, at 00:00:00,计算经过的时间,time.asctime,取得可读的时间, import time, print(time.asctime(),Mon Mar 11 22:03:41 2013, import time, t = (2020, 2, 23, 10, 30, 48, 6, 0, 0), print(time.asctime(t),Sun Feb 23 10:30:48 2020,time.asctime,自己定义一个时间,54,7.,时间模块,time-2,time.localtime,取得现在时间的列表, import time, 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 = t0, month = t1, print(year),2020, print(month),2,time.sleep,让计算机休息一会儿, for x in range(1, 61):,print(x),time.sleep(1),55,8.,保存信息模块,pickle,保存,map,信息到文件, import pickle,v game_data = ,player-position : N23 E45,pockets : keys, pocket knife, polished stone,backpack : rope, hammer, apple,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: N23 E45, pockets: keys, pocket knife,polished stone,56,第,十,章,高级海龟绘图,57,1.,进阶海龟绘图,运用学到的知识试试海龟画出下面的图,58,第,十一,章 图形界面,59,1.,什么是图形界面,你现在使用的计算机就是图形界面(例如,),60,2. Python,的图形界面,Python,的图形包,Import,tkinter,要开发图形界面,首先要导入图形包,Python,的图形,接口,tkniter.Tk(),创建基本的窗口,Python,的窗口控件,tkniter.Button(),按键,tkniter.Canvas(),用来在窗口画图的画布,等等。,Python,的窗口更新显示,xxxx.Pack(),当你画了控件,xxxx,后需要用执行,Pack,来让它显示,61,3. Python,的图形界面,Python,的标准图形控件,控件,描述,Button,按钮控件;在程序中显示按钮。,Canvas,画布控件;显示图形元素如线条或文本,Checkbutton,多选框控件;用于在程序中提供多项选择框,Entry,输入控件;用于显示简单的文本内容,Frame,框架控件;在屏幕上显示一个矩形区域,多用来作为容器,Label,标签控件;可以显示文本和位图,Listbox,列表框控件;在,Listbox,窗口小部件是用来显示一个字符串列表给用户,Menubutton,菜单按钮控件,由于显示菜单项。,Menu,菜单控件;显示菜单栏,下拉菜单和弹出菜单,Message,消息控件;用来显示多行文本,与,label,比较类似,Radiobutton,单选按钮控件;显示一个单选的按钮状态,Scale,范围控件;显示一个数值刻度,为输出限定范围的数字区间,Scrollbar,滚动条控件,当内容超过可视化区域时使用,如列表框。,.,Text,文本控件;用于显示多行文本,Toplevel,容器控件;用来提供一个单独的对话框,和,Frame,比较类似,Spinbox,输入控件;与,Entry,类似,但是可以指定输入范围值,PanedWindow,PanedWindow,是一个窗口布局管理的插件,可以包含一个或者多个子控件。,LabelFrame,labelframe,是一个简单的容器控件。常用与复杂的窗口布局。,tkMessageBox,用于显示你应用程序的消息框。,62,4.,实现你的第一个图形界面,import tkinter,def hello():,print(hello there),tk=tkinter.Tk(),btn = tkinter.Button(tk,text=click me,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),导入,tkinter,定义一个函数,在控制台输出,hello there,创建窗口,在窗口加入按键,尺寸为,8,1,显示,click,按下按键后执行,hello,函数,显示按键,创建画布尺寸为,500,500,显示画布,在画布尺上画一条线,这是执行结果,63,5.,常用的绘图方法,-1,绘制盒子,import tkinter,import random,tk=tkinter.Tk(),canvas = tkinter.Canvas(tk,width=500,height=500),canvas.pack(),def random_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),for x in range(0,100):,random_rectangle(400,400,#eb5699),64,5.,常用的绘图方法,-2,绘制圆弧,import tkinter,tk=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),参数的意义,1.,图形左上角坐标,2.,图形右下角坐标,3.,绘制角度,4.,绘制圆弧常量,65,6.,常用的绘图方法,-3,绘制多边形,import tkinter,tk=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),参数的意义,1.,给出所有顶点的坐标,66,7.,常用的绘图方法,-4,显示文字,import tkinter,tk=tkinter.Tk(),canvas = tkinter.Canvas(tk,width=500,height=500),canvas.pack(),canvas.create_text(150, 150, text=He said, Its my curse,font=(Times, 15),canvas.create_text(200, 200, text=But it could be worse,font=(Helvetica, 20),canvas.create_text(220, 250, text=My cousin rides round,font=(Courier, 22),canvas.create_text(220, 300, text=on a goose., font=(Courier, 30),67,8.,常用的绘图方法,-5,显示背景图片,import tkinter,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 压缩资料 > 基础医学


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!