Python基础之变量类型

Python 基础之变量类型

基本数据类型

Python有五个标准的数据类型:

  • Number(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Dictionary(字典)/Set(集合)

数字

Python支持四种不同的数值类型:

int(有符号整型)
long(长整型[也可以代表八进制和十六进制])
float(浮点型)
complex(复数)

数字是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象。

例子1:

# coding=utf-8
__author__ = 'Z'

a = 1
b = 1.5
c = 123456789123
d = 123L
e = 2 + 3j

print 'a:', a, type(a)
print 'b:', b, type(b)
print 'c:', c, type(c)
print 'd:', d, type(d)
print 'e:', e, type(e)
print '2**32=', 2 ** 32


运行结果:
a: 1 <type 'int'>
b: 1.5 <type 'float'>
c: 123456789123 <type 'long'>
d: 123 <type 'long'>
e: (2+3j) <type 'complex'>
2**32= 4294967296

例子2:

# coding=utf-8
__author__ = 'Z'

a = 1
b = 2
print 'a:', a, id(a)
print 'b:', b, id(b)

del a
print a


运行结果:
a: 1 7011808
b: 2 7011796
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print a
NameError: name 'a' is not defined

布尔bool

布尔“数”,本质上是整型的子类,对应于整型的1和0。

# coding=utf-8
__author__ = 'zyt'

print int(True)
print int(False)


运行结果:
1
0

字符串str

raw strings例子,当不想让\转义字符时,可在引号前放一个r

# coding=utf-8
__author__ = 'zyt'

print 'C:\some\name'  # here \n means newline!
print r'C:\some\name'  # raw strings:note the r before the quote


运行结果:
C:\some
ame
C:\some\name
  • 谈谈raw_input()

从标准输入读取字符串,读取值作为字符串形式返回

raw_input(…)
raw_input([prompt]) -> string

raw_input例子:

# coding=utf-8
__author__ = 'zyt'

a = raw_input("input something:")
print a, type(a)


运行结果:
input something:123
123 <type 'str'>

元组tuple

元组可用( )创建。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。

注意当定义的元组仅有1个元素时,也要加上逗号,否则就跟数学公式中的小括号发生歧义了:

# coding=utf-8
__author__ = 'zyt'

a = (1)  # 按数学公式小括号运算
b = (1,)

print a, type(a)
print b, type(b)


运行结果:
1 <type 'int'>
(1,) <type 'tuple'>

列表list

列表可用[ ]创建。

# coding=utf-8
__author__ = 'Z'

a = [1, 2, 3]
b = ['hello', 'world']
print type(a)
print a[0:2]
print a + b  # +号是连接运算符
print a * 3  # *号是重复操作次数


运行结果:
<type 'list'>
[1, 2]
[1, 2, 3, 'hello', 'world']
[1, 2, 3, 1, 2, 3, 1, 2, 3]

字典dict

字典可用{ }创建。字典由索引(key)和它对应的值value即key:value对组成。
几乎所有类型的Python对象都可以用作键,不过一般还是以不能改变的数字或者字符串最为常用。

# coding=utf-8
__author__ = 'zyt'

a = {'one': 1, 'two': 2, 'three': 3}
print a, type(a)
print a['two']
print a.keys()
print a.values()


运行结果:
{'three': 3, 'two': 2, 'one': 1} <type 'dict'>
2
['three', 'two', 'one']
[3, 2, 1]

集合set

集合也是可用{ }创建,内部元素间用逗号分割。set和dict类似。

# coding=utf-8
__author__ = 'zyt'

s = {4, 5, 6, 7, 8}
print s, type(s)

print 5 in s
print 6 not in s
print 10 in s

if 5 in s:
    print 'yes in it'
else:
    print 'not in it'


运行结果:
set([8, 4, 5, 6, 7]) <type 'set'>
True
False
False
yes in it

对象值的比较、对象身份的比较

参考《Python核心编程(第二版)WesleyJChun著-宋吉广译-4.5.2节》

# coding=utf-8
__author__ = 'zyt'

a = b = 8.5
c = 7.0 + 1.5
print 'id(a):', id(a)
print 'id(b):', id(b)
print 'id(c):', id(c)
print 'a==b:', a == b
print 'a==c:', a == c
print 'a is b:', a is b  # 等价于 id(a)==id(b)
print 'a is c:', a is c



运行结果:
id(a): 5393952
id(b): 5393952
id(c): 5393904
a==b: True
a==c: True
a is b: True
a is c: False

标准类型的分类

  • 按存储模型分类
分类Python类型
标量/原子类型数值(所有的数值类型),字符串(全部是文字)
容器类型元组、列表、字典
  • 按更新模型分类
分类Python类型
可变类型列表、字典
不可变类型数字、字符串、元组
  • 按访问模型分类
分类Python类型
直接访问数字
顺序访问字符串、元组、列表
映射访问字典

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值