Python基础之高级变量类型实例详解
Python  /  管理员 发布于 7年前   172
本文实例讲述了Python高级变量类型。分享给大家供大家参考,具体如下:
列表
Python 中数据类型可以分为 数字型 和 非数字型
List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组
注意:从列表中取值时,如果 超出索引范围,程序会报错
name_list = ["zhangsan", "lisi", "wangwu"]
In [1]: name_list.name_list.append name_list.count name_list.insert name_list.reversename_list.clear name_list.extend name_list.pop name_list.sortname_list.copy name_list.index name_list.remove
| | | 列表.append(数据) | 在末尾追加数据
| | | 列表.extend(列表2) | 将列表2 的数据追加到列表 |
| 2 | 修改 | 列表[索引] = 数据 | 修改指定索引的数据 |
| 3 | 删除 | del 列表[索引] | 删除指定索引的数据 |
| | | 列表.remove[数据] | 删除第一个出现的指定数据 |
| | | 列表.pop | 删除末尾数据 |
| | | 列表.pop(索引) | 删除指定索引数据 |
| | | 列表.clear | 清空列表 |
| 4 | 统计 | len(列表) | 列表长度 |
| | | 列表.count(数据) | 数据在列表中出现的次数 |
| 5 | 排序 | 列表.sort() | 升序排序 |
| | | 列表.sort(reverse=True) | 降序排序 |
| | | 列表.reverse() | 逆序、反转 |
del 关键字(科普)
del name_list[1]
在日常开发中,要从列表删除数据,建议 使用列表提供的方法
关键字、函数和方法(科普)
In [1]: import keywordIn [2]: print(keyword.kwlist)In [3]: print(len(keyword.kwlist))
关键字后面不需要使用括号
函数名(参数)
函数需要死记硬背
对象.方法名(参数)
在变量后面输入 .,然后选择针对这个变量要执行的操作,记忆起来比函数要简单很多
# for 循环内部使用的变量 in 列表for name in name_list: 循环内部针对列表元素进行操作 print(name)
尽管 Python 的 列表 中可以 存储不同类型的数据
info_tuple = ("zhangsan", 18, 1.75)
创建空元组
info_tuple = ()
元组中 只包含一个元素 时,需要 在元素后面添加逗号
info_tuple = (50, )
info.count info.index
有关 元组 的 常用操作 可以参照上图练习
取值 就是从 元组 中获取存储在指定位置的数据
# for 循环内部使用的变量 in 元组for item in info: 循环内部针对元组元素进行操作 print(item)
尽管可以使用 for in 遍历 元组
info = ("zhangsan", 18)print("%s 的年龄是 %d" % info)
元组和列表之间的转换
list(元组)
使用 tuple 函数可以把列表转换成元组
tuple(列表)
xiaoming = {"name": "小明", "age": 18, "gender": True, "height": 1.75}
在 ipython3 中定义一个 字典,例如:xiaoming = {}
In [1]: xiaoming.xiaoming.clear xiaoming.items xiaoming.setdefaultxiaoming.copy xiaoming.keys xiaoming.updatexiaoming.fromkeys xiaoming.pop xiaoming.valuesxiaoming.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)
在 ipython3 中定义一个 字符串,例如:hello_str = “”
In [1]: hello_str.hello_str.capitalize hello_str.isidentifier hello_str.rindexhello_str.casefold hello_str.islower hello_str.rjusthello_str.center hello_str.isnumeric hello_str.rpartitionhello_str.count hello_str.isprintable hello_str.rsplithello_str.encode hello_str.isspace hello_str.rstriphello_str.endswith hello_str.istitle hello_str.splithello_str.expandtabs hello_str.isupper hello_str.splitlineshello_str.find hello_str.join hello_str.startswithhello_str.format hello_str.ljust hello_str.striphello_str.format_map hello_str.lower hello_str.swapcasehello_str.index hello_str.lstrip hello_str.titlehello_str.isalnum hello_str.maketrans hello_str.translatehello_str.isalpha hello_str.partition hello_str.upperhello_str.isdecimal hello_str.replace hello_str.zfillhello_str.isdigit hello_str.rfind
提示:正是因为 python 内置提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作!应对更多的开发需求!
切片 方法适用于 字符串、列表、元组
字符串[开始索引:结束索引:步长]
注意:
索引的顺序和倒序
演练需求
答案
num_str = "0123456789"# 1. 截取从 2 ~ 5 位置 的字符串print(num_str[2:6])# 2. 截取从 2 ~ `末尾` 的字符串print(num_str[2:])# 3. 截取从 `开始` ~ 5 位置 的字符串print(num_str[:6])# 4. 截取完整的字符串print(num_str[:])# 5. 从开始位置,每隔一个字符截取字符串print(num_str[::2])# 6. 从索引 1 开始,每隔一个取一个print(num_str[1::2])# 倒序切片# -1 表示倒数第一个字符print(num_str[-1])# 7. 截取从 2 ~ `末尾 - 1` 的字符串print(num_str[2:-1])# 8. 截取字符串末尾两个字符print(num_str[-2:])# 9. 字符串的逆序(面试题)print(num_str[::-1])
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("找到了") # 如果已经找到,直接退出循环,就不需要再对后续的数据进行比较 breakelse: print("没有找到")print("循环结束")
关于Python相关内容感兴趣的读者可查看本站专题:《Python函数使用技巧总结》、《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号