python之路 第二篇 数据类型详解及其方法

字符串app

#做用:描述名字,性别,国籍,地址等信息

#定义:在单引号\双引号\三引号内,由一串字符组成
name='Matthew'

#优先掌握的操做:
#一、按索引取值(正向取+反向取) :只能取
#二、切片(顾头不顾尾,步长)
#三、长度len
#四、成员运算in和not in

#五、移除空白strip
#六、切分split
#七、循环
 

 用法实例iphone

#strip
name = '**Matthew*'
print(name.strip(*))
print(name.lstrip(*))
print(name.rstrip(*))

#lower,upper
name = 'Matthew'
print(name.lower())
print(name.upper())

#startwith,endwith
name = 'Matthew'
print(name.endwith('w'))
print(name.startwith('M'))

#format
res = '{},{},{}'.format('Matthew',18,'male')
res = '{0},{1},{0}'.format('Matthew',18,'male')
res = '{name},{age},{sex}'.format(name='Matthew',age=18,sex='male')

#split
name = 'Matthew_male'
name.split('_')

#join
tag = ''
print(tag.join({'Matthew','say','hello','world'})

#replace
msg = 'Alex is a boy'
print(msg.replace('boy','girl'))


 列表spa

#做用:存储数据,进行数据迭代
#定义:[]内能够有多个任意类型的值,逗号分隔

name = ['matthew','egon','alex']

#掌握的操做

1.取值
2.切片
3.长度
4.成员运算
5.追加
6.删除
7.循环

练习code

 
 
list1 = ['matthew','egon','alex']#增
>>>new_list=list1.append('小马哥')
new_list = ['matthew','egon','alex','小马哥']#添加到末尾
#删
>>>new_list = list1.pop() #从末尾删除1个值并有返回值
‘小马哥’

#改
>>>new_list[0] = 'matt'           #第一个元素值改成‘matt’
#查:

按索引取值
>>>list1[0]
'matthew'
从头至尾取值,步长为2
>>>list1[0::2]
‘matthew’,'alex'

#反转列表
>>>list1[::-1]
‘alex','egon','matthew'
#求列表长度(通常用于遍历条件)
>>>len(list1)
3

补充:

 

 元组orm

#做用:不可变的列表,(能够用做字典的key),主要用来读
 
 msg=(1,2,3,4)

#须要掌握的操做:
1.按索引取值(顾头不顾尾)
2.切片
3.成员运算 in和not in
4.循环

练习blog

age = [('matthew',18),('egon','38'),('alex','38')]
#遍历出每一个人的年龄
for res in age:
    print(res[1])

 

字典索引

#做用:用于存储数据,读取速度比列表快,存储方式为key-value方式存取

ps:key必须为不可变类型(字符串,数字,元组)

price = {
'星冰乐':28
'iphone':5000
'兰博基尼模型':200
}

#须要掌握的操做:
1.按key存取数据
2.字典长度(key的个数)
3.成员运算 in 和not in
4.删除
5.循环

练习ip

相关文章
相关标签/搜索