高级变量类型——Python
目标
-
列表
-
元组
-
字典
-
字符串
-
公共方法
-
变量高级
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In [1]: name_list.
name_list.append name_list.count name_list.insert name_list.reverse
name_list.clear name_list.extend name_list.pop name_list.sort
name_list.copy name_list.index name_list.remove
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
info_tuple = () info_tuple = (50, ) |
|
info = ()
info.count info.index
|
|
|
|
info = ("zhangsan", 18) print("%s 的年龄是 %d" % info) list(元组)
tuple(列表)
|
|
|
|
`xiaoming = {}` In [1]: xiaoming. xiaoming.clear xiaoming.items xiaoming.setdefault xiaoming.copy xiaoming.keys xiaoming.update xiaoming.fromkeys xiaoming.pop xiaoming.values xiaoming.get xiaoming.popitem
|
|
# for 循环内部使用的 `key 的变量` in 字典 for k in xiaoming: print("%s: %s" % (k, xiaoming[k])) |
|
card_list = [{"name": "张三", "qq": "12345", "phone": "110"}, {"name": "李四", "qq": "54321", "phone": "10086"} ] |
|
string = "Hello Python" for c in string: print(c) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
`hello_str = ""` In [1]: hello_str. hello_str.capitalize hello_str.isidentifier hello_str.rindex hello_str.casefold hello_str.islower hello_str.rjust hello_str.center hello_str.isnumeric hello_str.rpartition hello_str.count hello_str.isprintable hello_str.rsplit hello_str.encode hello_str.isspace hello_str.rstrip hello_str.endswith hello_str.istitle hello_str.split hello_str.expandtabs hello_str.isupper hello_str.splitlines hello_str.find hello_str.join hello_str.startswith hello_str.format hello_str.ljust hello_str.strip hello_str.format_map hello_str.lower hello_str.swapcase hello_str.index hello_str.lstrip hello_str.title hello_str.isalnum hello_str.maketrans hello_str.translate hello_str.isalpha hello_str.partition hello_str.upper hello_str.isdecimal hello_str.replace hello_str.zfill hello_str.isdigit hello_str.rfind 提示:正是因为 python 内置提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作!应对更多的开发需求
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
字符串[开始索引:结束索引:步长]
注意:
索引的顺序和倒序
|
|
Python 包含了以下内置函数:
注意
|
|||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||
|
注意
成员运算符成员运算符用于 测试 序列中是否包含指定的 成员
注意:在对 字典 操作时,判断的是 字典的键 |
|||||||||||||||||||||||||||||||||||||||
|
for 变量 in 集合: 循环体代码 else: 没有通过 break 退出循环,循环结束后,会执行的代码
应用场景
students = [ {"name": "阿土", "age": 20, "gender": True, "height": 1.7, "weight": 75.0}, {"name": "小美", "age": 19, "gender": False, "height": 1.6, "weight": 45.0}, ] find_name = "阿土" for stu_dict in students: print(stu_dict) # 判断当前遍历的字典中姓名是否为find_name if stu_dict["name"] == find_name: print("找到了") # 如果已经找到,直接退出循环,就不需要再对后续的数据进行比较 break else: print("没有找到") print("循环结束")
|