字符串
- ‘’ “”一样
- 转译' " \ \n
- 相加:str1+str2;字符串和数字不能直接相加,要转换格式
- 转换:str(18)
- 格式化:print 'sth is %d' % 18; print 'sth is %f' % 4.99; print 'sth is %.2f' % 4.99; print 'sth is %s' % apple; print "%s's score is %d" % ('Mike', 87)
元组tuple
('Mike', 87)
和我们用了很多次的list类似,有和list同样的索引、切片、遍历等操作
只是元组中的元素在创建之后就不能被修改
列表list
- 定义:[ , , ]不同元素可以是相同类型也可以是不同类型
- 索引:list[0]第一个,list[-1]倒数第一个,list[-3]倒数第三个
- 切片:list[a:b] 从a到b-1,list[:3],list[0:],list[:],list[0,-1]
- 修改:list[0]=
- 添加:list.append(sth.)
- 删除:del list[0]
- 随机挑选:sth=choice(list)
- 分割:list=string.split() 按照‘空格’ ‘\n’ ‘\t’
list=string.split('.') 按照‘.’
返回一个列表 - 连接:string.join(list) string表示连接符
' '.join(['hello','world']) 用空格连接
''.join(['hello','world']) 无缝连接 - 遍历:for...in...
字符串与list相同之处:
2.索引
3.切片
9.连接
10.遍历
字典dictionary
- 定义:d = {key1 : value1, key2 : value2 }
键必须是唯一的;
键只能是简单对象,比如字符串、整数、浮点数、bool值 - 空字典:d={}
- 索引:d[key]
- 修改:d[key]=
- 添加:d[新键]=
- 删除:del d[key]
- 遍历:同字符串和list
类
- 创建:class Name:
- 调用:Name()
- 类方法:和我们之前定义的函数区别在于,第一个参数必须为self。而在调用类方法的时候,通过“对象.方法名()”格式进行调用,而不需要额外提供self这个参数的值。self在类方法中的值,就是你调用的这个对象本身
网友评论