python 数据类型
计算机是用来辅助人们, 在程序设计中也映射了显示世界的分类,以便于抽象的分析.
- 数字
- 字符串
- 列表
- 元组
- 字典
准备:
type(variable|constant) 变量/常量 的类型
一, 数字 类型
1. 数字 -> 整型 - int
范围: 4 字节, -( 2 ** 31) -> 2 ** 31
>>> type(1)
<type 'int'>
2. 数字 -> 整型 - long
范围: 8 字节
用后缀 "L"/"l" 来标识 长整形
>>> type(1L)
<type 'long'>
3. 数字 -> 浮点型 - float
>>> type(1.2)
<type 'float'>
4. 数字 -> 复数型 - (complex)
>>> type(1j)
<type 'complex'>
二, 字符串 string
1. 字符串界定符
① ' 单引号
② " 双引号
③""" 三重引号 docstring, 自定义排版
2, 引号嵌套
① 双引号界定的字符了可以出现单引号
② 转义 (占义字符以"\"开头)
3. 举例
①
>>> str1 = 'hello'
>>> str2 = "hello"
>>> str3 = "Let's go!"
>>> str4 = 'Let\'s go!!'
>>> str1
'hello'
>>> str2
'hello'
>>> str3
"Let's go!"
>>> str4
"Let's go!!"
②
>>> say="""
... hello!
... I'm Tom,
... how are you?
... """
>>> say
"\nhello!\nI'm Tom,\n\thow are you?\n"
>>> print say
hello!
I'm Tom,
how are you?
三, 字符串操作
1. 通过索引取字符
mystring[0]
mystring[1]
2. 截取
mystring(start:end:step)
.... 待续