# -*- coding: utf-8 -*
'''
Created on 2013-7-26 @author: lixingle
'''
#!/usr/bin/python
import math#导入数学函数
import codecs
print "hello"
print type (2)
#type 类型转换函数
print int('2') print str(32)
#数学函数使用
print math
print math.log10(2)
print math.pi
print 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[:]#12345
print numberstr[:3]#123
print numberstr[3:]#45
numberstr='abcde'
print numberstr.upper()#ABCDE
index=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 charlist
charlist.sort()#排序
print charlist
charlist.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 stra
liststra=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]#lixingle
mydict['4']='haha'
print '添加后:',mydict#添加后: {1: 'lele', 2: 'lixingle', 3: 'wildcat', '4': 'haha'} print 2 in mydict#判断是否含键2:True
print 'lixingle' in mydict#:False in只能判断键是否在其中
#查看值
values=mydict.values()
print 'lixingle' in values#True stringstr='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 array
arrayb=(1,2,3,5,6)
print arrayb
#使用tuple创建一个元祖
arrc=tuple('hello')
print arrc#('h', 'e', 'l', 'l', 'o')
print arrc[1]#e
print arrc[2:4]#('l', 'l') arraye=('ll','ee','ss','dd','aa')
print arrayb
print arraye a,b=arrayb,arraye print '交换后'
print a,b#(1, 2, 3, 5, 6) ('ll', 'ee', 'ss', 'dd', 'aa')
#内建函数zip
print 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')]

python学习之路二(字符串,字典,序列和元组)的更多相关文章

  1. python学习笔记(二)— 字典(Dictionary)

    字典是另一种可变容器模型,且可存储任意类型对象.字典是无序的,因为它没有下标,用key来当索引,所以是无序的. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分 ...

  2. python学习之路06——字符串

    字符串 1.概念 字符串就是由若干个字符组成的有限序列 字符:字母,数字,特殊符号,中文 表示形式:采用的单引号或者双引号 注意:字符串属于不可变实体 2.创建字符串 str1 = "hel ...

  3. Python学习之路3 - 字符串操作&字典

    本节内容: 常用的字符串处理. 格式化输出字符串. 字符串的替换. 字符串和二进制的相互转化. 字典的操作 字符串操作 常用的字符串处理 name = 'vector' print(name.capi ...

  4. python学习之路 二 :基本数据类型

    本节重点 理解什么是变量? 掌握各种数据类型 理解可变类型和不可变类型 一.变量和常量 变量: 作用:存贮程序的中间结果在内存里,以备后边的程序调用 定义规范: 变量名只能是 字母.数字活下划线的任意 ...

  5. Python学习之路二

    今天主要学习了列表,python的列表真的事太强大了,由于内容比较多,今天就先简单的介绍一下新学的几个成员函数吧. 首先我们要了解list是一种序列类型,其构造方式有四种形式: (1)空列表 [] ( ...

  6. python学习笔记(三)字典操作和元组操作

    字典: 字典也是我们开发过程中最常用的一种数据类型:具有极快的查找速度:字典是一种key-value的数据类型,比如说要存每个人的信息,那么每个人的编号就是key,value就是每个人的信息,这样的话 ...

  7. python学习之路-day2-pyth基础2

    一.        模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...

  8. Python学习之路-Day2-Python基础2

    Python学习之路第二天 学习内容: 1.模块初识 2.pyc是什么 3.python数据类型 4.数据运算 5.bytes/str之别 6.列表 7.元组 8.字典 9.字符串常用操作 1.模块初 ...

  9. Python学习之路【第一篇】-Python简介和基础入门

    1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...

随机推荐

  1. hdu Write a simple HTML Browser

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1088 对比输出 代码: #include <stdio.h> #include <s ...

  2. C# 使用摄像头拍照 支持Win7 64位

    原文:C# 使用摄像头拍照 支持Win7 64位 So, how do we capture an image from a WebCam? Once you download the source ...

  3. Android结构分析Android智能指针(两)

    笔者:刘蒿羽 博客:http://blog.csdn.net/liuhaoyutz Android版本号:4.4.2 在上一篇文章中,我们分析了Android智能指针中的强指针sp,本文我们来分析弱指 ...

  4. .net cookie

    HttpCookie HttpRequest HttpResponse 这3个  类 . 之间的关系 . 请求 ,相应 都有一个   public HttpCookieCollection Cooki ...

  5. 3-05. 寻求倒数第二链线性表K项目(15)(STL list应用 ZJU_PAT)

    主题链接:http://pat.zju.edu.cn/contests/ds/3-05 给定一系列正整数,请设计一个尽可能高效的算法.查找倒数第K个位置上的数字. 输入格式说明: 输入首先给出一个正整 ...

  6. Junit指定测试运行顺序

    原文链接: Test execution order原始日期: 2012年12月06日本期: 2014年7月2日翻译人员: 铁锚 说明: Junit4.11版本号及以后才支持,建议升级到最新版本号. ...

  7. 在VS下用C语言连接SQLServer2008

    原文:在VS下用C语言连接SQLServer2008 step1:启动SQLSERVER服务 step2:打建立数据库test,在test库中建立test表(a varchar(200),b varc ...

  8. SQL点滴8—the account is currently locked out. The system administrator can unlock it.

    原文:SQL点滴8-the account is currently locked out. The system administrator can unlock it. 今天遇到的问题比较有意思. ...

  9. SQL Server 性能调优 之运行计划(Execution Plan)调优

    运行计划中的三种 Join 策略 SQL Server 存在三种 Join 策略:Hash Join,Merge Join,Nested Loop Join. Hash Join:用来处理没有排过序/ ...

  10. POJ2533 Longest Ordered Subsequence 【最长递增子序列】

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32192   Acc ...