Python数据类型和语法

基本数据类型

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

  • Numbers(数字)

  • String(字符串)

  • List(列表)

  • Tuple(元组)

  • Dictionary(字典)

shell好像无明显的数据类型区分

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

  • int(有符号整型)

  • long(长整型[也可以代表八进制和十六进制])

  • float(浮点型)

  • complex(复数)

下面重点介绍字符串,列表,元组和字典三种数据类型

字符串

重点在于截取功能:

?
1
2
3
4
5
6
7
8
9
10
#!/bin/python
 
#string testing
 
str = "aabbccdd"
print ( str )
print ( str [ 0 ])
print ( str [ 2 : 5 ])
print ( str * 2 )
print ( str + "join test" )

输出:

aabbccdd

a

bbc

aabbccddaabbccdd

aabbccddjoin test

kerry

eva

111

222

shell的字符串截取如下:

从左边第几个字符开始,及字符的个数
echo ${var:0:5}
其中的 0 表示左边第一个字符开始,5 表示字符的总个数

只是个数,不是结束的索引值,而且shell还可以根据#,%,##,%%截取。

shell中左边的第一个字符是用 0 表示,右边的第一个字符用 0-1 表示)

python 中,左边第一个是0,右边第一个是-1


列表

List(列表) 是 Python 中使用最频繁的数据类型。

列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。

列表用[ ]标识。是python最通用的复合数据类型。看这段代码就明白。

列表中的值得分割也可以用到变量[头下标:尾下标],就可以截取相应的列表,从左到右索引默认0开始的,从右到左索引默认-1开始,下标可以为空表示取到头或尾。

加号(+)是列表连接运算符,星号(*)是重复操作。如下实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[nxuser@PSjamsBond - 0 - 0 - 0  ~]$ vi testall.py 
#!/bin/python
 
name = [ "kerry" , "eva" , 111 , 222 ]
name2 = [ "eva" , 333 ]
 
for  element  in  name:
    print (element)
 
print ( "all elements:" ),name
print ( "the second element is:" ),name[ 2 ]
print ( "the 1,3,sub is:" ),name[ 1 : 3 / / 前闭后开区间
print ( "double:" ),name * 2
 
print ( "join:" ),name + name2

元组(只读列表)

圆括号括住,逗号分割

?
1
2
3
4
5
6
print ( "##############" )
print ( "group testing" )
name = ( "aaa" , "bbb" , "ccc" , "ddd" )
print (name)
for  element  in  name:
   print (element)

元组不可更新,只能读取,如下

##############

group testing

('aaa', 'bbb', 'ccc', 'ddd')

aaa

bbb

ccc

ddd

Traceback (most recent call last):

  File "testall.py", line 37, in <module>

    name[2]="eee"

TypeError: 'tuple' object does not support item assignment

字典(key-value形式)

字典(dictionary)是除列表意外python之中最灵活的内置数据结构类型。列表是有序的对象结合,字典是无序的对象集合。

两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。

字典用"{ }"标识。字典由索引(key)和它对应的值value组成

?
1
2
3
4
5
6
7
8
9
10
# dict testing
 
print ( "##############" )
print ( "dict testing" )
 
student = { "kerry" : 25 , "eva" : 28 }
print (student)
 
print ( "output the keys:" ),student.keys()
print ( "output the values:" ),student.values()

附完整code清单

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/python
 
#string testing
 
str = "aabbccdd"
print ( str )
print ( str [ 0 ])
print ( str [ 2 : 5 ])
print ( str * 2 )
print ( str + "join test" )
 
 
 
#list testing
 
name = [ "kerry" , "eva" , 111 , 222 ]
name2 = [ "eva" , 333 ]
 
for  element  in  name:
    print (element)
 
print ( "all elements:" ),name
print ( "the second element is:" ),name[ 2 ]
print ( "the 1,3,sub is:" ),name[ 1 : 3 ]
print ( "double:" ),name * 2
 
print ( "join:" ),name + name2
 
# tuple testing
 
print ( "##############" )
print ( "tuple testing" )
name = ( "aaa" , "bbb" , "ccc" , "ddd" )
print (name)
for  element  in  name:
   print (element)
#name[2]="eee"
 
# dict testing
 
print ( "##############" )
print ( "dict testing" )
 
student = { "kerry" : 25 , "eva" : 28 }
print (student)
 
print ( "output the keys:" ),student.keys()
print ( "output the values:" ),student.values()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值