您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > 干货:Python3-入门教程-简单但比较不错
Python已经是3.1版本了,与时俱进更新教程.2.5或2.6请看上一篇文章。本文适合有Java编程经验的程序员快速熟悉Python本文程序在windowsxp+python3.1a1测试通过.本文提到的idle指pythonshell,即安装python后你在菜单看到的IDLE(pythongui)在idle里ctrl+n可以打开一个新窗口,输入源码后ctrl+s可以保存,f5运行程序.凡打开新窗口即指ctrl+n的操作.1你好..............................................................................................................................................12字符串和数字..............................................................................................................................23列表..............................................................................................................................................34字典..............................................................................................................................................45字符串..........................................................................................................................................56条件和循环语句..........................................................................................................................67函数..............................................................................................................................................78异常处理......................................................................................................................................89文件处理......................................................................................................................................810类和继承....................................................................................................................................911包机制........................................................................................................................................912内建帮助手册..........................................................................................................................111你好复制代码代码如下:#打开新窗口,输入:#!/usr/bin/python#-*-coding:utf8-*-s1=input(Inputyourname:)print(你好,%s%s1)'''知识点:*input(某字符串)函数:显示某字符串,并等待用户输入.*print()函数:如何打印.*如何应用中文*如何用多行注释'''2字符串和数字但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下:复制代码代码如下:#!/usr/bin/pythona=2b=testc=a+b运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换复制代码代码如下:#!/usr/bin/python#运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换a=2b=testc=str(a)+bd=1111e=a+int(d)#Howtoprintmultiplyvaluesprint(cis%s,eis%i%(c,e))'''知识点:*用int和str函数将字符串和数字进行转换*打印以#开头,而不是习惯的//*打印多个参数的方式'''3列表复制代码代码如下:#!/usr/bin/python#-*-coding:utf8-*-#列表类似Javascript的数组,方便易用#定义元组word=['a','b','c','d','e','f','g']#如何通过索引访问元组里的元素a=word[2]print(ais:+a)b=word[1:3]print(bis:)print(b)#index1and2elementsofword.c=word[:2]print(cis:)print(c)#index0and1elementsofword.d=word[0:]print(dis:)print(d)#Allelementsofword.#元组可以合并e=word[:2]+word[2:]print(eis:)print(e)#Allelementsofword.f=word[-1]print(fis:)print(f)#Thelastelementsofword.g=word[-4:-2]print(gis:)print(g)#index3and4elementsofword.h=word[-2:]print(his:)print(h)#Thelasttwoelements.i=word[:-2]print(iis:)print(i)#Everythingexceptthelasttwocharactersl=len(word)print(Lengthofwordis:+str(l))print(Addsnewelement)word.append('h')print(word)#删除元素delword[0]print(word)delword[1:3]print(word)'''知识点:*列表长度是动态的,可任意添加删除元素.*用索引可以很方便访问元素,甚至返回一个子列表*更多方法请参考Python的文档'''4字典复制代码代码如下:#!/usr/bin/pythonx={'a':'aaa','b':'bbb','c':12}print(x['a'])print(x['b'])print(x['c'])forkeyinx:print(Keyis%sandvalueis%s%(key,x[key]))'''知识点:*将他当Java的Map来用即可.'''5字符串比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.复制代码代码如下:#!/usr/bin/pythonword=abcdefga=word[2]print(ais:+a)b=word[1:3]print(bis:+b)#index1and2elementsofword.c=word[:2]print(cis:+c)#index0and1elementsofword.d=word[0:]print(dis:+d)#Allelementsofword.e=word[:2]+word[2:]print(eis:+e)#Allelementsofword.f=word[-1]print(fis:+f)#Thelastelementsofword.g=word[-4:-2]print(gis:+g)#index3and4elementsofword.h=word[-2:]print(his:+h)#Thelasttwoelements.i=word[:-2]print(iis:+i)#Everythingexceptthelasttwocharactersl=len(word)print(Lengthofwordis:+str(l))中文和英文的字符串长度是否一样?复制代码代码如下:#!/usr/bin/python#-*-coding:utf8-*-s=input(输入你的中文名,按回车继续);print(你的名字是:+s)l=len(s)print(你中文名字的长度是:+str(l))知识点:类似Java,在python3里所有字符串都是unicode,所以长度一致.6条件和循环语句复制代码代码如下:#!/usr/bin/python#条件和循环语句x=int(input(Pleaseenteraninteger:))ifx0:x=0print(Negativechangedtozero)elifx==0:print(Zero)else:print(More)#LoopsLista=['cat','window','defenestrate']forxina:print(x,len(x))#知识点:#*条件和循环语句#*如何得到控制台输入7函数复制代码代码如下:#!/usr/bin/python#-*-coding:utf8-*-defsum(a,b):returna+bfunc=sumr=func(5,6)print(r)#提供默认值defadd(a,b=2):returna+br=add(1)print(r)r=add(1,5)print(r)一个好用的函数#!/usr/bin/python#-*-coding:utf8-*-#Therange()functiona=range(1,10)foriina:print(i)a=range(-2,-11,-3)#The3rdparameterstandsforstepforiina:print(i)知识点:Python不用{}来控制程序结构,他强迫你用缩进来写程序,使代码清晰.定义函数方便简单方便好用的range函数8异常处理复制代码代码如下:#!/usr/bin/pythons=input(Inputyourage:)ifs==:raiseException(Inputmustnobeempty.)try:i=int(s)exceptExceptionaserr:print(err)finally:#Cleanupactionprint(Goodbye!)9文件处理对比Java,python的文本处理再次让人感动复制代码代码如下:#!/usr/bin/pythonspath=D:/download/baa.txtf=open(spath,w)#Opensfile
本文标题:干货:Python3-入门教程-简单但比较不错
链接地址:https://www.777doc.com/doc-5329093 .html