《Python语言程序设计》课件-第一章(中英文课件)_第1页
《Python语言程序设计》课件-第一章(中英文课件)_第2页
《Python语言程序设计》课件-第一章(中英文课件)_第3页
《Python语言程序设计》课件-第一章(中英文课件)_第4页
《Python语言程序设计》课件-第一章(中英文课件)_第5页
已阅读5页,还剩70页未读 继续免费阅读

下载本文档

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

文档简介

Python语言程序设计【将数值转换成字符串】PythonLanguageProgramming[convertingnumericalvaluesintostrings]知识点【将数值转换成字符串】(Python2的用法)利用一对反撇(反撇和单引号不一样)【例】Python2用法a=123b=456c=a+bd='a与b作为字符连接:'+`a`+`b`print(c)print(d)【例】Python2和3共同用法(str())a=123b=456c=a+bd='a与b作为字符连接:'+str(a)+str(b)print(c)print(d)#579#a与b作为字符连接:123456#a与b作为字符连接:123456[Convertanumericvalueintoastring](UsageofPython2)Useapairofbackticks(backticksaredifferentfromsinglequotes)[Example]UsageofPython2a=123b=456c=a+bD='aandbareconnectedascharacters:'+`a`+`b`print(c)print(d)[Example]CommonusageofPython2and3(str())a=123b=456c=a+bD='aandbareconnectedascharacters:'+str(a)+str(b)print(c)print(d)#579#Aandbareconnectedascharacters:123456#Aandbareconnectedascharacters:123456KnowledgePointsPython语言程序设计【字符串的连接与倍增】PythonLanguageProgramming[Stringconcatenationandmultiplication]知识点【字符串的连接与倍增】字符串连接方式:直接用“+”来连接两个字符串,str1+str2【例】字符串连接方式1print(‘Deris’+‘Weng’)#

DerisWeng两个字符串用“逗号”隔开,但字符串之间会多出一个空格【例】字符串连接方式2print(‘Deris’,‘Weng’)#

DerisWeng[Stringconcatenationandmultiplication]Stringconnectionmethod:Connecttwostringsdirectlywith"+",str1+str2[Example]Stringconnectionmode1print(‘Deris’+‘Weng’)#

DerisWengThetwostringsareseparatedbycommas,butthereisanextraspacebetweenthestrings.[Example]Stringconnectionmode2print(‘Deris’,‘Weng’)#

DerisWengKnowledgePoints知识点【字符串的连接与倍增】字符串连接方式:两个字符串放在一起,中间有空格或者没有空格【例】字符串连接方式3print(‘Deris’‘Weng’)#

DerisWengprint(‘Deris’‘Weng’)#

DerisWeng用符号“%”连接一个字符串和一组变量【例】字符串连接方式4print('%s,%s'%('Deris','Weng'))#

Deris,WengStringconnectionmethod:Twostringsareputtogetherwithorwithoutspacesinthemiddle.[Example]StringConnectionMode3print(‘Deris’‘Weng’)#

DerisWengprint(‘Deris’‘Weng’)#

DerisWengConnectastringandasetofvariableswiththesymbol"%"[Example]StringConnectionMode4print('%s,%s'%('Deris','Weng'))#

Deris,Weng[Stringconcatenationandmultiplication]KnowledgePoints知识点【字符串的连接与倍增】字符串连接方式:利用字符串的函数join【例】字符串连接方式5var_list=['Deris','Weng','Female']a='***'print(a.join(var_list))#Deris***Weng***FemaleStringconnectionmethod:Usestringfunctionjoin[Example]Stringconnectionmode5var_list=['Deris','Weng','Female']a='***'print(a.join(var_list))#Deris***Weng***Female[Stringconcatenationandmultiplication]KnowledgePoints知识点【字符串的连接与倍增】字符串连接方式:字符串乘法,即字符串的倍增【例】字符串连接方式6a='Weng'print(a*3)#WengWengWengStringconnectionmethod:Stringmultiplication,thatis,themultiplicationofstrings[Example]StringConnectionMode6a='Weng'print(a*3)#WengWengWeng[Stringconcatenationandmultiplication]KnowledgePoints练习【字符串的连接与倍增】问题:

1.若定义a=‘A’,执行print(a*10)后,结果为

。2.若定义a=‘A’,b=‘B’,执行print(a+b)后,结果为

。3.若定义a=‘A’,b=‘B’,执行print(a,b)后,结果为

。4.若定义a=‘A’,b=‘B’,执行print(ab)后,结果为

。5.若定义a=‘A’,b=‘B’,执行print(ab)后,结果为

。practise[Stringconcatenationandmultiplication]Question:1.Ifa='A'isdefined,theresultisafterexecutingprint(a*10)

.2.Ifa='A'andb='B'aredefined,theresultisafterexecutingprint(a+b)

.3.Ifa='A'andb='B'aredefined,theresultisafterexecutingprint(a,b)

.4.Ifa='A'andb='B'aredefined,theresultisafterexecutingprint(ab)

.5.Ifa='A'andb='B'aredefined,theresultisafterexecutingprint(ab)

.Python语言程序设计【值与类型】PythonLanguageProgramming[ValueandType]知识点【值与类型】1.数字型:将数字分为:整数和带小数点的数。Python3支持int、float、complex(复数)。intfloatcomplex90.0123.45j999.991234.j-99-999.90.12e-34j0x123AB123EF99.9+e9912e+345jKnowledgePoints[ValueandType]1.Numericaltype:Dividenumbersintointegersandnumberswithdecimals.Python3supportint、float、complex.intfloatcomplex90.0123.45j999.991234.j-99-999.90.12e-34j0x123AB123EF99.9+e9912e+345j知识点【值与类型】观察数据的类型Python还提供了一个“内置函数”:type()用来观察数据的类型:若有定义a=3,执行print(type(a))

之后,结果是:int。KnowledgePoints[ValueandType]ObservethetypeofdataPythonalsoprovidesa'built-infunction':Type()isusedtoobservethetypeofdata:Ifa=3isdefined,afterexecutingprint(type(a)),theresultis:int.知识点【值与类型】2.字符串【例】print(“这是一个‘单、双引号混合使用’的字符串示例”)【例】转义字符\的使用print('I\'m\"OK\"!')作用:如果字符串内部既包含单引号'又包含双引号",可以用转义字符\来标识。KnowledgePoints[ValueandType]2.characterstring[Example]print("Thisisanexampleofastringthatusesacombinationofsingleanddoublequotationmarks")[Example]Useofescapecharacter“\”print('I\'m\"OK\"!')Function:Ifthestringcontainsbothsingleanddoublequotationmarksinside,theescapecharacter“\”canbeusedtoidentifyit.知识点【值与类型】3.布尔值(True、False)1234print(True)print(False)print(3>2)print(3>5)TrueFalseTrueFalse输出结果:1234print(TrueandTrue))print(Trueand

False)print(FalseandFalse)print(5>3and3>1)TrueFalseFalseTrue输出结果:KnowledgePoints[ValueandType]3.Booleanvalues(True,False)1234print(True)print(False)print(3>2)print(3>5)TrueFalseTrueFalseOutputresult:1234print(TrueandTrue))print(Trueand

False)print(FalseandFalse)print(5>3and3>1)TrueFalseFalseTrueOutputresult:知识点【值与类型】3.布尔值(True、False)1234print(Trueor

True)print(TrueorFalse)print(FalseorFalse)print(5>3or1>3)TrueTrueFalseTrue输出结果:123print(notTrue)print(notFalse)print(not1>2)FalseTrueTrue输出结果:KnowledgePoints[ValueandType]3.Booleanvalues(True,False)1234print(Trueor

True)print(TrueorFalse)print(FalseorFalse)print(5>3or1>3)TrueTrueFalseTrueOutputresult:123print(notTrue)print(notFalse)print(not1>2)FalseTrueTrueOutputresult:【例】布尔在条件判断中使用知识点【值与类型】3.布尔值1234ifage>=18:print('成年')else:print('未成年')KnowledgePoints[ValueandType]3.Booleanvalues[Example]Booleanisusedinconditionaljudgment1234ifage>=18:print('Adult')else:print('Minor')知识点【值与类型】空值是Python里一个特殊的值,用None表示。None不能理解为0,因为0是有意义的,而None是一个特殊的空值。4.空值Python还支持其他常用的数据类型,如:List(列表)、Tuple(元组)、Sets(集合)、Dictionary(字典)。5.其他数据类型KnowledgePoints[ValueandType]AnullvalueisaspecialvalueinPython,representedbyNone.Nonecannotbeunderstoodas0because0ismeaningful,whileNoneisaspecialnullvalue.Pythonalsosupportsothercommonlyuseddatatypes,suchasList,Tuple,Sets,Dictionary.5.Otherdatatypes4.Emptyvalue课后练习【值与类型】问题:

1.Python3的数字类型分为

等子类型。2.Python不支持的数据类型有(

)A.char B.int C.float D.listintcomplexfloatAAfter-schoolexercises[ValueandType]Question:

1.ThenumbertypesinPython3aredividedintosubtypessuchas

,

,

etc。2.ThedatatypesthatPythondoesnotsupportis(

)A.char B.int C.float D.listintcomplexfloatAPython语言程序设计【输入与输出】PythonLanguageProgramming[Inputandoutput]知识点【输入】变量=input(“提示信息”)从键盘读取字符串是从用户处获取信息的一种最基本方式。123print('你叫什么名字')

name=input("我的名字是:")

print("你好!"+name.capitalize())【例】input()输入[Input]Variable=input("prompt")Readingstringsfromthekeyboardisthemostbasicwaytoobtaininformationfromtheuser.123Print('What'syourname')

Name=input("Mynameis:")

Print("Hello!"+name.capitalize())[Example]input()inputKnowledgePoints知识点【输入】【例】输入数值型的数据1234print("请输入你的幸运数字")

number=input("我的幸运数字是:")

numberNew=int(number)+10

print("祝你好运!"+str(numberNew))[Input][Example]Inputnumericdata1234Print("Pleaseenteryourluckynumber")

Number=input("Myluckynumberis:")

numberNew=int(number)+10

Print("Goodluck!"+str(numberNew))KnowledgePoints知识点【输出】print(输出一个字符串)在python中打印内容到命令行(或者叫终端、控制台)里面123print‘你要打印的东西’#或者print(‘你要打印的东西’)【例】Python2输出12print‘你要打印的东西’print(‘你要打印的东西’)【例】Python3输出[Output]Print(outputastring)Printcontenttothecommandline(orterminal,console)inPython123Print'Whatyouwanttoprint'#OrPrint('Whatyouwanttoprint')[Example]Python2output12Print'Whatyouwanttoprint'Print('Whatyouwanttoprint')[Example]Python3outputKnowledgePoints实用技巧【capitalize()、strip()】【例】capitalize()print("derisweng".capitalize())【例】strip()print("derisweng".strip())作用:使字符串中的第一个字符为大写,而其他字符为小写。作用:去掉开头和末尾的空白字符。Practicaltips【capitalize()、strip()】[Example]capitalize()print("derisweng".capitalize())[Example]strip()print("derisweng".strip())Function:Makethefirstcharacterinthestringuppercaseandtheothercharacterslowercase.Function:Removetheblankcharactersatthebeginningandattheend.实用技巧【dir()】【例】dir('')print(dir(''))作用:查看字符串包含哪些函数。['__add__','__class__','__contains__','__delattr__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__getnewargs__','__getslice__','__gt__','__hash__','__init__','__le__','__len__','__lt__','__mod__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__rmod__','__rmul__','__setattr__','__sizeof__','__str__','__subclasshook__','_formatter_field_name_split','_formatter_parser','capitalize','center','count','decode','encode','endswith','expandtabs','find','format','index','isalnum','isalpha','isdigit','islower','isspace','istitle','isupper','join','ljust','lower','lstrip','partition','replace','rfind','rindex','rjust','rpartition','rsplit','rstrip','split','splitlines','startswith','strip','swapcase','title','translate','upper','zfill']Practicaltips【dir()】[Example]dir('')print(dir(''))Function:Viewwhichfunctionsastringcontains['__add__','__class__','__contains__','__delattr__','__doc__','__eq__','__format__','__ge__','__getattribute__','__getitem__','__getnewargs__','__getslice__','__gt__','__hash__','__init__','__le__','__len__','__lt__','__mod__','__mul__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__rmod__','__rmul__','__setattr__','__sizeof__','__str__','__subclasshook__','_formatter_field_name_split','_formatter_parser','capitalize','center','count','decode','encode','endswith','expandtabs','find','format','index','isalnum','isalpha','isdigit','islower','isspace','istitle','isupper','join','ljust','lower','lstrip','partition','replace','rfind','rindex','rjust','rpartition','rsplit','rstrip','split','splitlines','startswith','strip','swapcase','title','translate','upper','zfill']实用技巧【print技巧】【例】默认用空格分隔print('Hello','deris','weng')#

Helloderisweng【例】用字符串分隔print('Hello','deris','weng',sep=',')#Hello,deris,wengPracticaltips[PrintTechniques][Example]Defaultseparatedbyspacesprint('Hello','deris','weng')#

Helloderisweng[Example]Separatedbystringsprint('Hello','deris','weng',sep=',')#Hello,deris,weng实用技巧【print技巧】【例】在同一行打印123print('deris',end='')#通过指定结束字符为空字符串print('weng')#deriswengPracticaltips[PrintTechniques][Example]Printingonthesameline123Print('deris',end='')#Emptystringbyspecifyingtheendingcharacterprint('weng')#derisweng课后练习【输入与输出】问题:

1.若定义a=10,执行print(type(a))后,结果为

。2.若定义a=10.0,执行print(type(a))后,结果为

。3.三种基本的程序设计结构为

。4.圆的面积公式s=pi*r2,写成Python语言表达式为

。5.若定义a=97,执行print('a'+'8'+'3')后,结果为

。6.若定义a=97,执行print(a+'8'+'3')后,结果为

。7.若定义a=97,执行print(str(a)+'8'+'3')后,结果为

。8.若定义a=‘D’,执行print(a*10)后,结果为

。[Inputsandoutputs]Question:1.Ifa=10isdefined,afterexecutingprint(type(a)),theresultis

.2.Ifa=10.0isdefined,theresultisafterexecutingprint(type(a))

.3.Thethreebasicprogrammingstructuresare

,

,and

.4.Theareaformulaofcircles=pi*r2,writteninPythonlanguage,is

.5.Ifa=97isdefined,afterexecutingprint('a'+'8'+'3'),theresultis

.6.Ifa=97isdefined,afterexecutingprint(a+'8'+'3'),theresultis

.7.Ifa=97isdefined,afterexecutingprint(str(a)+'8'+'3'),theresultis

.8.Ifa='D'isdefined,theresultisafterexecutingprint(a*10)

.After-schoolexercisesPython语言程序设计【注释】PythonLanguageProgramming[Commentary]知识点【注释】单行注释-#开头形式单行注释-行末#形式多行注释-每行#开头[Commentary]Singlelinecomments-#beginningformSinglelinecomments-endofline#formMultilinecomments-eachlinestartswith#KnowledgePoints知识点【注释】!介绍一种特殊的多行注释多行注释-三重引号形式用“#”、“三重引号”表示注释,所有的注释是不执行的。注释总结[Commentary]!IntroducingaspecialtypeofmultilineannotationMultilinecomment-triplequotationmarkformUse"#","triplequotes"toindicatecomments,allcommentsarenotexecuted.AnnotatedsummaryKnowledgePoints课后练习【注释】提问考一考问题1:注释的方法是?问题2:注释里面的内容是不执行的,对or错?注释的方法:(1)#(2)

三重引号[Commentary]QUESTIONTakeatestQuestion1:Themethodofannotationis?Question2:Thecontentinthecommentisnotexecuted,rightorwrong?Methodsofcommenting:(1)#(2)triplequotesAfter-schoolexercisesPython语言程序设计【语句

温馨提示

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

评论

0/150

提交评论