-- 168-列表-10-列表的排序和反转
#! -*- coding=UTF-8-*-
name_list=[]
print(name_list,dir(name_list))
name_list=['xx','yy','zz']
print (name_list[2])
print (name_list.index('yy'))
name_list[2]='eeee' # 修改
print (name_list)
--
#! -*- coding=UTF-8-*-
name_list=['xx','yy','zz']
print (name_list[2])
name_list.append('ooo')
print (name_list)
name_list.insert(1,'llll')
print (name_list)
temp_list={'上完课','不知道'}
name_list.extend(temp_list) # 追加其它列表
print (name_list)
name_list=['xx','yy','zz','xx']
print (len(name_list),name_list.count('xx'))
--
#! -*- coding=UTF-8-*-
name_list=['xx','yy','zz','xx']
num_list=[543,5,3,63,6]
name_list.sort()
num_list.sort()
print (name_list,num_list)
num_list.sort( reverse=True) # 排序
print (num_list)
name_list.reverse() # 翻转
print (name_list)
-- 169-列表-11-关键字、函数和方法的特点和区别
打印ipython中所有关键字:
import keyword
print(keyword.kwlist)
-- 170-列表-12-迭代遍历
#! -*- coding=UTF-8-*-
name_list=['xx','yy','zz','xx']
for my_name in name_list:
print ("我的名字:%s" %my_name)
-- 172-元组-01-特点以及和列表的区别
元组用{}定义 一旦定义不能修改元素
info_tuple={"zhangsan",18,1.75} 元组通常存储不同类型的数据
-- 173-元组-02-元组变量的定义
#! -*- coding=UTF-8-*-
info_tuple = ('zhangsan', 18, 1.75)
print type( info_tuple ), info_tuple
print info_tuple[0]
empty_tuple = ()
print type( empty_tuple )
single_tuple = (5)
print type( single_tuple)
s1=(5,)
print type(s1)
-- 174-元组-03-元组变量的常用操作
#! -*- coding=UTF-8-*-
info_tuple = ('zhangsan', 18, 1.75,'zhangsan')
print(info_tuple[0])
print(info_tuple.index(18))
print(info_tuple.count("zhangsan"))
print(len(info_tuple))
-- 175-元组-04-元组变量的循环遍历
#! -*- coding=UTF-8-*-
info_tuple = ('zhangsan', 18, 1.75)
for my_inf in info_tuple:
print (my_inf)
print(dir(info_tuple))
-- 176-元组-05-元组的应用场景
-- 177-元组-06-元组和格式化字符串
#! -*- coding=UTF-8-*-
info_tuple = ('zhangsan', 18, 1.75)
print('%s 年龄是 %d 身高是 %.2f ' %('xx',18,1.75))
print('%s 年龄是 %d 身高是 %.2f ' %info_tuple)
-- 178-元组-07-元组和列表之间的转换
#! -*- coding=UTF-8-*-
num_list=[1,2,3,4]
print type(num_list)
num_tuple=tuple(num_list)
print type(num_tuple)
-- 184-字典-06-字典和列表组合的应用场景
列表是有序的 字典是无序的
#! -*- coding=UTF-8-*-
# 字典是无序的数据集合
xiaoming = {'name': 'xiaoming',
'age':18,
'gender':True,
'height':1.75,
'weight':134,}
print(type( xiaoming ), xiaoming)
print (xiaoming_dict['name'])
xiaoming_dict['xxx']='xxx' # 增加/修改
print(xiaoming_dict)
xiaoming_dict.pop('xxx')
print (xiaoming_dict)
print (len( xiaoming_dict ))
temp_dict = {'wocao': 222,
'weight': 999}
# 合并字典 如果被合并的字典中包含已经存在的键值对 会覆盖原有的键值对
xiaoming_dict.update( temp_dict )
print(xiaoming_dict)
for k in xiaoming_dict:
print('%s - %s' % (k, xiaoming_dict[k]))
#! -*- coding=UTF-8-*-
# 使用 多个键值对,存储 描述一个 物体 的相关信息 —— 描述更复杂的数据信息
# 将 多个字典 放在 一个列表 中,再进行遍历
card_list = [
{"name": "张三",
"qq": "12345",
"phone": "110"},
{"name": "李四",
"qq": "54321",
"phone": "10086"}
]
for card_info in card_list:
print(card_info)
-- 188-字符串-04-判断空白字符以及学习方法分享
#! -*- decoding=utf-8 -*-
str1 = "hello python"
str2 = '我的外号是"大西瓜"'
print(str2)
print(str1[6])
for char in str2:
print(char)
#! -*- decoding=utf-8 -*-
hello_str = "hello hello"
# 1. 统计字符串长度
print(len(hello_str))
# 2. 统计某一个小(子)字符串出现的次数
print(hello_str.count("llo"))
print(hello_str.count("abc"))
# 3. 某一个子字符串出现的位置
print(hello_str.index("llo"))
# 注意:如果使用index方法传递的子字符串不存在,程序会报错!
print(hello_str.index("abc"))
#! -*- encoding=utf-8 -*-
# 1. 判断空白字符
space_str = " \t\n\r"
print(space_str.isspace())
# 2. 判断字符串中是否只包含数字
# 1> 都不能判断小数
# num_str = "1.1"
# 2> unicode 字符串
# num_str = "\u00b2"
# 3> 中文数字
num_str = "一千零一"
print(num_str)
print(num_str.isdecimal())
print(num_str.isdigit())
print(num_str.isnumeric())
--