收藏 分享(赏)

Python入门教程.doc

上传人:tangtianxu1 文档编号:2869599 上传时间:2018-09-29 格式:DOC 页数:90 大小:456.05KB
下载 相关 举报
Python入门教程.doc_第1页
第1页 / 共90页
Python入门教程.doc_第2页
第2页 / 共90页
Python入门教程.doc_第3页
第3页 / 共90页
Python入门教程.doc_第4页
第4页 / 共90页
Python入门教程.doc_第5页
第5页 / 共90页
点击查看更多>>
资源描述

1、 火 龙 果 整 理uml.orgcnPython 入门教程 1 - Python Syntax1 Python 是一个高效的语言,读和写的操作都是很简单的,就像普通的英语一样2 Python 是一个解释执行的语言,我们不需要去编译,我们只要写出代码即可运行3 Python 是一个面向对象的语言,在 Python 里面一切皆对象4 Python 是一门很有趣的语言5 变量:一个变量就是一个单词,只有一个单一的值练习:设置一个变量 my_variable,值设置为 10 cpp #Write your code below! my_variable = 10 3 第三节1 Python 里面有三

2、种数据类型 interage , floats , booleans2 Python 是一个区分大小写的语言3 练习1 把变量 my_int 值设置为 72 把变量 my_float 值设置为 1.233 把变量 my_bool 值设置为 truepython 火 龙 果 整 理uml.orgcn#Set the variables to the values listed in the instructions! my_int = 7 my_float = 1.23 my_bool = True 6 Python 的变量可以随时进行覆盖2 练习:my_int 的值从 7 改为 3,并打印出

3、my_intpython #my_int is set to 7 below. What do you think #will happen if we reset it to 3 and print the result? my_int = 7 #Change the value of my_int to 3 on line 8! my_int = 3 #Heres some code that will print my_int to the console: #The print keyword will be covered in detail soon! print my_int 火

4、 龙 果 整 理uml.orgcn7 Pyhton 的声明和英语很像8 Python 里面声明利用空格在分开3 练习: 查看以下代码的错误python def spam(): eggs = 12 return eggs print spam() 9 Python 中的空格是指正确的缩进2 练习: 改正上一节中的错误python def spam(): eggs = 12 return eggs print spam() 10 Python 是一种解释执行的语言,只要你写完即可立即运行2 练习:设置变量 spam 的只为 True,eggs 的值为 False python 火 龙 果 整 理u

5、ml.orgcnspam = True eggs = False 11 Python 的注释是通过“#”来实现的,并不影响代码的实现2 练习:给下面的代码加上一行注释python #this is a comments for Python mysterious_variable = 42 12 Python 的多行注释是通过“ “ “ ”来实现的2 练习:把下面的代码加上多行python “ this is a Python course “ a = 5 13 Python 有 6 种算术运算符 +,-,*,/,*(幂),%2 练习:把变量 count_to 设置为 1+2python #S

6、et count_to equal to 1 plus 2 on line 3! 火 龙 果 整 理uml.orgcncount_to = 1+2 print count_to 14 Python 里面求 xm,写成 x*m2 练习:利用幂运算,把 eggs 的值设置为 100python #Set eggs equal to 100 using exponentiation on line 3! eggs = 10*2 print eggs 1 练习:1 写一行注释2 把变量 monty 设置为 True3 把变量 python 值设置为 1.2344 把 monty_python 的值设置

7、为 python 的平方python #this is a Python monty = True python = 1.234 monty_python = python*2 火 龙 果 整 理uml.orgcnPython 入门教程 2 - Tip Calculator1 把变量 meal 的值设置为 44.50python #Assign the variable meal the value 44.50 on line 3! meal = 44.50 1 把变量 tax 的值设置为 6.75% python meal = 44.50 tax = 6.75/100 1 设置 tip 的值

8、为 15% python #Youre almost there! Assign the tip variable on line 5. meal = 44.50 火 龙 果 整 理uml.orgcntax = 0.0675 tip = 0.15 1 把变量 meal 的值设置为 meal+meal*taxpython #Reassign meal on line 7! meal = 44.50 tax = 0.0675 tip = 0.15 meal = meal+meal*tax 设置变量 total 的值为 meal+meal*taxpython #Assign the variable

9、 total on line 8! meal = 44.50 tax = 0.0675 tip = 0.15 meal = meal + meal * tax total = meal + meal * tip print(“%.2f“ % total) 火 龙 果 整 理uml.orgcnPython 入门教程 3 - Strings and Console Output15 Python 里面还有一种好的数据类型是 String16 一个 String 是通过 或者 “包成的串3 设置变量 brian 值为“Always look on the bright side of life!“p

10、ython #Set the variable brian on line 3! brian = “Always look on the bright side of life!“ 1 练习1 把变量 caesar 变量设置为 Graham2 把变量 praline 变量设置为 john3 把变量 viking 变量设置为 Teresapython #Assign your variables below, each on its own line! caesar = “Graham“ praline = “John“ viking = “Teresa“ #Put your variables

11、 above this line print caesar print praline 火 龙 果 整 理uml.orgcnprint viking 17 Python 是通过来实现转义字符的2 练习把Help! Help! Im being repressed! 中的 Im 中的进行转义python #The string below is broken. Fix it using the escape backslash! Help! Help! m being repressed! 18 我们可以使用“ 来避免转义字符的出现2 练习: 把变量 fifth_letter 设置为 MONTY

12、 的第五个字符python “ The string “PYTHON“ has six characters, numbered 0 to 5, as shown below: +-+-+-+-+-+-+ | P | Y | T | H | O | N | +-+-+-+-+-+-+ 0 1 2 3 4 5 So if you wanted “Y“, you could just type “PYTHON“1 (always start counting from 0!) 火 龙 果 整 理uml.orgcn“ fifth_letter = “MONTY“4 print fifth_lette

13、r 19 介绍 String 的第一种方法, len()求字符串的长度2 练习: 把变量 parrot 的值设置为“Norweigian Blue“,然后打印 parrot 的长度python parrot = “Norwegian Blue“ print len(parrot) 20 介绍 String 的第二种方法 ,lower()把所有的大写字母转化为小写字母2 练习: 把 parrot 中的大写字母转换为小写字母并打印python parrot = “Norwegian Blue“ print parrot.lower() 21 介绍 String 的第三种方法 ,upper()把所有

14、的大写字母转化为小写字母2 练习: 把 parrot 中的小写字母转换为大写字母并打印火 龙 果 整 理uml.orgcnpython parrot = “norwegian blue“ print parrot.upper() 第八节1 介绍 String 的第四种方法,str()把非字符串转化为字符串,比如 str(2)是把 2 转化为字符串“2“2 练习: 设置一个变量 pi 值为 3.14 , 把 pi 转化为字符串python “Declare and assign your variable on line 4, then call your method on line 5!“

15、pi = 3.14 print str(pi) 22 主要介绍“.” 的用处,比如上面的四个 String 的四个方法都是用到了点2 练习: 利用“.”来使用 String 的变量 ministry 的函数 len()和 upper(),并打印出python ministry = “The Ministry of Silly Walks“ print len(ministry) 火 龙 果 整 理uml.orgcnprint ministry.upper() 23 介绍 print 的作用2 练习:利用 print 输出字符串“Monty Python“python “Tell Python

16、to print “Monty Python“ to the console on line 4!“ print “Monty Python“ 1 介绍 print 来打印出一个变量2 练习:把变量 the_machine_goes 值赋值“Ping!“,然后打印出python “Assign the string “Ping!“ to the variable the_machine_goes on line 5, then print it out on line 6!“ the_machine_goes = “Ping!“ print the_machine_goes 24 介绍我们可以

17、使用+ 来连接两个 String2 练习:利用+把三个字符串“Spam “和“and “和“eggs“连接起来输出火 龙 果 整 理uml.orgcnpython # Print the concatenation of “Spam and eggs“ on line 3! print “Spam “ + “and “ + “eggs“ 25 介绍了 str()的作用是把一个数字转化为字符串2 练习:利用 str()函数把 3.14 转化为字符串并输出python # Turn 3.14 into a string on line 3! print “The value of pi is ar

18、ound “ + str(3.14) 第十四节1 介绍了字符串的格式化,使用%来格式化,字符串是%s2 举例:有两个字符串,利用格式化%s 来输出python string_1 = “Camelot“ string_2 = “place“ print “Lets not go to %s. Tis a silly %s.“ % (string_1, string_2) 火 龙 果 整 理uml.orgcn1 回顾之前的内容2 练习1 设置变量 my_string 的值2 打印出变量的长度3 利用 upper()函数并且打印变量值python # Write your code below, s

19、tarting on line 3! my_string = “chenguolin“ print len(my_string) print my_string.upper() Python 入门教程 4 - Date and Time26 介绍得到当前的时间 datetime.now()2 练习1 设置变量 now 的值为 datetime.now()2 打印 now 的值python from datetime import datetime now = datetime.now() print now 火 龙 果 整 理uml.orgcn27 介绍从 datetime.now 得到的信息

20、中提取出 year,month 等2 练习: 从 datetime.now 中得到的信息中提取出 year,month,daypython from datetime import datetime now = datetime.now() print now.month print now.day print now.year 28 介绍把输出日期的格式转化为 mm/dd/yyyy,我们利用的是+来转化2 练习:打印当前的日期的格式为 mm/dd/yyyypython from datetime import datetime now = datetime.now() print str(n

21、ow.month)+“/“+str(now.day)+“/“+str(now.year) 29 介绍把输出的时间格式化为 hh:mm:ss2 练习:打印当前的时间的格式为 hh:mm:ss火 龙 果 整 理uml.orgcnpython from datetime import datetime now = datetime.now() print str(now.hour)+“:“+str(now.minute)+“:“+str(now.second) 第五节1 练习:把日期和时间两个连接起来输出python from datetime import datetime now = datet

22、ime.now() print str(now.month) + “/“ + str(now.day) + “/“ + str(now.year) + “ “ +str(now.hour) + “:“ + str(now.minute) + “:“ + str(now.second) Python 入门教程 5 - Conditionals & Control Flow30 介绍 Python 利用有 6 种比较的方式 = , != , , = , = -185 把 bool_five 的值设置为 99 != 98+1python #Assign True or False as approp

23、riate on the lines below! bool_one = 17 = -18 bool_five = 99 != 98+1 31 介绍了比较的两边不只是数值,也可以是两个表达式2 练习1 把 bool_one 的值设置为 20 + -10*2 10%3%22 把 bool_two 的值设置为 (10+17)*2 = 3*63 把 bool_two 的值设置为 1*2*3 = -4*25 把 bool_five 的值设置为 100*0.5 != 6+4python 火 龙 果 整 理uml.orgcn# Assign True or False as appropriate on

24、the lines below! bool_one = 20+-10*2 10%3%2 bool_two = (10+17)*2 = 3*6 bool_three = 1*2*3 = -4*2 bool_five = 100*0.5 != 6+4 32 介绍了 Python 里面还有一种数据类型是 booleans,值为 True 或者是 False2 练习:根据题目的意思来设置右边的表达式python # Create comparative statements as appropriate on the lines below! # Make me true! bool_one = 1

25、2 # Make me true! bool_three = 1 != 2 # Make me false! 火 龙 果 整 理uml.orgcnbool_four = 2 2 # Make me true! bool_five = 4 = 16*0.53 设置变量 bool_three 的值为 19%4 != 300/10/10 and False4 设置变量 bool_four 的值为-(1*2) = 16*0.5 bool_three = 19%4 != 300/10/10 and False bool_four = -(1*2) = 50 or False4 设置变量 bool_fou

26、r 的值为 True or True5 设置变量 bool_five 的值为 1*100 = 100*1 or 3*2*1 != 3+2+1python bool_one = 2*3 = 108%100 or Cleese = King Arthur bool_two = True or False bool_three = 100*0.5 = 50 or False bool_four = True or True bool_five = 1*100 = 100*1 or 3*2*1 != 3+2+1 35 介绍第三种连接符 not , 如果是 not True 那么结果为 False,no

27、t False 结果为 True2 练习1 设置变量 bool_one 的值为 not True火 龙 果 整 理uml.orgcn2 设置变量 bool_two 的值为 not 3*4 9: print “I dont get printed.“ else: 火 龙 果 整 理uml.orgcnprint “I also dont get printed!“ 4 练习:设置变量 response 的值为Ypython response = Y answer = “Left“ if answer = “Left“: print “This is the Verbal Abuse Room, y

28、ou heap of parrot droppings!“ # Will the above print statement print to the console? # Set response to Y if you think so, and N if you think not. 第十节1 介绍了 if 的格式python if EXPRESSION: # block line one # block line two # et cetera 2 练习:在两个函数里面加入两个加入条件语句,能够成功输出火 龙 果 整 理uml.orgcnpython def using_control

29、_once(): if 1 0: return “Success #1“ def using_control_again(): if 1 0: return “Success #2“ print using_control_once() print using_control_again() 39 介绍了 else 这个条件语句2 练习:完成函数里面 else 条件语句python answer = “Tis but a scratch!“ def black_knight(): if answer = “Tis but a scratch!“: return True else: retur

30、n False # Make sure this returns False 火 龙 果 整 理uml.orgcndef french_soldier(): if answer = “Go away, or I shall taunt you a second time!“: return True else: return False # Make sure this returns False 40 介绍了另外一种条件语句 elif 的使用2 练习:在函数里面第二行补上 answer 5, 第四行补上 answer 5: return 1 elif answer 2 or 1 = 2: r

31、eturn True else: return False Python 入门教程 6 - PygLatin火 龙 果 整 理uml.orgcn1 练习:使用 Python 来输出这句话“Welcome to the English to Pig Latin translator!“python print “Welcome to the English to Pig Latin translator!“ 41 介绍了 Python 的输入,Python 里面我们可以通过 raw_input 来实现出入2 比如我们使用 name = raw_ijnput(“whats your name“)

32、, 那么这里将会在whats your name 提示我们输入,并把结果保存到 name 里面3 练习:使用 original 变量来接受任何的输入python print “Welcome to the English to Pig Latin translator!“ original = raw_input(“welcome to the Python:“) 42 介绍了我们在输入的时候经常出现输入空字符串的情况,因此我们需要去检查是否是空字符串2 练习:在上一节的输入的值进行判断,如果不是空字符串那么打印这个值,否则直接输出“empty“python print “Welcome to

33、 the English to Pig Latin translator!“ original = raw_input(“welcome to the Python:“) if len(original) 0: print original else: 火 龙 果 整 理uml.orgcnprint “empty“ 43 介绍了怎样判断一个字符串是数字的方法,我们可以通过 isalpha()来判断如果是阿拉伯数字,则 isalpha 的值为 false ,否则为 TRUE2 比如有一个变量为 x = “123“,那么 x.isalpha()是 True3 练习:通过变量 original 的输

34、入值来判断是否是一个数字串python print “Welcome to the English to Pig Latin translator!“ original = raw_input(“welcome to the Python:“) if original.isalpha(): print “True“ else: print “False“ 第五节1 练习:利用多次的输入来判断是否是数字串和非空字符串python print “Welcome to the English to Pig Latin translator!“ original = raw_input(“welcom

35、e to the Python:“) if original.isalpha(): print “True“ 火 龙 果 整 理uml.orgcnelse: print “False“ original = raw_input(“welcome to the Python:“) if len(y) = 0: print “empty“ else: print “no empty“ 第六节1 回顾了一下之前的 String 的 lower()函数2 练习1 设置变量 word 等于 original.lower()2 设置变量 first 等于 word 的第一个字符python pyg = ay original = raw_input(Enter a word:) word = original.lower() first = word0 if len(original) 0 and original.isalpha(): print original

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 专业基础教材

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报