Python 入门教程1 ---- Python Syntax1 Python是一个高效的语言,读和写的操作都是很简单的,就像普通的英语一样2 Python是一个解释执行的语言,我们不需要去编译,我们只要写出代码即可运行3 Python是一个面向对象的语言,在Python里面一切皆对象4 Python是一门很有趣的语言5 变量:一个变量就是一个单词,只有一个单一的值练习:设置一个变量my_variable,值设置为10[cpp]#Write your code below!my_variable = 103 第三节1 Python里面有三种数据类型 interage , floats , booleans2 Python是一个区分大小写的语言3 练习1 把变量my_int 值设置为72 把变量my_float值设置为1.233 把变量my_bool值设置为true[python]#Set the variables to the values listed in the instructions!my_int = 7my_float = 1.23my_bool = True6 Python的变量可以随时进行覆盖2 练习:my_int的值从7改为3,并打印出my_int[python]#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#Here's some code that will print my_int to the console: #The print keyword will be covered in detail soon!print my_int7 Pyhton的声明和英语很像8 Python里面声明利用空格在分开3 练习:查看以下代码的错误[python]def spam():eggs = 12return eggsprint spam()9 Python中的空格是指正确的缩进2 练习:改正上一节中的错误[python]def spam():eggs = 12return eggsprint spam()10 Python是一种解释执行的语言,只要你写完即可立即运行2 练习:设置变量spam的只为True,eggs的值为False [python]spam = Trueeggs = False11 Python的注释是通过“#”来实现的,并不影响代码的实现2 练习:给下面的代码加上一行注释[python]#this is a comments for Pythonmysterious_variable = 4212 Python的多行注释是通过“""" """ ”来实现的2 练习:把下面的代码加上多行[python]"""this is a Python course"""a = 513 Python有6种算术运算符+,-,*,/,**(幂),%2 练习:把变量count_to设置为1+2[python]#Set count_to equal to 1 plus 2 on line 3!count_to = 1+2print count_to14 Python里面求x^m,写成x**m2 练习:利用幂运算,把eggs的值设置为100 [python]#Set eggs equal to 100 using exponentiation on line 3! eggs = 10**2print eggs1 练习:1 写一行注释2 把变量monty设置为True3 把变量python值设置为1.2344 把monty_python的值设置为python的平方[python]#this is a Pythonmonty = Truepython = 1.234monty_python = python**2Python 入门教程2 ---- Tip Calculator1 把变量meal的值设置为44.50[python]#Assign the variable meal the value 44.50 on line 3!meal = 44.501 把变量tax的值设置为6.75%[python]meal = 44.50tax = 6.75/1001 设置tip的值为15%[python]#You're almost there! Assign the tip variable on line 5.meal = 44.50tax = 0.0675tip = 0.151 把变量meal的值设置为meal+meal*tax[python]#Reassign meal on line 7!meal = 44.50tax = 0.0675tip = 0.15meal = meal+meal*tax设置变量total的值为meal+meal*tax[python]#Assign the variable total on line 8!meal = 44.50tax = 0.0675tip = 0.15meal = meal + meal * taxtotal = meal + meal * tipprint("%.2f" % total)Python 入门教程3 ----Strings and Console Output15Python里面还有一种好的数据类型是String16一个String是通过'' 或者""包成的串3 设置变量brian值为"Always look on the bright side of life!" [python]#Set the variable brian on line 3!brian = "Always look on the bright side of life!"1 练习1 把变量caesar变量设置为Graham2 把变量praline变量设置为john3 把变量viking变量设置为Teresa[python]#Assign your variables below, each on its own line!caesar = "Graham"praline = "John"viking = "Teresa"#Put your variables above this lineprint caesarprint pralineprint viking17 Python是通过\来实现转义字符的2 练习把'Help! Help! I'm being repressed!' 中的I'm中的'进行转义[python]#The string below is broken. Fix it using the escape backslash!'Help! Help! \'\m being repressed!'18 我们可以使用""来避免转义字符的出现2 练习:把变量fifth_letter设置为MONTY的第五个字符[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 5So if you wanted "Y", you could just type"PYTHON"[1] (always start counting from 0!)"""fifth_letter = "MONTY"[4]print fifth_letter19 介绍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()把所有的大写字母转化为小写字母2 练习:把parrot中的小写字母转换为大写字母并打印[python]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!"""pi = 3.14print str(pi)22 主要介绍“.”的用处,比如上面的四个String的四个方法都是用到了点2 练习: 利用“.”来使用String的变量ministry的函数len()和upper(),并打印出[python]ministry = "The Ministry of Silly Walks"print len(ministry)print ministry.upper()23 介绍print的作用2 练习:利用print输出字符串"Monty Python"[python]"""Tell Python 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!" tothe variable the_machine_goes online 5, then print it out on line 6!"""the_machine_goes = "Ping!"print the_machine_goes24 介绍我们可以使用+来连接两个String2 练习:利用+把三个字符串"Spam "和"and "和"eggs"连接起来输出[python]# 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 around " + str(3.14)第十四节1 介绍了字符串的格式化,使用%来格式化,字符串是%s2 举例:有两个字符串,利用格式化%s来输出[python]string_1 = "Camelot"string_2 = "place"print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)1 回顾之前的内容2 练习1 设置变量my_string的值2 打印出变量的长度3 利用upper()函数并且打印变量值[python]# Write your code below, starting on line 3!my_string = "chenguolin"print len(my_string)print my_string.upper()Python 入门教程4 ---- Date and Time 26 介绍得到当前的时间datetime.now()2 练习1 设置变量now的值为datetime.now()2 打印now的值[python]<span style="font-size:18px">from datetime import datetimenow = datetime.now()print now</span>27 介绍从datetime.now得到的信息中提取出year,month等2 练习: 从datetime.now中得到的信息中提取出year,month,day [python]<span style="font-size:18px">from datetime import datetimenow = datetime.now()print now.monthprint now.dayprint now.year</span>28 介绍把输出日期的格式转化为mm//dd//yyyy,我们利用的是+来转化2 练习:打印当前的日期的格式为mm//dd//yyyy[python]<span style="font-size:18px">from datetime import datetimenow = datetime.now()print str(now.month)+"/"+str(now.day)+"/"+str(now.year)</span>29 介绍把输出的时间格式化为hh:mm:ss2 练习:打印当前的时间的格式为hh:mm:ss[python]<span style="font-size:18px">from datetime import datetimenow = datetime.now()print str(now.hour)+":"+str(now.minute)+":"+str(now.second)</span>第五节1 练习:把日期和时间两个连接起来输出[python]<span style="font-size:18px">from datetime import datetimenow = datetime.now()print str(now.month) + "/" + str(now.day) + "/" + str(now.year) + " "\+str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)</span>Python 入门教程 5 ----Conditionals & Control Flow30 介绍Python利用有6种比较的方式== , != , > , >= , < , <=2 比较后的结果是True或者是False3 练习1 把bool_one的值设置为 17 < 118%1002 把bool_two的值设置为 100 == 33*3 + 13 把bool_two的值设置为 19 <= 2**44 把bool_four的值设置为 -22 >= -185 把bool_five的值设置为 99 != 98+1[python]#Assign True or False as appropriate on the lines below! bool_one = 17 < 118%100bool_two = 100 == 33*3+1bool_three = 19 <= 2**4bool_four = -22 >= -18bool_five = 99 != 98+131 介绍了比较的两边不只是数值,也可以是两个表达式2 练习1 把bool_one的值设置为 20 + -10*2 > 10%3%22 把bool_two的值设置为 (10+17)**2 == 3**63 把bool_two的值设置为 1**2**3 <= -(-(-1))4 把bool_four的值设置为 40/20*4 >= -4**25 把bool_five的值设置为 100**0.5 != 6+4 [python]# Assign True or False as appropriate on the lines below! bool_one = 20+-10*2 > 10%3%2bool_two = (10+17)**2 == 3**6bool_three = 1**2**3 <= -(-(-1))bool_four = 40/20*4 >= -4**2bool_five = 100**0.5 != 6+432 介绍了Python里面还有一种数据类型是booleans,值为True或者是False2 练习:根据题目的意思来设置右边的表达式[python]# Create comparative statements as appropriate on the lines below!# Make me true!bool_one = 1 <= 2# Make me false!bool_two = 1 > 2# Make me true!bool_three = 1 != 2# Make me false!bool_four = 2 > 2# Make me true!bool_five = 4 < 533 介绍了第一种连接符and的使用,只有and的两边都是True那么结果才能为True2 练习1 设置变量bool_one的值为False and False2 设置变量bool_two的值为-(-(-(-2))) == -2 and 4 >= 16**0.53 设置变量bool_three的值为19%4 != 300/10/10 and False4 设置变量bool_four的值为-(1**2) < 2**0 and 10%10 <= 20-10*25 设置变量bool_five的值为True and True[python]bool_one = False and Falsebool_two = -(-(-(-2))) == -2 and 4 >= 16**0.5bool_three = 19%4 != 300/10/10 and Falsebool_four = -(1**2) < 2**0 and 10%10 <= 20-10*2bool_five = True and True34 介绍了第二种连接符or的使用,只要or的两边有一个True那么结果才能为True2 练习1 设置变量bool_one的值为2**3 == 108%100 or 'Cleese' == 'King Arthur'2 设置变量bool_two的值为True or False3 设置变量bool_three的值为100**0.5 >= 50 or False4 设置变量bool_four的值为True or True5 设置变量bool_five的值为1**100 == 100**1 or 3*2*1 != 3+2+1 [python]bool_one = 2**3 == 108%100 or 'Cleese' == 'King Arthur'bool_two = True or Falsebool_three = 100**0.5 >= 50 or Falsebool_four = True or Truebool_five = 1**100 == 100**1 or 3*2*1 != 3+2+135 介绍第三种连接符not , 如果是not True那么结果为False,not False结果为True2 练习1 设置变量bool_one的值为not True2 设置变量bool_two的值为not 3**4 < 4**33 设置变量bool_three的值为not 10%3 <= 10%24 设置变量bool_four的值为not 3**2+4**2 != 5**25 设置变量bool_five的值为not not False[python]bool_one = not Truebool_two = not 3**4 < 4**3bool_three = not 10%3 <= 10%2bool_four = not 3**2+4**2 != 5**2bool_five = not not False36 介绍了由于表达式很多所以我们经常使用()来把一些表达式括起来,这样比较具有可读性2 练习1 设置变量bool_one的值为False or (not True) and True2 设置变量bool_two的值为False and (not True) or True3 设置变量bool_three的值为True and not (False or False)4 设置变量bool_four的值为not (not True) or False and (not True)5 设置变量bool_five的值为False or not (True and True)[python]bool_one = False or (not True) and Truebool_two = False and (not True) or Truebool_three = True and not (False or False)bool_four = not (not True) or False and (not True)bool_five = False or not (True and True)第八节1 练习:请至少使用and,or,not来完成以下的练习[python]# Use boolean expressions as appropriate on the lines below!# Make me false!bool_one = not ((1 and 2) or 3)# Make me true!bool_two = not (not((1 and 2) or 3))# Make me false!bool_three = not ((1 and 2) or 3)# Make me true!bool_four = not (not((1 and 2) or 3))# Make me true!bool_five = not (not((1 and 2) or 3)37 介绍了条件语句if38 if的格式如下, 比如[python]if 8 < 9:print "Eight is less than nine!"3 另外还有这elif 以及else,格式如下[python]if 8 < 9:print "I get printed!"elif 8 > 9:print "I don't get printed."else:print "I also don't get printed!"4 练习:设置变量response的值为'Y'[python]response = 'Y'answer = "Left"if answer == "Left":print "This is the Verbal Abuse Room, you 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 cetera2 练习:在两个函数里面加入两个加入条件语句,能够成功输出[python]def using_control_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 Trueelse:return False # Make sure this returns Falsedef french_soldier():if answer == "Go away, or I shall taunt you a second time!":return Trueelse:return False # Make sure this returns False40 介绍了另外一种条件语句elif的使用2 练习:在函数里面第二行补上answer > 5,第四行补上answer < 5 , 从而完成这个函数[python]def greater_less_equal_5(answer):if answer > 5:return 1elif answer < 5:return -1else:return 0print greater_less_equal_5(4)print greater_less_equal_5(5)print greater_less_equal_5(6)第十三节1 练习:利用之前学的比较以及连接符以及条件语句补全函数。