python学习之路二(字符串,字典,序列和元组)

来源:互联网 发布:存储图片用什么数据库 编辑:程序博客网 时间:2024/04/29 22:06
# -*- coding: utf-8 -*'''Created on 2013-7-26@author: lixingle'''#!/usr/bin/pythonimport math#导入数学函数import codecsprint "hello"print type (2)#type 类型转换函数print int('2')print str(32)#数学函数使用print mathprint math.log10(2)print math.piprint math.sqrt(2)#开方#键盘输入#myinput=raw_input('请输入你的姓名\n')#print myinput#字符串。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。fruit='banana'print fruit[1]length=len(fruit)print length,'\n'#遍历for char in fruit:    print char#字符串切片numberstr='12345'print numberstr[:]#12345print numberstr[:3]#123print numberstr[3:]#45numberstr='abcde'print numberstr.upper()#ABCDEindex=numberstr.find('c')print 'the index is :',index#the index is : 2#在3,4中找index=numberstr.find('c',3,4)print 'the index is :',index#文件读取。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。fin=open('G://aa.txt').read().decode("gbk")#print fin#定义一个函数。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。def ifAinB(aword,bword):    for letter in aword:        if letter not in bword:            return False     return True                  print ifAinB('123','12345')#列表。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。charlist=['a','g','c','f','e']charlist2=['a','g']print charlistcharlist.sort()#排序print charlistcharlist.extend(charlist2)#添加print charlist#删除元素 (四种方法)charlist.pop(1) #要删除的下标print '删除后: ',charlist#['a', 'e', 'f', 'g', 'a', 'g']charlist.pop() #不传参数,默认删除最后一个print '删除后: ',charlist# ['a', 'e', 'g', 'a']del charlist[2:3]#删除第三个print '3删除后: ',charlist# ['a', 'e', 'g', 'a']charlist.remove('a') #找到第一个匹配结果即停止print '4删除后: ',charlist# ['e', 'g', 'a']#列表和字符串。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。stra='welcome to yantai univercity'print straliststra=list(stra)print liststra#['w', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'y', 'a', 'n', 't', 'a', 'i', ' ', 'u', 'n', 'i', 'v', 'e', 'r', 'c', 'i', 't', 'y']t=stra.split()#分割单词print t#['welcome', 'to', 'yantai', 'univercity']stra='welcome-to-yantai-univercity't=stra.split('-')#分割单词print t#['welcome', 'to', 'yantai', 'univercity']#字典。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。mydict={1:'lele',2:'lixingle',3:'wildcat'}print mydict#{1: 'lele', 2: 'lixingle', 3: 'wildcat'}print mydict[2]#lixinglemydict['4']='haha'print '添加后:',mydict#添加后: {1: 'lele', 2: 'lixingle', 3: 'wildcat', '4': 'haha'}print 2 in mydict#判断是否含键2:Trueprint 'lixingle' in mydict#:False   in只能判断键是否在其中#查看值values=mydict.values()print 'lixingle' in values#Truestringstr='abcdacft'#定义一个求字符串中字符和个数的函数def thecharAndNumber(s):    chardict=dict()#创建一个空的字典    for lt in s:        if lt not in chardict:            chardict[lt]=1        else:            chardict[lt]+=1    return chardict#调用函数print thecharAndNumber(stringstr)    #{'a': 2, 'c': 2, 'b': 1, 'd': 1, 'f': 1, 't': 1}    #元组  是不可变的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。#元组一班用()array='a',#创建一个元素的元组不要忘了最后的','否则不是元组print arrayarrayb=(1,2,3,5,6)print arrayb#使用tuple创建一个元祖arrc=tuple('hello')print arrc#('h', 'e', 'l', 'l', 'o')print arrc[1]#eprint arrc[2:4]#('l', 'l')arraye=('ll','ee','ss','dd','aa')print arraybprint arrayea,b=arrayb,arrayeprint '交换后'print a,b#(1, 2, 3, 5, 6) ('ll', 'ee', 'ss', 'dd', 'aa')#内建函数zipprint zip([1,2,3],'abc')#生成一个元祖的列表[(1, 'a'), (2, 'b'), (3, 'c')]#字典和元组dictll={1: 'lele', 2: 'lixingle', 3: 'wildcat', '4': 'haha'}print dictll.items() #把字典转换为元祖:[(1, 'lele'), (2, 'lixingle'), (3, 'wildcat'), ('4', 'haha')]

原创粉丝点击