




已阅读5页,还剩65页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Python 入门教程 1 - Python Syntax1 Python是一个高效的语言,读和写的操作都是很简单的,就像普通的英语一样2 Python是一个解释执行的语言,我们不需要去编译,我们只要写出代码即可运行3 Python是一个面向对象的语言,在Python里面一切皆对象4 Python是一门很有趣的语言5 变量:一个变量就是一个单词,只有一个单一的值练习:设置一个变量my_variable,值设置为10cpp #Write your code below! my_variable = 10 3 第三节 1 Python里面有三种数据类型 interage , floats , booleans 2 Python是一个区分大小写的语言 3 练习 1 把变量my_int 值设置为7 2 把变量my_float值设置为1.23 3 把变量my_bool值设置为truepython #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,并打印出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 #Heres some code that will print my_int to the console: #The print keyword will be covered in detail soon! print my_int 7 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的值为Falsepythonspam = 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#Set count_to equal to 1 plus 2 on line 3! count_to = 1+2 print count_to 14 Python里面求xm,写成x*m 2 练习:利用幂运算,把eggs的值设置为100python #Set eggs equal to 100 using exponentiation on line 3! eggs = 10*2 print eggs 1 练习: 1 写一行注释 2 把变量monty设置为True 3 把变量python值设置为1.234 4 把monty_python的值设置为python的平方python #this is a Python monty = True python = 1.234 monty_python = python*2 Python 入门教程 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的值为15% python #Youre almost there! Assign the tip variable on line 5. meal = 44.50 tax = 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 total on line 8! meal = 44.50 tax = 0.0675 tip = 0.15 meal = meal + meal * tax total = meal + meal * tip print(%.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变量设置为Graham 2 把变量praline变量设置为john 3 把变量viking变量设置为Teresapython #Assign your variables below, each on its own line! caesar = Graham praline = John viking = Teresa #Put your variables above this line print caesar print praline print 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的第五个字符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 typePYTHON1 (always start counting from 0!) fifth_letter = MONTY4 print fifth_letter 19 介绍String的第一种方法,len()求字符串的长度 2 练习: 把变量parrot的值设置为Norweigian Blue,然后打印parrot的长度pythonparrot = 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.14 print 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 Pythonpython Tell Python to print Monty Pythonto 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_goes 24 介绍我们可以使用+来连接两个String 2 练习:利用+把三个字符串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 介绍了字符串的格式化,使用%来格式化,字符串是%s 2 举例:有两个字符串,利用格式化%s来输出python string_1 = Camelot string_2 = place print Lets 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 Time26 介绍得到当前的时间datetime.now() 2 练习 1 设置变量now的值为datetime.now() 2 打印now的值python from datetime import datetime now = datetime.now() print now 27 介绍从datetime.now得到的信息中提取出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(now.month)+/+str(now.day)+/+str(now.year) 29 介绍把输出的时间格式化为hh:mm:ss 2 练习:打印当前的时间的格式为hh:mm:sspython from datetime import datetime now = datetime.now() print str(now.hour)+:+str(now.minute)+:+str(now.second) 第五节 1 练习:把日期和时间两个连接起来输出python from datetime import datetime now = datetime.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种比较的方式 = , != , , = , , = 2 比较后的结果是True或者是False 3 练习 1 把bool_one的值设置为 17 118%100 2 把bool_two的值设置为 100 = 33*3 + 1 3 把bool_two的值设置为 19 = -18 5 把bool_five的值设置为 99 != 98+1python#Assign True or False as appropriate on the lines below! bool_one = 17 118%100 bool_two = 100 = 33*3+1 bool_three = 19 = -18 bool_five = 99 != 98+1 31 介绍了比较的两边不只是数值,也可以是两个表达式 2 练习 1 把bool_one的值设置为 20 + -10*2 10%3%2 2 把bool_two的值设置为 (10+17)*2 = 3*6 3 把bool_two的值设置为 1*2*3 = -4*2 5 把bool_five的值设置为 100*0.5 != 6+4python# Assign True or False as appropriate on 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或者是False 2 练习:根据题目的意思来设置右边的表达式python # Create comparative statements as appropriate on the lines below! # Make me true! bool_one = 1 2 # Make me true! bool_three = 1 != 2 # Make me false! bool_four = 2 2 # Make me true! bool_five = 4 = 16*0.5 3 设置变量bool_three的值为19%4 != 300/10/10 and False 4 设置变量bool_four的值为-(1*2) 2*0 and 10%10 = 16*0.5 bool_three = 19%4 != 300/10/10 and False bool_four = -(1*2) 2*0 and 10%10 = 50 or False 4 设置变量bool_four的值为True or True 5 设置变量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,not False结果为True 2 练习 1 设置变量bool_one的值为not True 2 设置变量bool_two的值为not 3*4 4*3 3 设置变量bool_three的值为not 10%3 = 10%2 4 设置变量bool_four的值为not 3*2+4*2 != 5*2 5 设置变量bool_five的值为not not Falsepython bool_one = not True bool_two = not 3*4 4*3 bool_three = not 10%3 = 10%2 bool_four = not 3*2+4*2 != 5*2 bool_five = not not False 36 介绍了由于表达式很多所以我们经常使用()来把一些表达式括起来,这样比较具有可读性 2 练习 1 设置变量bool_one的值为False or (not True) and True 2 设置变量bool_two的值为False and (not True) or True 3 设置变量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)pythonbool_one = False or (not True) and True bool_two = False and (not True) or True bool_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 dont get printed. else: print I also dont get printed! 4 练习:设置变量response的值为Ypython 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 cetera 2 练习:在两个函数里面加入两个加入条件语句,能够成功输出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 True else: return False # Make sure this returns False def 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 5: return -1 else: return 0 print greater_less_equal_5(4) print greater_less_equal_5(5) print greater_less_equal_5(6) 第十三节 1 练习:利用之前学的比较以及连接符以及条件语句补全函数。所有的都要出现至少一次python def the_flying_circus(): # Start coding here! if 1 2 and not False: return True elif 1 = 1 or 1 != 2 or 1 2 or 1 2 or 1 = 2: return True else: return False Python 入门教程 6 - PygLatin1 练习:使用Python来输出这句话Welcome to the English to Pig Latin translator!pythonprint Welcome to the English to Pig Latin translator! 41 介绍了Python的输入,Python里面我们可以通过raw_input来实现出入 2 比如我们使用name = raw_ijnput(whats your name) , 那么这里将会在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 练习:在上一节的输入的值进行判断,如果不是空字符串那么打印这个值,否则直接输出emptypython print Welcome to the English to Pig Latin translator! original = raw_input(welcome to the Python:) if len(original) 0: print original else: print empty 43 介绍了怎样判断一个字符串是数字的方法,我们可以通过isalpha()来判断如果是阿拉伯数字,则isalpha的值为false ,否则为TRUE 2 比如有一个变量为x = 123,那么x.isalpha()是True 3 练习:通过变量original的输入值来判断是否是一个数字串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 练习:利用多次的输入来判断是否是数字串和非空字符串pythonprint Welcome to the English to Pig Latin translator! original = raw_input(welcome to the Python:) if original.isalpha(): print True else: 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 else: print empty 第六节 1 介绍了if语句里面还可以嵌套语句 2 练习:判断上一节里面的first字符是否是元音字符是的话输出vowel,否则输出consonantpython pyg = ay original = raw_input(Enter a word:) word = original.lower() first = word0 if len(original) 0 and original.isalpha(): if first = a or first = i or first = o or first = u or first = e: print vowel else: print consonant else: print empty 第七节 1 利用String的+操作,产生一个新的变量new_word等于word+pyg 2 练习:把print vowel替换成print new_wordpython pyg = ay original = raw_input(Enter a word:) word = original.lower() first = word0 new_word = word+pyg if len(original) 0 and original.isalpha(): if first = a or first = i or first = o or first = u or first = e: print new_word else: print consonant else: print empty 44 介绍了String中得到子串的方法,比如我们有一个字符串s = foo,现在s0:2就是fo45 如果结束是末尾,那么可以直接写成这样si:,省略第二个数 2 练习:在嵌套的if语句里面设置new_word的值为word从第1位到最后一位+变量pygpython pyg = ay original = raw_input(Enter a word:) word = original.lower() first = word0 new_word = word+pyg if len(original) 0 and original.isalpha(): if first = a or first = i or first = o or first = u or first = e: new_word = word1:+pyg print new_word else: print consonant else: print empty Python 入门教程 7 - PygLatin46 介绍了Python的函数组成有三部份,函数头,函数体 2 函数的举例pythondef ni_sayer(): Prints Ni! to the console. print Ni! 3 练习:写一个函数,输出字符串Eggs!,函数体增加一行注释python # Define your spam function starting on line 5. You # can leave the code on line 11 alone for now-well # explain it soon! def spam(): this is a zhushi print Eggs! # Define the spam function above this line. spam() 47 介绍了函数的调用,就是直接函数名 2 练习:调用函数spam(10)python def square(n): Returns the square of a number. squared = n*2 print %d squared is %d. % (n, squared) return squared # Call the square function on line 9! Make sure to # include the number 10 between the parentheses. square(10) 48 介绍了函数可以使用参数的传递 2 比如函数no_one(sentence)的使用,传入字符串作为参数python def no_one(sentence): print sentence no_one(The Spanish Inquisition) 3 练习:把函数的参数改为base,exponent。调用函数传入37和4python view plaincopydef power(base,exponent): # Add your parameters here!
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湛江市高三测试题(一)文综地理试卷
- 2025版权转让协议合同范本
- 2025快递加盟合同模板
- 2025年智能家居系统合同范例
- 2025办公用品采购合同书
- 2025市场营销承包合同
- 2025环保设备工程承包合同
- 江苏省盐城市东台市第五教育联盟2024-2025学年七年级下学期期中地理试题(含答案)
- 河南省开封市金明中学2024-2025学年八年级下学期期中考试数学试题(含部分答案)
- 二手解困房买卖合同
- GB/T 6451-2023油浸式电力变压器技术参数和要求
- 幼儿园中班绘本《城市里最漂亮的巨人》课件
- 医院廉洁行医廉政教育专题课件
- 医务人员职业健康安全健康-课件
- 安全组织机构图
- 旧石器时代考古-基础知识课件
- 江苏省建设工程现场安全文明施工措施费计价管理办法
- 病区药品规范化管理与问题对策黄池桃
- 螺纹塞规操作规程
- 2023年北京天文馆招聘笔试备考题库及答案解析
- 应急救援队伍单兵体能训练项目要求
评论
0/150
提交评论