python变量类型

一、数据类型

  1. Numbers(数字)
  2. String(字符串)
  3. List(列表)
  4. Tuple(元组)
  5. Dictionary(字典)

二、数字类型

  1. int(有符号整型)
  2. long(长整型,也可以代表八进制和十六进制)
  3. float(浮点型)
  4. complex(复数)

三、字符串

str = "abcdefg"
print(str[1])
print(str[-1])
print(str[1:3])
print(str[-3:-1])

返回结果:

b
g
bc
ef

注释:正数是从前到后,从0开始排序。负数是从后到前排序,从-1开始。

负数拿不到最后一个字符

 

四、列表和元组

#列表
list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
list1 = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
list[1] = 100
print(list + list1)

#元组
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
tuple1 = (123, 'john')
print(tuple + tuple1)

输出结果:

['runoob', 100, 2.23, 'john', 70.2, 'runoob', 786, 2.23, 'john', 70.2]

('runoob', 786, 2.23, 'john', 70.2, 123, 'john')

注:列表、元组和字符查询的方式是一样的。区别为元组内容不可修改。

 

五、字典

dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
 
tinydict = {'name': 'tiny','code':6734, 'dept': 'sales'}
 
print dict['one']          # 输出键为'one' 的值
print dict[2]              # 输出键为 2 的值
print tinydict             # 输出完整的字典
print tinydict.keys()      # 输出所有键
print tinydict.values()    # 输出所有值

输出结果:

This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'runoob'}
['dept', 'code', 'name']
['sales', 6734, 'runoob']

 

posted @ 2022-02-07 11:22  mywink  阅读(119)  评论(0)    收藏  举报