元组
元组:特点就是内容不可变,算只读的列表,可以被查询,不能被修改
a = 2, print(a) print(type(a)) b = ('a','b','c') print(b[1])
运行结果:
(2,) <class 'tuple'> b
字典
字典:内部数据打印时是无序的,键值是哈希的,有唯一性
#创建字典 a = {'a':'python','b':'ruby','c':'java'} #直接创建一个字典 print(a) #内置字典方法创建:dict a = dict((('a','b'),)) print(a) a = dict([['a','b'],]) print(a) #修改字典 a = {'a': 'b'} set1 = a.setdefault("c","java") print(a) print(set1) #查询操作 a = {'a':'python','b':'ruby','c':'java'} print(a.keys()) print(a.values()) print(a.items()) print(a['a']) #update方法 a = {'a':'python','b':'ruby','c':'java'} b = {'d':'shell'} a.update(b) print(a) a.update({'a':'javascript'}) print(a) #删除方法 a = {'a':'python','b':'ruby','c':'java'} a.clear() print(a) # 清空字典 a = {'a':'python','b':'ruby','c':'java'} del a['a'] print(a) print("********************") a = {'a':'python','b':'ruby','c':'java'} b = a.pop('b') print(a) print(b) print("********************") a = {'a':'python','b':'ruby','c':'java'} b = a.popitem() print(a) print(b) a = {'a':'python','b':'ruby','c':'java'} del a #整个字典被删除 print('其他字典方法') a = {'first':'python'} b = dict.fromkeys(['a','b'],['test']) print(a) print(b) print('字典的遍历') a = {'a':'python','b':'ruby','c':'java'} for i,j in a.items(): print(i,j) for i in a: #效率比上面的高 print(i,a[i])
字符串
只列举几个简单的应用
print('join 字符串合并方法') a = ['python','ruby','java'] b = ''.join(a) print(b) c = '*'.join(a) print(c) print('字符串常用方法') a = 'python py' print(a.count('p')) print(a.capitalize()) print("format格式化输出") a = "my favorite book {name} and {name2}" print(a.format_map({'name':'python','name2':'java'})) print(a.format(name = 'shell',name2 = 'bash')) print(a.isdigit())
PS:
前期的数据类型只是基础,掌握了常用的那几个内置函数,基础的语法就可以,在学习函数,对象一些高级话题时,可以继续巩固,所以继续加油吧
今天上班时看python时,被c++的开发鄙视了,说太简单,唉,继续加油吧