Python入门之基本数据类型
2017-07-23 01:14
417 查看
数据类型的组成
由3部分组成:id:通过id方法查看它的唯一标识,内存地址
type:数据类型
值
python 里面一切都是指针
基本数据类型
intboolean
字符串,也称之为“序列”
列表
元组 tuple
字典
类型也分为可变类型和不可变类型;其中可变类型:int ,string,tuple(a=(1,2,3));可变类型如list,tuple,dict
注意:python虽然是动态语言,但是如果变量的类型确定了之后,就不能更改。
整型与boolean
print 1==1 //true print 1=="1" //虽然值相同,但是类型不同 bool(1==1) //该表达式的“结果”是true
字符串的认识
python 默认的编码方式 ascii码len() 方法的使用
a="1234" len(a) //返回结果是 4 a=“哈” len(a) //返回结果是3 a= u"哈” len(a) //返回结果是1 a="哈哈哈" g=a.decode("utf-8") len(g) //返回结果是3
创建一个.py文件,文件的内容如下:
#coding = utf-8 a = “哈哈”.decode("uft-8") print len(a)
2.转义字符
将要转意的字符前面加上 “\”
print "abc\n" //返回结果是 abc 加换行的文本 print r"\n" //r表示不要转移 ,故结果是 \n print u"abc" //u 表示是unicode编码
3.子串
a = "abcdef" a[0] //返回值是 a a[len(a)-1] //返回值是f a[-1] //返回值是 f a[0:] //返回值是 "abcdef" a[1:2] //返回值 b
4.替换方法 replace(old,new)
a = "abc" g = a.replace('a','cccc') print g //返回值 ccccbc
5.拼接字符串
a= "abc" b="def" print a+b //显示值 "abcdef" //优化的解决办法,通过"占位符" print "my name is %s" %"zhang" //返回值结果是 my name is zhang //多个占位符 print "%d world, one %s" %(1,"dream") //join方法实现拼接 a = "zhang" b = "wang" c = "li" print "".join([a,b,c]) //输出结果是 zhangwangli print ",".join([c,b,a]) //输出结果是 li,wang,zhang
6 文件打开
//写入文件 a = open("a.txt",'w') a.write("hi\nsecond hi.") a.close() //一定要关闭流 //读取文件 r = open("a.txt",'r') print r.readLine(); //读取一行 print r.read(1000); //读取1000字节
7 符号区分
’ ‘
“”
“”” “”” : 连续三个”“” 表示是一个多行文本
8 字符串的内置方法
replace
a = "this is the world" print a.repleace("this","that") // that is the world
find
定位子字符串的位置
9.占位符的描述
%d %s a = "this is %s %s" %("my","apple") b = "this is {1} {0}".format("my","apple") c = "this is {whose} {fruit}".format(fruit="apple",whose="my") //通过字典 d = "this is %(whose)s %(fruit)s" %{'whose':'my','fruit':'apple'}
10 文件读写 import linecache
相关文章推荐
- Python的入门与进阶的学习笔记之Python基本数据类型
- 从零学Python之入门(二)基本数据类型
- python快速入门 数据输出和基本类型 常用的循环遍历等
- Python入门笔记 之 基本数据类型和变量
- python3 入门 个人学习笔记 基本数据类型
- 从零学Python之入门(二)基本数据类型
- Python 入门之基本数据类型
- python入门(二) -- 基本数据类型
- 思维导图版Python3入门与进阶(一)基本数据类型
- python入门基础----基本数据类型
- python入门--基本数据类型
- python入门-基本数据类型和集合
- python 入门学习笔记之基本语法与基本数据类型
- python 入门-输入输出与基本数据类型
- python基础-基本数据类型
- Python入门(一):变量和数据类型
- java入门学习(2)—基本数据类型
- java入门-标识符、关键字、基本数据类型、变量与常量、运算符
- python基本数据类型,数字与字符串
- scala 开发入门(1)-- 变量及基本数据类型