python 基本数据类型讲解

一:类型

1 不可变数据类型

string,int ,tuple 常量

字符串不可变之再探

string[1] = 'new_one' can?

2 可变数据类型变量

dict list

a = "test"
print a[0]

b = [1,1,2,3]
print b[0]
b[0] = 4
print b

在这里如果是字符串形式的 a[0] = 'r',这里会出现错误,这个就是不可变数据类型和可变数据类型的区别!

 

二 再研究字符串

序列到底是什么

1 三个符合的区别 '',"",""" """

a = "test '"
print a

b = 'test"'
print b

在这里这2种都是合法的,都不会出现编译错误!
但是如果

a = "test ""
print a

这个会出现编译错误!
#coding=utf-8
print """
"哈哈哈哈"

"我是一个小菜鸟"

"""

 

2 偏移量从0开始

3 如何修改字符串之replace,find

由于字符串不可变,所以其实是形成了一个新的字符串变量
a = "i love china"
a = a.replace("china","wit")
print a

i love wit
a = "i love china"
a = a.find("china")
print a

结果
7

在这里找到下标是单词的第一个字符的起始位置。

 

其实本质是新建了一个字符串变量!


三 格式化细究

1 % 格式化方式
2 format格式化方式

a = "this is %s %s"%("my","apple")
print a

b = "this is {} {}".format ("my","apple")
print b

c = "this is {1} {0}".format ("apple","my")
print c

结果:
this is my apple
this is my apple
this is my apple

 


3 为什么要用format
4 还有一个方法,字典来了。

四 再议打开文件

标准库的介绍 linecache

posted on 2015-01-17 16:45  liu168aad  阅读(197)  评论(0编辑  收藏  举报