Python基础之高级变量类型的示例分析

发布时间:2021-07-16 14:07:56 作者:小新
来源:亿速云 阅读:88

这篇文章主要介绍了Python基础之高级变量类型的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

目标

列表

知识点回顾

Python 中数据类型可以分为 数字型 和 非数字型

01. 列表

1.1 列表的定义

List(列表) 是 Python 中使用 最频繁 的数据类型,在其他语言中通常叫做 数组

注意:从列表中取值时,如果 超出索引范围,程序会报错

name_list = ["zhangsan", "lisi", "wangwu"]

Python基础之高级变量类型的示例分析

1.2 列表常用操作

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

Python基础之高级变量类型的示例分析
| | | 列表.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 keyword
In [2]: print(keyword.kwlist)
In [3]: print(len(keyword.kwlist))

关键字后面不需要使用括号

函数名(参数)

函数需要死记硬背

对象.方法名(参数)

在变量后面输入 .,然后选择针对这个变量要执行的操作,记忆起来比函数要简单很多

1.3 循环遍历

# for 循环内部使用的变量 in 列表
for name in name_list:

  循环内部针对列表元素进行操作
  print(name)

Python基础之高级变量类型的示例分析

1.4 应用场景

尽管 Python 的 列表 中可以 存储不同类型的数据

02. 元组

2.1 元组的定义

info_tuple = ("zhangsan", 18, 1.75)

创建空元组

info_tuple = ()

元组中 只包含一个元素 时,需要 在元素后面添加逗号

info_tuple = (50, )

Python基础之高级变量类型的示例分析

2.2 元组常用操作

info.count info.index

有关 元组 的 常用操作 可以参照上图练习

2.3 循环遍历

取值 就是从 元组 中获取存储在指定位置的数据

# for 循环内部使用的变量 in 元组
for item in info:

  循环内部针对元组元素进行操作
  print(item)

2.4 应用场景

尽管可以使用 for in 遍历 元组

info = ("zhangsan", 18)

print("%s 的年龄是 %d" % info)

元组和列表之间的转换

list(元组)

使用 tuple 函数可以把列表转换成元组

tuple(列表)

03. 字典

3.1 字典的定义

xiaoming = {"name": "小明",
      "age": 18,
      "gender": True,
      "height": 1.75}

Python基础之高级变量类型的示例分析

3.2 字典常用操作

在 ipython3 中定义一个 字典,例如: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

有关 字典 的 常用操作 可以参照上图练习

3.3 循环遍历

# for 循环内部使用的 `key 的变量` in 字典
for k in xiaoming:

  print("%s: %s" % (k, xiaoming[k]))

提示:在实际开发中,由于字典中每一个键值对保存数据的类型是不同的,所以针对字典的循环遍历需求并不是很多

3.4 应用场景

card_list = [{"name": "张三",
       "qq": "12345",
       "phone": "110"},
       {"name": "李四",
       "qq": "54321",
       "phone": "10086"}
       ]

04. 字符串

4.1 字符串的定义

字符串 就是 一串字符,是编程语言中表示文本的数据类型

大多数编程语言都是用 " 来定义字符串

string = "Hello Python"

for c in string:
  print(c)

Python基础之高级变量类型的示例分析

4.2 字符串的常用操作

在 ipython3 中定义一个 字符串,例如: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 内置提供的方法足够多,才使得在开发时,能够针对字符串进行更加灵活的操作!应对更多的开发需求!

  1. 判断类型 - 9
     Python基础之高级变量类型的示例分析

  2. 查找和替换 - 7
     Python基础之高级变量类型的示例分析

  3. 大小写转换 - 5
     Python基础之高级变量类型的示例分析

  4. 文本对齐 - 3
     Python基础之高级变量类型的示例分析

  5. 去除空白字符 - 3
     Python基础之高级变量类型的示例分析

  6. 拆分和连接 - 5
     Python基础之高级变量类型的示例分析

4.3 字符串的切片

切片 方法适用于 字符串、列表、元组

Python基础之高级变量类型的示例分析

字符串[开始索引:结束索引:步长]

注意:

  1. 指定的区间属于 左闭右开 型 [开始索引, 结束索引) => 开始索引 >= 范围 < 结束索引

  2. 从头开始,开始索引 数字可以省略,冒号不能省略

  3. 到末尾结束,结束索引 数字可以省略,冒号不能省略

  4. 步长默认为 1,如果连续切片,数字和冒号都可以省略

索引的顺序和倒序

演练需求

答案

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])

05. 公共方法

5.1 Python 内置函数

Python 包含了以下内置函数:
Python基础之高级变量类型的示例分析
注意

5.2 切片

Python基础之高级变量类型的示例分析

5.3 运算符

Python基础之高级变量类型的示例分析
注意

成员运算符

成员运算符用于 测试 序列中是否包含指定的 成员
Python基础之高级变量类型的示例分析
注意:在对 字典 操作时,判断的是 字典的键

5.4 完整的 for 循环语法

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("循环结束")

感谢你能够认真阅读完这篇文章,希望小编分享的“Python基础之高级变量类型的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. python高级变量类型的实例用法
  2. Python中变量类型的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:ASP.Net中怎么利用Datalist实现删除功能

下一篇:Web开发中客户端跳转与服务器端跳转有什么区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》