python 变量类型

python 中有5种标准类型

1 numbers(数字)
2 string(字符串)
3 list(列表)
4 tuple(元组)
5 dict(字典)

数字

python支持4种类型的数字

  1. int (有符号整型)
  2. long (长整型[八进制或十六进制])
  3. float (浮点型)
  4. complex (复数)

字符串、列表、元组

  1. str=” ” , list = [] , tuple=()
  2. 2种索引,从左到右默认下标从0开始,从右到左默认下标从-1开始
  3. 可以用[头下标:尾下标]来选取部分元素
  4. +表示连接操作,*表示重复操作
  5. 列表和元组可以嵌套,元组中的元素不可以改变,列表中可以
str = 'hello world'

print str[0:3]
print str[-1]
print str * 2
print str + "ok"

list = ['sleepy', 134, 'canwe', 456]
tinylist = [ list, 123]

print list
print list[0][1]
print list[2:]
print list[0][1:] * 2
print list + tinylist

运行结果

hel
d
hello worldhello world
hello worldok
['sleepy', 134, 'canwe', 456]
l
['canwe', 456]
leepyleepy
['sleepy', 134, 'canwe', 456, ['sleepy', 134, 'canwe', 456], 123]

字典

  1. 字典用{}来表示
  2. 字典由索引(keys)和键值(values)组成
list = ['sleepy', 134, 'canwe', 456]
dict = {}
dict['one'] = "this is one"
dict[2] = "this is two"
dict[3] = [123, list]

print dict
print dict[3]
print dict.keys()
print dict.values()

运行结果

{2: 'this is two', 3: [123, ['sleepy', 134, 'canwe', 456]], 'one': 'this is one'}
[123, ['sleepy', 134, 'canwe', 456]]
[2, 3, 'one']
['this is two', [123, ['sleepy', 134, 'canwe', 456]], 'this is one']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值