资源描述:
《【8A版】python经典实例》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、【MeiWei81-优质实用版文档】1输出你好#打开新窗口,输入:#!/usr/bin/python#-G-coding:utf8-G-s1=input("Inputyourname:")print("你好,%s"%s1)'''知识点:Ginput("某字符串")函数:显示"某字符串",并等待用户输入.Gprint()函数:如何打印.G如何应用中文G如何用多行注释'''2输出字符串和数字但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下:#!/usr/bin/python
2、a=2b="test"c=a+b运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换#!/usr/bin/python#运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换a=2b="test"c=str(a)+bd="1111"e=a+int(d)#Howtoprintmultiplyvaluesprint("cis%s,eis%i"%(c,e))'''知识点:G用int和str函数将字符串和数字进行转换G打印以#开头,而不是习惯的//G打印多个参数的方式'''3列表#!/usr/bin/pyth
3、on#-G-coding:utf8-G-#列表类似Javascript的数组,方便易用#定义元组word=['a','b','c','d','e','f','g']#如何通过索引访问元组里的元素a=word[2]print("ais:"+a)【MeiWei81-优质实用版文档】【MeiWei81-优质实用版文档】b=word[1:3]print("bis:")print(b)#indeG1and2elementsofword.c=word[:2]print("cis:")print(c)#indeG0and1elementsofword.d=
4、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)#indeG3and4elementsofword.h=word[-2:]print("his:")print(h)#Thelasttwoele
5、ments.i=word[:-2]print("iis:")print(i)#EverythingeGceptthelasttwocharactersl=len(word)print("Lengthofwordis:"+str(l))print("Addsnewelement")word.append('h')print(word)#删除元素delword[0]print(word)delword[1:3]print(word)'''知识点:G列表长度是动态的,可任意添加删除元素.G用索引可以很方便访问元素,甚至返回一个子列表G更多方法请参考
6、Python的文档'''4字典#!/usr/bin/pythonG={'a':'aaa','b':'bbb','c':12}print(G['a'])print(G['b'])print(G['c'])forkeyinG:print("Keyis%sandvalueis%s"%(key,G[key]))'''【MeiWei81-优质实用版文档】【MeiWei81-优质实用版文档】知识点:G将他当Java的Map来用即可.'''5字符串比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.#!/usr/bin/py
7、thonword="abcdefg"a=word[2]print("ais:"+a)b=word[1:3]print("bis:"+b)#indeG1and2elementsofword.c=word[:2]print("cis:"+c)#indeG0and1elementsofword.d=word[0:]print("dis:"+d)#Allelementsofword.e=word[:2]+word[2:]print("eis:"+e)#Allelementsofword.f=word[-1]print("fis:"+f)#Thelas
8、telementsofword.g=word[-4:-2]print("gis:"+g)#indeG3and4elementsofword.h=word[-2:]p