python儿童编程PPT演示课件(PPT 72页)_第1页
python儿童编程PPT演示课件(PPT 72页)_第2页
python儿童编程PPT演示课件(PPT 72页)_第3页
python儿童编程PPT演示课件(PPT 72页)_第4页
python儿童编程PPT演示课件(PPT 72页)_第5页
已阅读5页,还剩67页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、初级编程2018/3/17 并非所有的蛇都会爬行1第1页,共72页。第一章 开始P2你将了解 什么是python 在计算机上安装并使用python第2页,共72页。1.Python介绍一种计算机语言 高级语言(Java,Vb,Ruby,Python,C等多达上百种)P3和人类一样,计算机使用多种语言进行交流。一个编程语言只是一种与计算机对话的特殊方式。人类和计算机都能理解的指令。第3页,共72页。2.安装Python-1获取安装程序(下载) /downloads/windows/ 注意根据操作系统选择下载64或32位版本(可执行文件)在windows下执行安装程序P4第4页,共72页。2.安装

2、Python-2启动python shell(IDLE)P5这就是Python ShellPython Shell就是在计算机上解释执行python语言的控制台。相当于你的大脑负责解释你和别人所说的话,并按照要求进行动作。第5页,共72页。3.和计算机交流吧你告诉计算机的第一句话 print(Hello World) Hello World P6让计算机做几道数学题 3 * 52 156 3670 - 156 3514 Symbol Operation+ Addition(加)- Subtraction(减)* Multiplication(乘)/ Division(除)第6页,共72页。第二

3、章 编程第一步(变量)P7你将了解 什么是变量? 它能干什么? 如何使用它第7页,共72页。4.什么是变量变量(variable) 编程中的变量描述了存储信息的地方。比如数字、文本、数字和文本等等。从另一方面看,变量就像一个标签。P8 fred = 100 #定义一个变量,并给变量赋值 print(fred) #告诉计算机把变量表示的内容显示出来100 fred = 200 #定义一个变量,并给变量赋值 john = fred #定义另一个变量,并把fred的值赋值给它 print(john)200 found_coins = 20 magic_coins = 10 stolen_coins

4、= 3 found_coins + magic_coins * 2 - stolen_coins * 331第8页,共72页。第三章 编程第二步(常用数据)P9你将了解 STRINGS - 字符串 LISTS - 列表 TUPLES - 元组 MAPS - 地图第9页,共72页。1.字符串StringString(字符串)在编程术语中,我们通常称文本为字符串。你可以把一个字符串看作字母的集合,本资料里所有的字母、数字和符号都是一串字符。P10 fred = What is pink and fluffy? Pink fluff! print(fred)What is pink and fluf

5、fy? Pink fluff!创造一个字符串,把它放在变量里,让计算机显示出来说明字符串用” 或者 来定义字符串转义符号 , 试着顶一个 IAM COMPUTER第10页,共72页。1.字符串String在字符串种嵌入值P11 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 nu

6、mber 0 say to the number 8? Nice belt!字符串乘法 print(10 * a)Aaaaaaaaaa试试下面的输出结果spaces = * 25print(%s 12 Butts Wynd % spaces)第11页,共72页。2.比字符串更强大的列表(list)LIST(列表) 很多变量的集合,用进行定义P12 some_numbers = 1, 2, 5, 10, 20 some_strings = Which, Witch, Is, Which定义一个list你可以对list进行如下操作 some_ some_strings.append(bear bu

7、rp) #追加项目 del some_strings2 #删除第3项 print(some_strings2:3) #显示第3-4项 print(some_strings) #显示所有项 print(some_numbers + some_strings) #可以做加法 print(some_numbers * 5) #可以做乘法除法,减法不行哦!考虑一下为什么第12页,共72页。2.另一种列表元祖(tuples)TUPLE(元祖)元组类似于使用圆括号的列表,用()进行定义,区别是创建后不能更改P13 fibs = (0, 1, 1, 2, 3) print(fibs3)定义一个tuple你不

8、可以改变tuple的内容否则计算机给给你报错 fibs0 = 4Traceback (most recent call last):File , line 1, in fibs0 = 4TypeError: tuple object does not support item assignment第13页,共72页。2.帮你找到你想要的(字典)MAP(字典)字典中的每一项都有一个键和一个对应的值。你可以根据键找到值。P14 favorite_sports = Ralph Williams : Football,Michael Tippett :Basketball,Edward Elgar :

9、 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 #追加ca

10、ncan喜欢的项目第14页,共72页。第四章 海龟画图P15你可以画出绚丽的图案第15页,共72页。1.什么是海龟P16Turbles是一个画板模块,你可以利用它绘图。正如你写字并不需要你去制造铅笔和纸张,你可以利用turtle去绘画第16页,共72页。2.海龟绘图P17import turtle #引进海龟,你可以开始使用它turtle.pencolor(red) #设置画笔颜色(红色)turtle.pensize(1) #设置画笔粗细turtle.forward(100) #让海龟前进50个像素turtle.left(90) #左转90度turtle.forward(100) #让海龟继续

11、前进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的圆第1

12、7页,共72页。3.运用技巧P18import turtle #引进海龟,你可以开始使用它myColor=red,green,brownindex =0for x in range(250): turtle.pencolor(myColorindex) index +=1 if index = 3: index = 0 turtle.forward(x*2) turtle.left(92)右边的图怎么画出来的?看看下面的代码让计算机干了什么第18页,共72页。第五章 逻辑判断P19用IF ELSE判断逻辑第19页,共72页。1. 逻辑判断P20age = 10if age = 20: prin

13、t(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

14、= 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页,共72页。第六章 重复事件处理P24第24页,共72页。1. 循环P25作业要抄写100遍? NO! print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)print(“homework”)

15、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页,共72页。2. 列表(list)的循环P26 print(list(range(10, 20)10, 11, 12, 13, 14, 15, 16, 17, 18,

16、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, pantsfor i i

17、n hugehairypants: print(i) for j in hugehairypants: print(j)推测一下下面的结果第26页,共72页。3. 一道循环的计算题P27问题 宝箱里有20枚金币,每天会增加10枚,但是乌鸦每周会偷走3枚,请计算一年53周每周宝箱内会剩余多少金币 found_coins = 20 magic_coins = 70 stolen_coins = 3u coins = found_coinsv for week in range(1, 53):w coins = coins + magic_coins - stolen_coinsx print(We

18、ek %s = %s % (week, coins)第27页,共72页。4. 循环处理的几种语法P28for step in range(0, 20): print(step)FOR循环x = 45y = 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: breakBreak可以提前退出循环第28页,共72页。第七章 模块和函数P29函数是一些处理逻辑的集合模块是函数,变量的集合拥有更强大的功能海龟就是一个绘图模块第2

19、9页,共72页。1. 函数构成P30def 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页,共72页。2. 一

20、个函数的例子P31每周生产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页,共72页。P323. 模块(moudle)如何导入模块i

21、mport sys #导入系统模块Import turtle #导入海龟绘图模块只有导入模块后,才可以使用它第32页,共72页。P334. 使用sys模块sys模块内部有一个特殊的对象称为stdin(标准输入),它提供了一个相当有用的函数readline。ReadLine函数用于读取一行文本类型在键盘上,直到按回车键。Standard input的略称import sysdef ageEV(): print(How old are you?) age = int(sys.stdin.readline() if age 15 and age ozwald = Giraffes(100) gert

22、rude = Giraffes(150) print(ozwald.giraffe_spots)100 print(gertrude.giraffe_spots)150初期化函数的例子初期化函数的使用实例第41页,共72页。第九章 python自带的常用函数P42第42页,共72页。P431. 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(bo

23、ol(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_, _getnew

24、args_, _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_, _

25、rtruediv_, _rxor_, _setattr_, _sizeof_, _str_, _sub_, _subclasshook_, _truediv_, _trunc_, _xor_, bit_length, conjugate, denominator, from_bytes, imag, numerator, real, to_bytes第43页,共72页。P442. Python自带函数-2获得帮助help help(abs)Help on built-in function abs in module builtins:abs(x, /) Return the absolute

26、 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)hamsandwich区别 eval 可以有返回值 exec 无返回值第44页,共72页。P453. Python自

27、带函数-3浮点值 float() print(abs(10)10整数 int() float(123.456789)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

28、last):File , line 1, in int(123.456)ValueError: invalid literal for int() with base 10: 123.456出错了!字符串123.456不可以第45页,共72页。P464. 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,

29、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_two

30、s)40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12第46页,共72页。P475. Python自带函数-5计算和文件访问 test_file = open(c:test.txt) text = test_file.read() 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(m

31、y_list_of_numbers)2250 test_file = open(c:myfile.txt, w) test_file.write(What is green and loud? A froghorn!) test_file.close()读取文件写入文件第47页,共72页。第十章 python常用的模块P48Python模块是函数、类和变量的集合。为了使它们更容易使用。 Python使用模块来分组函数和类。例如,海龟模块,我们在前几章使用它,用它创建的画布在屏幕上画画。第48页,共72页。P491. 复制模块 copy -1导入复制模块复制模块的使用实例 class Anima

32、l:def _init_(self, species, number_of_legs, color):self.species = speciesself.number_of_legs = number_of_legsself.color = colorimport copy import copy #导入复制模块 harry = Animal(hippogriff, 6, pink) #创建harry对象 harriet = copy.copy(harry) #把harry复制到harriet print(harry.species) #输出harry的species属性hippogriff

33、 print(harriet.species) #输出hariet的species属性hippogriff作用 把一个对象复制给另一个对象就像你在复印机上复印资料一样写入文件创建一个动物类第49页,共72页。P502. 复制模块 copy-2Copy 和 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

34、 = 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(m

35、ore_animals0.species)ghoul第50页,共72页。P513. 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, i

36、n, is, lambda,nonlocal, not, or, pass, raise, return, try, while,with, yield通过关键字模块输出python关键字,帮助我们认识到python语言中那些单词是有特殊意义的,我们定义变量和函数时需要避开重名。第51页,共72页。P524. 随机函数模块 randomrandom 返回制定范围的随机值 import random print(random.randint(1, 100)58 print(random.randint(100, 1000)861choice 从列表随机取出一个项目 import random d

37、esserts = ice cream, pancakes, brownies, cookies,candy print(random.choice(desserts)browniesShuffle 把列表洗牌重新排序 import random desserts = ice cream, pancakes, brownies, cookies,candy random.shuffle(desserts) print(desserts)pancakes, ice cream, candy, brownies, cookies第52页,共72页。P535. 系统模块对控制台进行操作 sysexi

38、t 关闭控制带 import sys sys.exit()stdin.readline 从控制台读入输入信息 import sys v = sys.stdin.readline()He who laughs last thinks slowest print(v)He who laughs last thinks sloweststdout.write 把内容输出到控制台 import sys sys.stdout.write(What does a fish say when it swims into a wall?Dam.)What does a fish say when it swi

39、ms into a wall? Dam.52 import sys print(sys.version)3.1.2 (r312:79149, Mar 21 2013, 00:41:52) MSC v.1500 32 bit (Intel)version 显示系统版本第53页,共72页。P546. 时间模块 time-1time 取得现在时间 import time print(time.time()1300139149.34 def lots_of_numbers(max):u t1 = time.time()v for x in range(0, max):print(x)w t2 = ti

40、me.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 2020time.asctime 自己定义一个时间第54

41、页,共72页。P557. 时间模块 time-2time.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)2time.sleep 让计算机休息一会儿 for

42、x in range(1, 61):print(x)time.sleep(1)第55页,共72页。P568. 保存信息模块 pickle保存map信息到文件 import picklev game_data = player-position : N23 E45,pockets : keys, pocket knife, polished stone,backpack : rope, hammer, apple,money : 158.50w save_file = open(save.dat, wb)x pickle.dump(game_data, save_file)y save_file

43、.close()从文件读取保存的信息 load_file = open(save.dat, rb) loaded_game_data = pickle.load(load_file) load_file.close() print(loaded_game_data)money: 158.5, backpack: rope, hammer, apple,player-position: N23 E45, pockets: keys, pocket knife,polished stone第56页,共72页。第十章 高级海龟绘图P57第57页,共72页。P581. 进阶海龟绘图运用学到的知识试试海

44、龟画出下面的图第58页,共72页。第十一章 图形界面P59第59页,共72页。P601. 什么是图形界面你现在使用的计算机就是图形界面(例如)第60页,共72页。P612. Python的图形界面Python的图形包Import tkinter要开发图形界面,首先要导入图形包Python的图形接口tkniter.Tk()创建基本的窗口Python的窗口控件tkniter.Button()按键tkniter.Canvas()用来在窗口画图的画布等等。Python的窗口更新显示xxxx.Pack()当你画了控件xxxx后需要用执行Pack来让它显示第61页,共72页。P623. Python的图形

45、界面Python的标准图形控件控件描述Button按钮控件;在程序中显示按钮。Canvas画布控件;显示图形元素如线条或文本Checkbutton多选框控件;用于在程序中提供多项选择框Entry输入控件;用于显示简单的文本内容Frame框架控件;在屏幕上显示一个矩形区域,多用来作为容器Label标签控件;可以显示文本和位图Listbox列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户Menubutton菜单按钮控件,由于显示菜单项。Menu菜单控件;显示菜单栏,下拉菜单和弹出菜单Message消息控件;用来显示多行文本,与label比较类似Radiobutton单选按钮控件

46、;显示一个单选的按钮状态Scale范围控件;显示一个数值刻度,为输出限定范围的数字区间Scrollbar滚动条控件,当内容超过可视化区域时使用,如列表框。.Text文本控件;用于显示多行文本Toplevel容器控件;用来提供一个单独的对话框,和Frame比较类似Spinbox输入控件;与Entry类似,但是可以指定输入范围值PanedWindowPanedWindow是一个窗口布局管理的插件,可以包含一个或者多个子控件。LabelFramelabelframe 是一个简单的容器控件。常用与复杂的窗口布局。tkMessageBox用于显示你应用程序的消息框。第62页,共72页。P634. 实现你

47、的第一个图形界面import tkinterdef 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 显示cli

48、ck 按下按键后执行hello函数显示按键创建画布尺寸为500,500显示画布在画布尺上画一条线这是执行结果第63页,共72页。P645. 常用的绘图方法-1绘制盒子import tkinterimport randomtk=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+ran

49、dom.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页,共72页。P655. 常用的绘图方法-2绘制圆弧import tkintertk=tkinter.Tk()canvas = tkinter.Canvas(tk,width=500,height=500)canvas.pack()canvas.create_arc(10

50、,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页,共72页。P666. 常用的绘图方法-3绘制多边形import tkintertk=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_po

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论