基础数据类型:序列

数据结构的概念:

数据结构是通过某种方式(例如通过对元素进行编号)组织在一起的数据元素的集合。

最基本的数据结构是序列(sequence)

Python 6种内建的序列:列表、元组、字符串、Unicode字符串、buffer对象、xrange对象

序列基本的操作:

1. 索引

name = ['alex','alan','lee']

name[0] ==> 'alex'

 

age = (1,2,3,4)

age[0] ==> 1

 

str = 'abcdefg'

str[3] ==> 'd'

 

unicodestr = u'hello world'

unicodestr[4] ==> 'o'

 

2.分片

name_s = ['alex','alan','lee']

name_list[1:2] ===>alan

 

age_s = (1,2,3,4)

age_s[0:2] ===> (1,2)

 

str_s = 'oiuytr'

str_s[1:3] ===> 'iu'

 

unicodestr_s = u'hello world'

unicodestr_s[1:4] ==> u'ell'

 

3.序列相加

name_add = ['alex','alan','lee']

f_add = ['cool', 'Coiu']

name_add + f_add ===> ['alex', 'alan' , 'lee', 'cool', 'Coiu']

 

age_add1 = (1,2)

age_add2 = (3,4)

age_add1 + age_add2 ===> (1,2,3,4)

 

'hello, ' + ‘world’ ===> 'hello, world'

 

u"hello, " + u"cool" ===> u'hello, cool'

 

4.乘法

name_mul = ['alex', 'alan', 'lee']

name_mul * 2 ===> ['alex', 'alan', 'lee', 'alex', 'alan', 'lee']

 

age_mul = (1,2)

age_mul*2 ===> (1,2,1,2)

 

str_mul = 'nihao'

str_mul*2 ===> 'nihaonihao'

 

unicode_str_mul = u'hello '

unicode_str_mul *2 ===> u'hello hello'

 

5.成员资格

name_in = ['alex', 'alan', 'lee']

'alex' in name_in ===>True

 

age_in = (1, 2, 3)

4 in age_in ===> False

 

str_in = 'fuckyou'

'fc' in str_in ===> False

 

unicode_str_in = u'sb'

's' in unicode_str_in ===> True

 

6.长度、最小值、最大值

age_len = (1,2,3)

len(ahe_len) ===> 3

 

age_max = (1,2,3)

max(age_max) ===>3

 

age_min = (1,2,3)

min(age_min) ===> 1

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值