[Python] Python 语法及入门 (超全超详细) 专为Python零基础 一篇博客让你完全掌握

550 0
Honkers 2025-3-5 20:17:30 | 显示全部楼层 |阅读模式
<p><br></p>

<h2>一、什么是Python</h2>
<h3>1.1 Python的诞生</h3>
<p style="margin-left:0in; text-align:left">1989年,为了<strong>打发</strong>圣诞节假期,Gudio van Rossum吉多· 范罗苏姆(龟叔)决心开发一个新的解释程序(Python雏形)</p>
<p style="margin-left:0in; text-align:left">1991年,第一个Python解释器诞生</p>
<p style="margin-left:0in; text-align:left">Python这个名字,来自龟叔所挚爱的电视剧Monty Python's Flying Circus</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/dc2b66d391bcf68772e0eb1f11ccbf18.png">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/c0e53b3957a3a1f6df25f63d097e187b.png">
</p>
<p></p>
<h3>&nbsp;1.2 为什么学习Python</h3>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/38e810b6499ec70c095f7fb615f52582.png">
</p>

<p>&nbsp;简单易学、全球第一、优雅、应用场景丰富(就业方向多)</p>

<h3>&nbsp;1.3Python的应用场景</h3>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ae71f8053dafb97c6ebdd45d72c6ff26.png">
</p>
<h3>&nbsp;1.4 Python环境及软件的安装</h3>

<p>&nbsp;请移步其他博客,此篇博客主要讲述Python语法&nbsp; 软件的安装会在后一段时间再发表新博客</p>

<hr>
<p></p>
<h2>二、Python的基础语法</h2>
<h3>2.1字面量</h3>
<h4>2.1.1 什么是字面量</h4>
<p>在代码中,被写下来的固定的值(数据),叫做字面量</p>
  1. "abcd"
  2. 1
  3. 3.6
复制代码

<h4>2.1.2 字面量类型</h4>
<p>同时也是值(数据)类型</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/48f91dce71cf4525a67bd99269f81381.png">
</p>
<h4>2.1.3 什么是字符串</h4>

<p>注:先简单提出概念,方便写简易的代码,后续字符串有详解</p>

<p style="margin-left:0in; text-align:left">字符串(string),又称文本,是由任意数量的字符如中文、英文、各类符号、数字等组成。所以叫做字符的串</p>

<p style="margin-left:0in; text-align:left">如:</p>
<p style="margin-left:0in; text-align:left">"abcde"</p>
<p style="margin-left:0in; text-align:left">"世界真美好"</p>
<p style="margin-left:0in; text-align:left">"123456大揭秘"</p>
<p style="margin-left:0in; text-align:left">都是字符串</p>

<p style="margin-left:0in; text-align:left"><strong>Python</strong><strong>中,字符串需要用双引号(</strong><strong>"字符串内容")包围起来</strong></p>
<p style="margin-left:0in; text-align:left"><strong>被引号包围起来的,都是字符串</strong></p>
<p style="margin-left:0in; text-align:left">注:实际使用字符串时,无论是单引号,双引号,还是三引号都可以</p>
<p style="margin-left:0in; text-align:left">即:字符串有三种不同的定义方式</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/905e3bf83455c1a40b37f61d9220d22f.png">
</p>
<p>&nbsp;三引号定义法,表示在一堆三个双引号的范围内,均是字符串(<strong>可以换行</strong>),如下:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/fb5351203735206cc6c22c1ef72319e4.png">
</p>
<hr>
<p></p>
<h3 style="margin-left:0in; text-align:left">2.2 基础Python语句 体验Python特点</h3>
<h4>2.2.1 print</h4>
<p>print 相当于C语言中的 printf ,用法些许类似</p>
<p>如:</p>
  1. print("abc")
  2. print(123)
  3. print("9277万物可爱")
复制代码

<p>&nbsp; 运行结果:</p>

<p>abc<br> 123<br> 9277万物可爱</p>

<p>&nbsp;需要注意的是,单独输出常数和变量时,不需要使用引号</p>
<h4>2.2.2 Python 语句格式与C的区别</h4>

<p>首先,python语句不需要以分号结尾,而是以每一行作为区分,有点像每一行末尾处都加了分号(当然,实际不是,也不相同)</p>


<p>代码缩进:在C中,代码的缩进只影响代码的可读性和美观,不影响实际使用</p>
<p>而在python中,代码缩进控制着不同函数相互间的嵌套和归属</p>
<p><strong>Python</strong><strong>通过缩进判断代码块的归属关系。</strong></p>


<p>大括号格式:Kernighan和Ritchie格式 (Kb&amp;R格式)&nbsp;</p>
<p>当大括号内需要有多行语句,左侧的大括号与语句同行,不再另一分行</p>

<p></p>
  1. stu_score_dict = {    #回车符对字典间的元素无影响
  2.     "王力鸿": {
  3.         "语文": 77,
  4.         "数学": 66,
  5.         "英语": 33
  6.     }, "周杰轮": {
  7.         "语文": 88,
  8.         "数学": 86,
  9.         "英语": 55
  10.     }, "林俊节": {
  11.         "语文": 99,
  12.         "数学": 96,
  13.         "英语": 66
  14.     }
  15. }
  16. print(f"学生的考试信息是:{stu_score_dict}")
复制代码

<p>由上图可以看出左侧大括号并未独自占一行</p>

<p>注:Python语句和C语句之间还有很多区别,后续会逐步发掘</p>

<hr>
<p></p>
<h3>2.3 注释</h3>

<p style="margin-left:0in; text-align:left">注释:在程序代码中对程序代码进行解释说明的文字。</p>
<p style="margin-left:0in; text-align:left">作用:注释不是程序,<strong>不能被执行</strong>,只是对程序代码进行解释说明,让别人可以看懂程序代码的作用,能够大大增强程序的可读性。</p>

<h4>&nbsp;2.3.1 单行注释</h4>
<p>单行注释:通过 # 号定义,在#号右侧的所有内容均作为注释</p>
<p>以 #开头,#右边 的所有文字当作说明,而不是真正要执行的程序,起辅助说明作用</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/572174a598e8ccc23f6ff050764b2ccd.png">
</p>
<p>&nbsp;注:#号和注释内容一般建议以一个空格隔开</p>
<p style="margin-left:0in; text-align:left">单行注释一般用于对一行或一小部分代码进行解释</p>
<h4>2.3.2 多行注释</h4>
<p>以 一对三个双引号 引起来&nbsp; """注释内容"""&nbsp;&nbsp;来解释说明一段代码的作用使用方法</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/7407d40662950ec9b6f7fe5831713288.png">
</p>

<p><strong>&nbsp;注:多行注释可以换行</strong></p>

<p>&nbsp;多行注释一般对:Python文件、类或方法进行解释</p>
<h4>2.3.3 关于注释的面试题</h4>
<p style="margin-left:0in; text-align:left">1. 单行注释中能否使用多行注释?</p>

<p style="margin-left:0in; text-align:left">可以,但实际使用时敲下回车键会跳转下一行并自动蹦出#&nbsp; &nbsp;实际没有三引号的太多关系</p>

<p style="margin-left:0in; text-align:left">2. 多行注释中能否使用单行注释?</p>

<p style="margin-left:0in; text-align:left">可以,但无论使用还是不使用,结果都完全一致</p>

<p style="margin-left:0in; text-align:left">3. 多行注释中能否使用多行注释?</p>

<p style="margin-left:0in; text-align:left">不可以,三引号之间会就近匹配</p>

<hr>
<p></p>
<h3>2.4 变量&nbsp;&nbsp;</h3>
<h4>2.4.1 什么是变量</h4>

<p style="margin-left:0in; text-align:left">变量:<strong>在程序运行时</strong>,能储存计算结果或能表示值的抽象概念。</p>

<p style="margin-left:0in; text-align:left">简单的说,变量就是在程序运行时,记录数据用的</p>
<h4 style="margin-left:0in; text-align:left">2.4.2 变量的定义格式</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/c78dce915e82752981c2e820c8f13b91.png">
</p>
<p>&nbsp;如:</p>
  1. a = 10
复制代码

<h3>待处理位置</h3>
<p style="margin-left:0in; text-align:left">print语句如何输出多份内容?</p>
<p style="margin-left:0in; text-align:left">print(内容1, 内容2, ......, 内容N)</p>
<p style="margin-left:0in; text-align:left">和那个加号的区别</p>
<hr>
<p></p>
<h3 style="margin-left:0in; text-align:left">2.5 数据类型(初识)</h3>
<h4 style="margin-left:0in; text-align:left">2.5.1 入门款三种输入类型</h4>
<p style="margin-left:0in; text-align:left">目前在入门阶段,我们主要接触如下三类数据类型:</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/fc879b32b7b52455e3f2a4e69df8e5ab.png">
</p>
<p>&nbsp;string、int、float这三个英文单词,就是类型的标准名称。</p>
<h4>2.5.2 type() 语句</h4>
<p style="margin-left:0in; text-align:left">当某个数据编写的令人迷惑时,问题来了,如何验证数据的类型呢?</p>

<p style="margin-left:0in; text-align:left">我们可以通过type()语句来得到数据的类型:</p>
<p style="margin-left:0in; text-align:left">语法:</p>
<p style="margin-left:0in; text-align:left">type(被查看类型的数据)</p>

<p>&nbsp;使用方式:</p>
<p style="margin-left:0in; text-align:left"><strong>1. 在print语句中,直接输出类型信息:</strong></p>
  1. print(type("云边有个小卖部"))
  2. print(type(123))
  3. print(type(11.345))
复制代码

<p>&nbsp;运行结果:&nbsp;</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/95c23b494d60a141424c91b0d514dea1.png">
</p>
<p><strong>&nbsp;2. 用变量存储type()的结果(返回值):</strong></p>
  1. type_1 = type("云边有个小卖部")
  2. type_2 = type(123)
  3. type_3 = type(11.345)
  4. print(type_1)
  5. print(type_2)
  6. print(type_3)
复制代码

<p>运行结果:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/95c23b494d60a141424c91b0d514dea1.png">
</p>
<h4>&nbsp;2.5.3 变量有类型么</h4>

<p>答:变量无类型</p>

<p style="margin-left:0in; text-align:left">我们通过type(变量)可以输出类型,这是查看变量的类型还是数据的类型?</p>
<p style="margin-left:0in; text-align:left">查看的是:变量存储的数据的类型。因为,变量无类型,但是它存储的数据有。</p>
<hr>
<p></p>
<h3 style="margin-left:0in; text-align:left">2.6 类型转换</h3>
<p style="margin-left:0in; text-align:left">数据类型之间,在特定的场景下,是可以相互转换的,如字符串转数字、数字转字符串等</p>
<h4 style="margin-left:0in; text-align:left">2.6.1 常见的转换语句</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/1ee3047ab584b0a3320809ee14df8bba.png">
</p>
<p></p>
<p style="margin-left:0in; text-align:center"><strong>同前面学习的</strong><strong>type()</strong><strong>语句一样,这三个语句,都是带有结果的(返回值)</strong></p>
<p style="margin-left:0in; text-align:center"><strong>我们可以用</strong><strong>print</strong><strong>直接输出</strong></p>
<p style="margin-left:0in; text-align:center"><strong>或用变量存储结果值</strong></p>
<h4>&nbsp;2.6.2 类型转换注意事项</h4>
<p>类型转换不是万能的,毕竟强扭的瓜不甜,我们需要注意:</p>

<p style="margin-left:0in; text-align:left">1. 任何类型,都可以通过str(),转换成字符串</p>
<p style="margin-left:0in; text-align:left">2. 字符串内必须真的是数字,才可以将字符串转换为数字</p>

<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/74d85fa081a00477e996129cce65256b.png">
</p>

<p style="margin-left:0in; text-align:left">浮点数转整数会丢失精度,也就是小数部分</p>

<hr>
<p></p>
<h3>&nbsp;2.7 标识符</h3>

<p style="margin-left:0in; text-align:left">是用户在编程的时候所使用的一系列名字,用于给变量、类、方法等命名。</p>

<h4>&nbsp;2.7.1 标识符命名规则</h4>
<p style="margin-left:0in; text-align:left">Python中,标识符命名的规则主要有3类:</p>


   •
  <strong>内容限定</strong>
  

   •
  <strong>大小写敏感</strong>
  

   •
  <strong>不可使用关键字</strong>
  

<p><strong>标识符命名规则1 ——&nbsp;内容限定</strong></p>
<p style="margin-left:0in; text-align:left">标识符命名中,只允许出现:</p>

  
  <strong>•英文</strong>
  
  
  <strong>•中文</strong>
  
  
  <strong>•数字</strong>
  
  
  <strong>•下划线(_)</strong>
  

<p style="margin-left:0in; text-align:left">这四类元素。</p>
<p style="margin-left:0in; text-align:left">其余任何内容都不被允许。</p>

<p style="margin-left:0in; text-align:left">注意:<strong>1. </strong><strong>不推荐</strong><strong>使用中文</strong></p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.<strong>数字不可以开头</strong></p>

<p style="margin-left:0in; text-align:left"><strong>标识符命名规则2 —— 大小写敏感</strong></p>

<p style="margin-left:0in; text-align:left">以定义变量为例:</p>
<p style="margin-left:0in; text-align:left">Andy = “安迪1”</p>
<p style="margin-left:0in; text-align:left">andy = “安迪2”</p>
<p style="margin-left:0in; text-align:left">字母a的大写和小写,是完全能够区分的。</p>

<p style="margin-left:0in; text-align:left"><strong>标识符命名规则3&nbsp;—— 不可使用关键字</strong></p>
<p style="margin-left:0in; text-align:left">Python中有一系列单词,称之为关键字</p>
<p style="margin-left:0in; text-align:left">关键字在Python中都有特定用途</p>
<p style="margin-left:0in; text-align:left">我们不可以使用它们作为标识符</p>
<p style="margin-left:0in; text-align:left">常见的关键字有:</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/9573d9b3ae4699432fa566692a2244ac.png">
</p>
<h4>&nbsp;2.7.2 变量的命名规范</h4>
<p style="margin-left:0in; text-align:left"><strong>变量命名规范&nbsp;——&nbsp;见名知意</strong></p>
<p style="margin-left:0in; text-align:left">变量的命名要做到:</p>

  •
明了:尽量做到,看到名字,就知道是什么意思



   a = "张三"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = ''张三''
  

   b = 11&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; age = 11
  


  •
简洁:尽量在确保

明了

的前提下,减少名字的长度



<p>&nbsp;a_person_name = "张三"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name = "张三"</p>

<p>&nbsp;很明显,大多数情况下,都是右侧的命名规范更合适一些</p>
<p><strong>变量命名规范&nbsp;—— 下划线命名法</strong></p>
<p style="margin-left:0in; text-align:left">多个单词组合变量名,要使用下划线做分隔。</p>

<p style="margin-left:0in; text-align:left">firstnumber = 1</p>
<p style="margin-left:0in; text-align:left">studentnickname = "小明"</p>


<p>&nbsp;first_number = 1</p>
<p>student_nickname = "小明"</p>

<p>&nbsp;很明显,下面的两个变量命名更合适一些</p>
<hr>
<p></p>
<h3>2.8 运算符</h3>
<h4>&nbsp;2.8.1 算术运算符</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/9a960603b0dd2492ed1883846ef78175.png">
</p>
<p><strong>&nbsp;算术运算符的演示:</strong></p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/83249660a3ec9bed444f52e9b8322598.png">
</p>
<h4>&nbsp;2.8.2 赋值运算符</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4da4b4af4df080d771b0a98a0695e705.png">
</p>
<h4>&nbsp;2.8.3 复合赋值运算符</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/d23b1b600a70e9c307d37633e8d60c8a.png">
</p>
<hr>
<p></p>
<h3>&nbsp;2.9 字符串拓展</h3>
<p><strong>引号的嵌套:</strong></p>


   •
  <strong>可以使用转移字符(\)来将引号解除效用,变成普通字符串</strong>
  

   •
  <strong>单引号内可以写双引号或双引号内可以写单引号</strong>
  

<h4>&nbsp;2.9.1 字符串拼接</h4>
<p style="margin-left:0in; text-align:left"><strong>使用“+”号连接字符串变量或字符串字面量即可</strong></p>
<p></p>
  1. name = "阿蛮"
  2. print("这位青年的名字叫做" + name + ",他今年18岁了")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/a1d45a05d1b4d2787a08a802a8a68776.png">
</p>
<p style="margin-left:0in; text-align:left"><strong>字符串无法和非字符串变量进行拼接&nbsp; &nbsp; &nbsp; &nbsp;因为类型不一致,无法接上</strong></p>
  1. name = "阿蛮"
  2. age = 18
  3. print("这位青年的名字叫做" + name + "年龄周岁是" + age)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/c09b04bc9d53718838310c0e13c99dc8.png">
</p>
<h4>&nbsp;2.9.2 字符串格式化</h4>
<p>当变量过多时,我们会发现上述的字符串拼接并不好用,由此引出字符串格式化这个方法。</p>
  1. name = "CSDN"
  2. message = "学IT就来%s" % name
  3. print(message)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/407d7d3b2a62254f8beca895d7e4352b.png">
</p>

<p style="margin-left:0in; text-align:left">其中的,%s </p>

   •
  %&nbsp;  
  表示:我要占位
  

   •
  s&nbsp;&nbsp;  
  表示:将变量变成字符串放入占位的地方
  
<p style="margin-left:0in; text-align:left">所以,综合起来的意思就是:我先占个位置,等一会有个变量过来,我把它变成字符串放到占位的位置</p>

<p></p>
<p style="margin-left:0in; text-align:left"><strong>多个变量占位,</strong><strong>变量要用括号括起来,</strong><strong>并按照占位的顺序填入:</strong></p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/8d2fb5ae53498bd2144821e9e4018901.png">
</p>

<p style="margin-left:0in; text-align:left">数字也能用%s占位吗?</p>
<p style="margin-left:0in; text-align:left">可以的哦,这里是将数字 转换成了 字符串哦</p>
<p style="margin-left:0in; text-align:left">也就是数字57,变成了字符串"57"被放入占位的地方</p>

<p></p>
<p style="margin-left:0in; text-align:left"><strong>Python中,其实支持非常多的数据类型占位,</strong><strong>最常用的是如下三类:</strong></p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/484a4628ca96efa6cde48fe412320f29.png">
</p>
<p>如下代码,完成字符串、整数、浮点数,三种不同类型变量的占位(演示):</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/22eb93ae0da96d2daf75dbb9d09a8d50.png">
</p>
<h4>&nbsp;2.9.3 格式化的精度控制</h4>

<p style="margin-left:0in; text-align:left">我们可以使用辅助符号"m.n"来控制数据的宽度和精度</p>

   •
  m
  ,控制宽度,要求是数字(
  很少使用
  )
  ,
  若
  设置的宽度小于数字自身,不生效
  

   •
  .n
  ,控制小数点精度,要求是数字,
  会进行小数的四舍五入
  
<p style="margin-left:0in; text-align:left">示例:</p>

   •
  %5d
  :表示将整数的宽度控制在
  5
  位,如数字
  11
  ,被设置为
  5d
  ,就会变成:
  [
  空格
  ][
  空格
  ][
  空格
  ]11
  ,用三个空格补足宽度。
  

   •
  %5.2f
  :表示将宽度控制为
  5
  ,将小数点精度设置为
  2
  
<p style="margin-left:0in; text-align:left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 小数点和小数部分也算入宽度计算。如,对11.345设置了%7.2f 后,结果是:[空格][空格]11.35。2个空格补足宽度,小数部分限制2位精度后,四舍五入为 .35&nbsp; </p>
<p style="margin-left:0in; text-align:left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(此处需要注意的是<strong>小数点本身也是占一个宽度的</strong>,然后有些朋友可能下去也试了 print("%4.2f" % 11.345) 这段代码,发现输出是11.35,带上小数点是有5个宽度的,这是怎么回事呢?简单解释就是编译器在格式化时,如果格式化代码出现问题,会根据语义进行了代码优化,不然你试试下面这个代码 print("%1.2f" % 11.345) ,会发现输出依然是11.35,足以证实)</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%.2f:表示不限制宽度,只设置小数点精度为2,如11.345设置%.2f后,结果是11.35</p>

<p>&nbsp;体验代码:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/583998eb31c0ea97532435fe5a2895d5.png">
</p>
<h4>&nbsp;2.9.4 字符串格式化方法二</h4>

<p>&nbsp;通过语法:f"内容{变量}"的格式来快速格式化</p>

<p>演示代码:&nbsp;</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/bcb3c989f975380b5bdcb428373af25f.png">
</p>
<p style="margin-left:0in; text-align:left">这种写法不做精度控制,也不理会类型,适用于快速格式化字符串</p>
<p></p>
<h4 style="margin-left:0in; text-align:left">2.9.5 表达式的格式化</h4>
<p>什么是表达式?</p>

<p style="margin-left:0in; text-align:left">表达式:一条具有明确执行结果的代码语句</p>
<p style="margin-left:0in; text-align:left">如:</p>
<p style="margin-left:0in; text-align:left">1 + 1、5 * 2,就是表达式,因为有具体的结果,结果是一个数字</p>
<p style="margin-left:0in; text-align:left">又或者,常见的变量定义:</p>
<p style="margin-left:0in; text-align:left">name = “张三”&nbsp;&nbsp;&nbsp;&nbsp; age = 11 + 11</p>
<p style="margin-left:0in; text-align:left">等号右侧的都是表达式呢,因为它们有具体的结果,结果赋值给了等号左侧的变量。</p>

<p>&nbsp;演示代码:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ee253771e2c6a800bdba05761edf1f1c.png">
</p>
<p>&nbsp;在无需使用变量进行数据存储的时候,可以直接格式化表达式,简化代码哦</p>
<hr>
<p></p>
<h3>&nbsp;2.10 数据输入 input()函数</h3>


   •
  使用
  input()
  语句可以从键盘获取输入
  

   •
  使用一个变量接收(存储)
  input
  语句获取的键盘输入数据即可
  

   •
  <strong>要注意,无论键盘输入什么类型的数据,获取到的数据永远都是字符串类型</strong>
  

  1. print("请问你是谁")
  2. name = input ()
  3. print(f"Get!!! 你是{name}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/27be99bd1a5bfab00cbbc859cb2861fb.png">
</p>
<p>绿色字体内容是需要我们主动从键盘输入的数据,input 接收的便是我们输入的内容&nbsp;</p>
<p></p>
<p>input()语句其实是可以在要求使用者输入内容前,输出提示内容的哦,方式如下:</p>
  1. name = input ("请问你是谁 ")
  2. print(f"Get!!! 你是{name}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/5049a9582e8b85d362363a1acc6e3d40.png">
</p>
<hr>
<p></p>
<h2>&nbsp;三、Python判断语句</h2>
<h3>3.1 布尔类型 bool</h3>

<p style="margin-left:0in; text-align:left">布尔(bool)表达现实生活中的逻辑,即真和假</p>

   •
  True表示真
  

   •
  False表示假
  。
  
<p style="margin-left:0in; text-align:left">True本质上是一个数字记作1,False记作0</p>

<p>&nbsp;布尔类型也是字面量,也可以用变量存储</p>
<h3>3.2 比较运算符</h3>
<p style="margin-left:0in; text-align:left">布尔类型的数据,不仅可以通过定义得到,也可以通过比较运算符进行内容比较得到。</p>

<p style="margin-left:0in; text-align:left"><strong>比较运算的表达式返回值是布尔类型</strong></p>

  1. result = 10 &gt; 5
  2. print(f"10 &gt; 5 的结果是{result},类型是{type(result)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/917e1b9f3053e3e6015f7e9bee98cd02.png">
</p>
<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/17aeb8d5e5652d2301e630bab90db820.png">
</p>
<h3>&nbsp;3.3 if语句</h3>
<h4>3.3.1 if语句的基础语法格式</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/a472d0a17a3b870733c897c03da32254.png">
</p>

<p style="margin-left:0in; text-align:left"><strong>归属于</strong><strong>if</strong><strong>判断的代码语句块,需在前方填充</strong><strong>4</strong><strong>个空格缩进</strong></p>
<p style="margin-left:0in; text-align:left"><strong>Python</strong><strong>通过缩进判断代码块的归属关系。</strong></p>

<p>&nbsp;演示代码:</p>
  1. age = int(input())  #将字符串转换为整型
  2. print(f"我今年已经{age}岁了")
  3. if age &gt;= 18 :
  4.     print("我已经成年了")
  5.     print("即将步入大学生活")
  6. print("时间过得真快")
复制代码

<p>如果age = 18</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/c6619b2d868a46de6dde7e8eb5168715.png">
</p>
<p>&nbsp;如果age = 10&nbsp;</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/5f0d4220382271462430c3158cee8390.png">
</p>
<h4>&nbsp;3.3.2 if语句的注意事项</h4>


   •
  判断条件的结果一定要是布尔类型
  

   •
  不要忘记判断条件后的:
  引号
  

   •
  归属于
  if
  语句的代码块,需在前方填充
  4
  个空格缩进
  

<h3>&nbsp;3.4 if else 语句</h3>

<p style="margin-left:0in; text-align:left">if 条件:</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件的执行语句1</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件的执行语句2</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; ...省略...</p>
<p style="margin-left:0in; text-align:left">else:</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 不满足条件的执行语句1</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 不满足条件的执行语句2</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; ...省略...</p>

<p>&nbsp;代码演示:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/411a23da1ff5c6d6878b47581bf4bb36.png">
</p>


   •
  if
  和其代码块,条件满足时执行
  

   •
  else
  搭配
  if
  的判断条件,当不满足的时候执行
  


  
  &nbsp;if else语句的注意事项:
  

   •
  else
  不需要判断条件,当
  if
  的条件不满足时,
  else
  执行
  

   •
  else
  的代码块,同样要
  4
  个空格作为缩进
  

<h3>&nbsp;3.5&nbsp;if elif else语句</h3>
<p>使用场景:某些场景下,判断条件不止一个,可能有多个。</p>
<p>elif 类似于C中的else if</p>

<p></p>
<p style="margin-left:0in; text-align:left">if 条件一:</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件一的执行语句1</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件一的执行语句2</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; ...省略...</p>
<p style="margin-left:0in; text-align:left">elif 条件二:</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件二的执行语句1</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件二的执行语句2</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; ...省略...</p>
<p style="margin-left:0in; text-align:left">elif 条件三:</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件三的执行语句1</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 满足条件三的执行语句2</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; ...省略...</p>
<p style="margin-left:0in; text-align:left">else:</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 不满足条件的执行语句1</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; 不满足条件的执行语句2</p>
<p style="margin-left:0in; text-align:left">&nbsp; &nbsp; ...省略...</p>

<p>演示代码:&nbsp;</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/acc1a3b72c86aa0449bcff0bf4d6190c.png">
</p>
<p>&nbsp;用input语句精简代码:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/direct/0bf9f760b880419aa475a15b228e77ca.png">
</p>
  1. print("欢迎来到一曲无忧动物园")

  2. # 判断身高
  3. if int(input("请输入你的身高(cm):")) &lt; 120:
  4.     print("您的身高小于120cm,可以免费游玩。")

  5. # 判断日期
  6. elif int(input("请输入今天的日期(1-30):")) == 1:
  7.     print("今天是1号免费日,可以免费游玩。")

  8. # 判断VIP级别
  9. elif int(input("请输入你的vip级别(1-5):")) &gt; 3:
  10.     print("您的vip级别大于3,可以免费游玩。")

  11. # 所有条件都不满足
  12. else:
  13.     print("不好意思,所有条件都不满足,需要购票10元。")

  14. print("祝您游玩愉快。")
复制代码


<p>&nbsp;注意事项</p>

   •
  elif
  可以写多个
  

   •
  判断是互斥且有序的,上一个满足后面的就不会判断了
  

   •
  可以在条件判断中,直接写
  input
  语句,节省代码量
  

<h3>3.6 判断语句的嵌套</h3>
<h4>3.6.1 语法格式</h4>

<p>if&nbsp; 条件一:</p>
<p>&nbsp; &nbsp; 条件一满足时的执行语句1</p>
<p>&nbsp; &nbsp; 条件一满足时的执行语句2</p>
<p>&nbsp; &nbsp; if 条件二:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 条件二满足时的执行语句1</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 条件二满足时的执行语句2</p>

<p style="margin-left:0in; text-align:left">第二个if,属于第一个if内,只有第一个if满足条件,才会执行第二个if</p>
<h4>3.6.2 嵌套的关键点</h4>

<p style="margin-left:0in; text-align:left">嵌套的关键点,在于:空格缩进</p>
<p style="margin-left:0in; text-align:left">通过空格缩进,来决定语句之间的:层次关系</p>

<h4>3.6.3 演示代码</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/7384c104183422613e00f90c57b850e5.png">
</p>
<hr>
<p></p>
<h2 style="margin-left:0in; text-align:left">四、Python的循环语句</h2>
<h3>4.1 while循环</h3>
<h4>4.1.1while循环的基础语法</h4>

<p>while 条件:</p>
<p>&nbsp; &nbsp; 执行语句1</p>
<p>&nbsp; &nbsp; 执行语句2</p>
<p>&nbsp; &nbsp; ...省略...</p>

<p>&nbsp;每次进入循环后,将执行语句全部执行完毕后再次来到判断,如果条件依然成立,则继续进入循环,以此类推,直到条件不成立,跳出循环</p>

<p>注意事项:</p>

   •
  <strong>条件需提供布尔类型结果,</strong>
  <strong>True</strong>
  <strong>继续,</strong>
  <strong>False</strong>
  <strong>停止</strong>
  

   •
  <strong>空格缩进不能忘</strong>
  

   •
  <strong>请规划好循环终止条件,否则将无限循环</strong>
  

<h4>4.1.2 演示代码</h4>
  1. i = 0
  2. while i &lt; 5:
  3.     print("小美,我喜欢你")
  4.     i += 1
复制代码

<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/789e237ee3a6608210f4fc5d63f5543a.png">
</p>
<h4>&nbsp;4.1.3 while循环的嵌套</h4>
<p>和判断语句的嵌套类似,这里不再长篇介绍</p>
<p>演示代码:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/d40187eb5441c9bea9179829b74cf574.png">
</p>

<p>&nbsp;注意事项</p>

   •
  注意条件的控制,避免无限循环
  

   •
  多层嵌套,主要空格缩进来确定层次关系
  

<h3>&nbsp;4.2 for循环</h3>

<p><strong>!!!注意!!!</strong></p>
<p><strong>python中的for循环与C语言中的for循环差别巨大,请勿混淆</strong></p>

<h4>&nbsp;4.2.1 for循环的语法格式</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/6edcace967898f040f28b411c8ce30a7.png">
</p>
<p style="margin-left:0in; text-align:left"><strong>从待处理数据集中:逐个取出数据,</strong><strong>赋值给临时变量</strong></p>
<p style="margin-left:0in; text-align:left">演示代码:</p>
  1. # 定义字符串name
  2. name = "itCSDN"
  3. # for循环处理字符串
  4. for x in name:
  5.        print(x)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/23cc304cda011f76599717bb5ae89fb2.png">
</p>
<p></p>

<p style="margin-left:0in; text-align:left">可以看出,for循环是将字符串的内容:依次取出</p>
<p style="margin-left:0in; text-align:left">所以,for循环也被称之为:遍历循环</p>


<p style="margin-left:0in; text-align:left">同while循环不同,for循环是无法定义循环条件的。</p>
<p style="margin-left:0in; text-align:left">只能从被处理的数据集中,依次取出内容进行处理。</p>
<p style="margin-left:0in; text-align:left">所以,理论上讲,Python的for循环无法构建无限循环(被处理的数据集不可能无限大)</p>


<p>•<strong>要注意,循环内的语句,需要有空格缩进</strong></p>

<h4>&nbsp;4.2.2 range 语句</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/d1c7fed9082271ff3b1c7f5d1a430ff3.png">
</p>
<p style="margin-left:0in; text-align:left">for循环语句,本质上是遍历:可迭代对象。</p>
<p style="margin-left:0in; text-align:left">尽管除字符串外,其它可迭代类型目前没学习到,但不妨碍我们通过学习range语句,获得一个简单的数字序列(可迭代类型的一种)。</p>
<p style="margin-left:0in; text-align:left"><strong>语法1:</strong></p>

<p style="margin-left:0in; text-align:left"><strong>range(num)</strong></p>

<p style="margin-left:0in; text-align:left">获取一个从0开始,到num结束的数字序列(不含num本身)</p>
<p style="margin-left:0in; text-align:left">如range(5)取得的数据是:[0, 1, 2, 3, 4]</p>
<p><strong>语法2:</strong></p>

<p><strong>range(num1,num2)</strong></p>

<p style="margin-left:0in; text-align:left">获得一个从num1开始,到num2结束的数字序列(不含num2本身)</p>
<p style="margin-left:0in; text-align:left">如,range(5, 10)取得的数据是:[5, 6, 7, 8, 9]</p>
<p style="margin-left:0in; text-align:left"><strong>语法3:</strong></p>

<p style="margin-left:0in; text-align:left"><strong>range(num1,num2,step)</strong></p>

<p style="margin-left:0in; text-align:left">获得一个从num1开始,到num2结束的数字序列(不含num2本身)</p>
<p style="margin-left:0in; text-align:left">数字之间的步长,以step为准(step默认为1)</p>
<p style="margin-left:0in; text-align:left">如,range(5, 10, 2)取得的数据是:[5, 7, 9]</p>
<p style="margin-left:0in; text-align:left"><strong>代码演示:</strong></p>
  1. print("输出7内的偶数")
  2. for i in range(0,7,2):
  3.     print(i)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/5c5dce79ebc5dde6576260a3fa880bfd.png">
</p>
<h4>&nbsp;4.2.3 变量的作用域</h4>

<p><strong>变量的作用域指变量的有效范围</strong></p>


<p style="margin-left:0in; text-align:left">1. for循环中的变量叫做临时变量,其作用域限定为:循环内</p>
<p style="margin-left:0in; text-align:left">2. 这种限定:</p>

   •
  是编程规范的限定,而非强制限定
  

   •
  不遵守也能正常运行,但是不建议这样做
  

   •
  如需访问临时变量,可以预先在循环外定义它
  

<p style="margin-left:0in; text-align:left">如果在for循环外部访问临时变量:</p>

  •
实际上是可以访问到的


  •
在编程规范上,是不允许、不建议这么做的

<h4>&nbsp;4.2.4 for循环的嵌套</h4>
<p>for循环嵌套模式与while循环嵌套以及判断语句的嵌套都类似,注意事项如下:</p>


   •
  需要注意缩进,嵌套
  for
  循环同样通过缩进确定层次关系
  

   •
  for
  循环和
  while
  循环可以相互嵌套使用
  

<h3>4.3 continue 和 break</h3>

<p style="margin-left:0in; text-align:left">Python提供continue和break关键字</p>
<p style="margin-left:0in; text-align:left">用以对循环进行临时跳过和直接结束</p>

<p>&nbsp;和C语言中continue和break的用法和作用类似</p>
<h4>4.3.1 continue</h4>

<p style="margin-left:0in; text-align:left">continue关键字用于:中断本次循环,直接进入下一次循环</p>
<p style="margin-left:0in; text-align:left">continue可以用于:&nbsp;&nbsp;&nbsp; for循环和while循环,效果一致</p>

<p>&nbsp;当continue出现在嵌套循环中时,continue关键字只可以控制:它所在的循环临时中断</p>
<p>&nbsp;代码演示:</p>
  1. for i in range(1,3):
  2.     print(f"第{i}天:今晚的晚霞很漂亮")
  3.     for j in range(5,7):
  4.         print("今天还是去咖啡店买点面包吧")
  5.         if j == 6:
  6.             continue
  7.         print(f"下午{j}点了,晚风吹拂")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/159bcbf2fd7db61dfa8761fca1dfe0b7.png">
</p>
<p>&nbsp;可以看到,j == 6时,下面的print语句始终没有执行,而是<strong>跳了过去继续循环</strong></p>
<h4>4.3.2 break</h4>

<p style="margin-left:0in; text-align:left">break关键字用于:直接结束所在循环</p>
<p style="margin-left:0in; text-align:left">break可以用于:&nbsp;&nbsp;&nbsp; for循环和while循环,效果一致</p>

<p>当break出现在嵌套循环中时,break关键字同样只可以控制:它所在的循环永久中断</p>
<p>代码演示:</p>
  1. for i in range(1,5):
  2.     if i == 3 :
  3.         break
  4.     print(i)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/56c7bd3cffec26d7388fce876d4f364f.png">
</p>
<hr>
<p></p>
<h2>&nbsp;五、函数</h2>

<p style="margin-left:0in; text-align:left">函数:是组织好的,可重复使用的,用来实现特定功能的代码段。</p>

<p><strong>&nbsp;</strong>我们使用过的:input()、print()、str()、int()等都是Python的内置函数</p>


   •
  将功能封装在函数内,可供随时随地重复利用
  

   •
  提高代码的复用性,减少重复代码,提高开发效率
  

<h3>&nbsp;5.1 函数的定义</h3>

<p>函数的定义:<br> <strong>def 函数名(传入参数):<br> &nbsp; &nbsp; 函数体</strong></p>
<p><strong>&nbsp; &nbsp; return 返回值</strong></p>


<p>&nbsp;函数的调用:</p>
<p><strong>函数名(参数)</strong></p>


<p><strong>&nbsp;注意事项:</strong></p>
<p style="margin-left:0in; text-align:left"><strong>① 参数如不需要,可以省略</strong></p>
<p style="margin-left:0in; text-align:left"><strong>② 返回值如不需要,可以省略</strong></p>
<p style="margin-left:0in; text-align:left"><strong>③ 函数必须先定义后使用</strong></p>

<h3>&nbsp;5.2 函数的参数</h3>

<p style="margin-left:0in; text-align:left">传入参数的数量是不受限制的。</p>

   •
  可以不使用参数
  

   •
  也可以仅使用
  任意
  N
  个参数
  

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/e5a28cf9f726ddf86e11107119d1dc7c.png">
</p>


   •
  函数定义中,提供的
  x
  和
  y
  ,称之为:形式参数(形参),表示函数声明将要使用
  2
  个参数
  

   •
  <strong>参数之间使用逗号进行分隔</strong>
  

   •
  函数调用中,提供的
  5
  和
  6
  ,称之为:实际参数(实参),表示函数执行时真正使用的参数值
  

   •
  <strong>传入的时候,按照顺序传入数据,使用逗号分隔</strong>
  

<p>&nbsp;总结:</p>

  
  <strong>•函数定义中的参数,称之为形式参数</strong>
  
  
  <strong>•函数调用中的参数,称之为实际参数</strong>
  
  
  <strong>•函数的参数数量不限,使用逗号分隔开</strong>
  
  
  <strong>•传入参数的时候,要和形式参数一一对应,逗号隔开</strong>
  

<h3>&nbsp;5.3 函数的返回值</h3>
<h4>&nbsp;5.3.1 return 返回值</h4>

<p style="margin-left:0in; text-align:left">什么是函数的返回值</p>
<p style="margin-left:0in; text-align:left">函数在执行完成后,返回给调用者的结果</p>


<p>&nbsp;返回值的应用语法:<br> &nbsp;使用关键字:return 来返回结果</p>
<p>&nbsp;然后可以用变量来接收这个结果&nbsp; 变量 = 函数(参数)</p>


<p>&nbsp;注意事项:</p>
<p style="margin-left:0in; text-align:left">函数体在遇到return后就结束了,所以写在return后的代码不会执行。</p>

<h4><strong>&nbsp;5.3.2 None类型</strong></h4>

<p style="margin-left:0in; text-align:left">思考:如果函数没有使用return语句返回数据,那么函数有返回值吗?</p>
<p style="margin-left:0in; text-align:left">实际上是:有的。</p>
<p style="margin-left:0in; text-align:left"></p>
<p style="margin-left:0in; text-align:left">Python中有一个特殊的字面量:None,其类型是:<class 'nonetype'=""></class></p>
<p style="margin-left:0in; text-align:left">无返回值的函数,实际上就是返回了:None这个字面量</p>
<p style="margin-left:0in; text-align:left"></p>
<p style="margin-left:0in; text-align:left">None表示:空的、无实际意义的意思</p>
<p style="margin-left:0in; text-align:left">函数返回的None,就表示,这个函数没有返回什么有意义的内容。</p>
<p style="margin-left:0in; text-align:left">也就是返回了空的意思。</p>

<p style="margin-left:0in; text-align:left">None可以主动使用return返回,效果等同于不写return语句:&nbsp; return None</p>
<p><strong>None的应用场景:&nbsp;</strong></p>
<p style="margin-left:0in; text-align:left"></p>

<p style="margin-left:0in; text-align:left">None作为一个特殊的字面量,用于表示:空、无意义,其有非常多的应用场景。</p>

   •
  用在函数无返回值上
  
<p style="margin-left:0in; text-align:left"></p>

   •
  用在
  if
  判断上
  

   •
  <strong>在</strong>
  <strong>if</strong>
  <strong>判断中,</strong>
  <strong>None</strong>
  <strong>等同于</strong>
  <strong>False</strong>
  

   •
  <strong>一般用于在函数中主动返回</strong>
  <strong>None</strong>
  <strong>,配合</strong>
  <strong>if</strong>
  <strong>判断做相关处理</strong>
  
  

   •
  用于声明无内容的变量上
  

   •
  <strong>定义变量,但暂时不需要变量有具体值,可以用</strong>
  <strong>None</strong>
  <strong>来代替</strong>
  
  
  例如:name = None
  

<h3>&nbsp;5.4 函数的说明文档</h3>
<p style="margin-left:0in; text-align:left">函数是纯代码语言,想要理解其含义,就需要一行行的去阅读理解代码,效率比较低。</p>
<p style="margin-left:0in; text-align:left">我们可以给函数添加说明文档,辅助理解函数的作用。</p>
<p style="margin-left:0in; text-align:left">语法如下:</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/283bcf017924b869af034a688696d5c4.png">
</p>


   •
  :param  
  用于解释参数
  

   •
  :return  
  用于解释返回值
  
<p style="margin-left:0in; text-align:left"><strong>&nbsp; &nbsp; &nbsp; &nbsp;通过多行注释的形式,对函数进行说明解释</strong></p>
  
  <strong>&nbsp; &nbsp; &nbsp; &nbsp; •内容应写在函数体之前</strong>
  

<p style="margin-left:0in; text-align:left">在PyCharm编写代码时,可以通过鼠标悬停,查看调用函数的说明文档</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/e1716f393c532cf9728727b972b0dd49.png">
</p>
<h3>&nbsp;5.5 函数的嵌套调用</h3>
<p style="margin-left:0in; text-align:left">所谓函数嵌套调用指的是一个函数里面又调用了另外一个函数</p>
<p>&nbsp;执行过程:</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/161e6d76f0b7f8f58019cf20ebd2ed58.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ec76831c4b15d430146715a41d3824fb.png">
</p>
<p>&nbsp;如果函数A中,调用了另外一个函数B,那么先把函数B中的任务都执行完毕之后才会回到上次 函数A执行的位置</p>
<p>注:其实函数调用非常简单,和正常使用函数无太大区别,多写几段代码就明白了</p>
<h3>5.6 变量的作用域</h3>

<p style="margin-left:0in; text-align:left">变量作用域指的是变量的作用范围(变量在哪里可用,在哪里不可用)</p>
<p style="margin-left:0in; text-align:left">主要分为两类:局部变量和全局变量</p>

<h4>&nbsp;5.6.1 局部变量</h4>

<p style="margin-left:0in; text-align:left">所谓局部变量是定义在函数体内部的变量,即只在函数体内部生效</p>
<p style="margin-left:0in; text-align:left">局部变量的作用:在函数体内部,临时保存数据,即当函数调用完成后,则销毁局部变量</p>

<p>&nbsp;演示代码:</p>
<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/6e47387654e14db1c93dc8355831224e.png">
</p>
<p>&nbsp;变量a是定义在`testA`函数内部的变量,在函数外部访问则立即报错.</p>
<h4>5.6.2 全局变量</h4>

<p>&nbsp;所谓全局变量,指的是在函数体内、外都能生效的变量</p>

<p style="margin-left:0in; text-align:left">思考:如果有一个数据,在函数A和函数B中都要使用,该怎么办?</p>
<p style="margin-left:0in; text-align:left">答:将这个数据存储在一个全局变量里面</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/e326c5cf32d18c789f8bbf8e9eb90a5e.png">
</p>

<p>&nbsp;当全局变量和局部变量发生冲突时,优先使用局部变量</p>

<h4>&nbsp;5.6.3 global 关键字</h4>
<p style="margin-left:0in; text-align:left">☆ 使用 global关键字 可以在函数内部声明变量为全局变量, 如下所示</p>
<p style="margin-left:0in; text-align:left">
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/b00e62a2fc012faee12ac937605c3d25.png">
</p>
<p>&nbsp;若不声明,则num = 100 因为:&nbsp;当全局变量和局部变量发生冲突时,优先使用局部变量</p>
<hr>
<p></p>
<h2>&nbsp;六、数据容器</h2>
<h3>6.1 数据容器入门 数据容器是什么</h3>

<p style="margin-left:0in; text-align:left">什么是数据容器?</p>
<p style="margin-left:0in; text-align:left">一种可以存储多个元素的Python数据类型</p>

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/a4a270fb95a1cfca281177f5e1da6636.png">
</p>
<p>&nbsp;<strong>学习数据容器,就是为了批量存储或批量使用多份数据</strong></p>

<p style="margin-left:0in; text-align:left">Python中的数据容器:</p>
<p style="margin-left:0in; text-align:left">一种可以容纳多份数据的数据类型,容纳的每一份数据称之为1个元素</p>
<p style="margin-left:0in; text-align:left">每一个元素,可以是任意类型的数据,如字符串、数字、布尔等。</p>


<p style="margin-left:0in; text-align:left">数据容器根据特点的不同,如:</p>

   •
  是否支持重复元素
  

   •
  是否可以修改
  

   •
  是否有序,等
  
<p style="margin-left:0in; text-align:left">分为5类,分别是:</p>
<p style="margin-left:0in; text-align:left">列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)</p>

<hr>
<p></p>
<h3>&nbsp;6.2 list 列表</h3>
<h4>6.2.1 列表的定义</h4>
<p>基本语法:</p>

  1. <strong>字面量</strong>
  2. [元素1,元素2,元素3,元素4, ...]

  3. <strong>定义变量</strong>
  4. 变量名称 = [元素1,元素2,元素3,元素4, ...]

  5. <strong>定义空变量</strong>
  6. 变量名称 = []
  7. 变量名称 = list()
复制代码



<p style="margin-left:0in; text-align:left">列表内的每一个数据,称之为元素</p>
  
  以 [ ] 作为标识
  
  
  列表内每一个元素之间用, 逗号隔开
  
  
  元素的数据类型没有任何限制,甚至元素也可以是列表,这样就定义了嵌套列表
  

<p>演示代码:</p>
  1. name1_list = ['欧阳无双','上官婉儿','tom','black']
  2. print(name1_list)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/6e2270ba1d317dfad315aaf1ac51274a.png">
</p>
<p>也可以嵌套:&nbsp;</p>
  1. name2_list = [['red','green'],['bule','white'],name1_list]
  2. print(name2_list)
复制代码

<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/b4f2b25ce6745c9748a0d233af0c0486.png">
</p>
<hr>
<h4>&nbsp;6.2.2 列表的下标索引</h4>
<p style="margin-left:0in; text-align:left">如图,列表中的每一个元素,都有其位置下标索引,从前向后的方向,从0开始,依次递增</p>
<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4bd3d3f6c4c4080e815bc58281650dc4.png">
</p>
<p>&nbsp;我们只需要按照下标索引,即可取得对应位置的元素。</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/892b2f85b3446f31559155b6690be710.png">
</p>
<p>&nbsp;或者,可以反向索引,如图,从后向前,下标索引为:-1、-2、-3,依次递减。</p>
<hr>
<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ef40f103839579c5ecacd08714822b88.png">
</p>
<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/f82c6228eefd6ff7e0db4a5fc6b77e2b.png">
</p>
<hr>
<p>&nbsp;如果列表是嵌套的列表,同样支持下标索引</p>
  1. #取出嵌套列表的元素
  2. my_list2 = [[1,2,3],[4,5,6]]
  3. print(my_list2[1][1])
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/7a09a890130d66182c785f0a9deae552.png">
</p>

<p>&nbsp;嵌套列表时,被嵌套的列表可以看作一个元素,第一个下标就是确定元素是列表[1,2,3],再用一个下标取出这个被嵌套的列表中的元素</p>

<hr>
<p></p>

<p>总结:</p>
<p style="margin-left:0in; text-align:left">1. 列表的下标索引是什么?</p>
<p style="margin-left:0in; text-align:left">列表的每一个元素,都有编号称之为下标索引</p>
<p style="margin-left:0in; text-align:left">从前向后的方向,编号从0开始递增</p>
<p style="margin-left:0in; text-align:left">从后向前的方向,编号从-1开始递减</p>
<p style="margin-left:0in; text-align:left">2. 如何通过下标索引取出对应位置的元素呢?</p>
<p style="margin-left:0in; text-align:left">列表[下标],即可取出</p>
<p style="margin-left:0in; text-align:left">3. 下标索引的注意事项:</p>

   •
  要注意下标索引的取值范围,超出范围无法取出元素,并且会报错
  

<h4>&nbsp;6.2.3 列表的常用操作(方法)</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ff607ca9c7d1c172a1d7ad35e1e997aa.png">
</p>
<p>&nbsp;演示代码:</p>
<p>1、在列表的尾部追加 ''' 单个 ''' 元素&nbsp; &nbsp; &nbsp; &nbsp;语法:列表.append(元素)</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. mylist.append("CSDN程序员")
  3. print(f"列表在追加了元素后,结果是:{mylist}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4f4b85fc0b0924ef1ee5a75533a6dd58.png">
</p>
<p>2、在列表的尾部追加 ''' 一批 ''' 元素&nbsp; &nbsp; &nbsp; &nbsp; 语法:列表.extend(另一个数据容器)</p>
<p>将另一个数据容器的内容取出,依次加到列表尾部&nbsp;&nbsp;&nbsp;</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. mylist2 = [1, 2, 3]
  3. mylist.extend(mylist2)
  4. print(f"列表在追加了一个新的列表后,结果是:{mylist}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/807d93485655ad1be1f3f96bb7ffd34c.png">
</p>
<p>&nbsp;3、在指定下标位置插入新元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:列表.insert(下标,元素)</p>
<p>在指定的下标位置,插入指定的元素,其余元素向后移动&nbsp;</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. mylist.insert(1, "best")
  3. print(f"列表插入元素后,结果是:{mylist}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/38353ee430d1e4dc133e6dc763d07d1f.png">
</p>
<p>4、删除列表指定下标元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:del 列表[下标]</p>
<p>与pop的区别:仅仅能完成删除操作</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. del mylist[2]
  3. print(f"列表删除元素后结果是:{mylist}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/1aae3b56fc5f4f5889556932940e0439.png">
</p>
<p>&nbsp;5、删除指定下标元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:列表.pop(下标)</p>
<p>与del的区别:不仅能把元素删掉,还能把删除元素作为返回值去得到</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. element = mylist.pop(2)
  3. print(f"通过pop方法取出元素后列表内容:{mylist}, 取出的元素是:{element}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ec06cb47f24de2d4411d289d99e2d628.png">
</p>
<p>&nbsp;6、删除某元素在列表中的第一个匹配项&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:列表.remove(元素)</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. mylist.remove("itlove")
  3. print(f"通过remove方法移除元素后,列表的结果是:{mylist}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/30f9a6545c47919adf40c8fceea14af5.png">
</p>
<p>&nbsp;7、清空列表&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:列表.clear()</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. mylist.clear()
  3. print(f"列表被清空了,结果是:{mylist}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/8f4c6d64ef72e842c24a5b743c830795.png">
</p>
<p>&nbsp;8、统计列表内某元素的数量&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:列表.count(元素)</p>
  1. mylist = ["itCSDN", "itlove", "itlove", "itlove", "python"]
  2. count = mylist.count("itlove")
  3. print(f"列表中itlove的数量是:{count}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/fc4810187e0e8222f6a82b44bb09045b.png">
</p>
<p></p>
<p>9、查找某元素在列表中的下标索引&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:列表.index(元素)</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. index = mylist.index("itlove")
  3. print(f"itlove在列表中的下标索引值是:{index}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/23b20f47fde685d4e8df3f8c8cd9090d.png">
</p>
<p>如果被查找的元素不存在,会报错</p>
<p>10、统计列表中全部的元素数量&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:len(列表)</p>
  1. mylist = ["itCSDN", "itlove", "python"]
  2. count = len(mylist)
  3. print(f"列表的元素数量总共有:{count}个")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/25b2bda6c1093f140ac7009cefe1886f.png">
</p>
<h4>&nbsp;6.2.4 总结列表的特点</h4>


   •
  可以容纳多个元素(上限为
  2**63-1
  、9223372036854775807个)
  

   •
  可以容纳不同类型的元素(混装)
  

   •
  数据是有序存储的(有下标序号)
  

   •
  允许重复数据存在
  

   •
  可以修改(增加或删除元素等)
  

<h4>&nbsp;6.2.5 列表的遍历</h4>

<p style="margin-left:0in; text-align:left">既然数据容器可以存储多个元素,那么,就会有需求从容器内依次取出元素进行操作。</p>
<p style="margin-left:0in; text-align:left"><strong>将容器内的元素依次取出进行处理的行为,称之为:遍历、迭代。</strong></p>

<p><strong>&nbsp;列表的遍历——while循环</strong></p>

<p><strong>index = 0</strong></p>
<p><strong>while index &lt; len(列表) :</strong></p>
<p><strong>&nbsp; &nbsp; 元素 = 列表[index]</strong></p>
<p><strong>&nbsp; &nbsp; 对元素进行处理</strong></p>
<p><strong>&nbsp; &nbsp; index += 1</strong></p>

<p>&nbsp;<strong>列表的遍历——for循环</strong></p>
<p>对比while,for循环更加适合对列表等数据容器进行遍历。</p>

<p>for 临时变量 in 数据容器 :</p>
<p>&nbsp; &nbsp; 对临时变量进行处理</p>


<p style="margin-left:0in; text-align:left">for循环和while对比</p>

   •
  for
  循环更简单,
  while
  更灵活
  

   •
  for
  用于从容器内依次取出元素并处理,
  while
  用以任何需要循环的场景
  

<hr>
<p></p>
<h3>&nbsp;6.3 tuple 元组</h3>

<p style="margin-left:0in; text-align:left"><strong>元组一旦定义完成,就不可修改</strong></p>

<h4>&nbsp;6.3.1 元组的定义</h4>
<p style="margin-left:0in; text-align:left">元组定义:定义元组使用<strong>小括号</strong>,且使用逗号隔开各个数据,数据可以是不同的数据类型。</p>

<p style="margin-left:0in; text-align:left"></p>
  1. #定义元组字面量
  2. (元素,元素,......,元素)

  3. #定义元组数量
  4. 变量名称 = (元素,元素,...... ,元素)

  5. #定义空元组
  6. 变量名称 = ()          方式一
  7. 变量名称 = tuple()     方式二
复制代码


<p><strong>&nbsp;注:1、元组只有一个数据时,这个数据后面要添加逗号,否则不是元组类型!</strong>&nbsp;</p>
<p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2、元组也支持嵌套</strong></p>
<p>演示代码:</p>
  1. t2 = ("Hello",)
  2. print(f"t2的类型是:{type(t2)},t2的内容是:{t2}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/23b10a9283f1dbe37b498ab5c24c6238.png">
</p>
<h4>&nbsp;6.3.2 元组的相关操作</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/75727f49aa6b6631893f60eefeedf1ce.png">
</p>
<p>&nbsp;元组由于不可修改的特性,所以其操作方法非常少。</p>

<p>&nbsp;元组的三种方法和列表相对应的这三种方法操作一致,这里就不再讲解,若有疑问,请私信博主或者移步前文列表方法查看</p>

<h4>&nbsp;6.3.3 元组的遍历</h4>
<p>元组同样支持while循环和for循环的遍历操作</p>
<p>用法和列表遍历一致,请移步前文列表遍历处查看</p>
<h4>6.3.4总结元组的特点</h4>


   •
  可以容纳多个数据
  

   •
  可以容纳不同类型的数据(混装)
  

   •
  数据是有序存储的(下标索引)
  

   •
  允许重复数据存在
  

   •
  不可以修改
  (增加或删除元素等)
  

   •
  支持for循环
  

<p style="margin-left:0in; text-align:left">多数特性和list一致,不同点在于不可修改的特性。</p>
<p style="margin-left:0in; text-align:left">但如果元组中嵌套的有列表,那么列表中的元素可以修改(列表list的本质没有改变,所以不违背元组不可以修改的原则)</p>
<p style="margin-left:0in; text-align:left">演示代码:</p>
  1. t4 = (1,2,['青灯古刹','刹那芳华'])
  2. t4[2][0] = '黄粱一梦'
  3. t4[2][1] = '大梦初醒'
  4. print(t4)
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/dfcb33c1e9a88529f1f0c9d3d3d150d5.png">
</p>
<hr>
<p></p>
<h3>&nbsp;6.4 str 字符串</h3>

<p style="margin-left:0in; text-align:left">&nbsp;字符串是字符的容器,一个字符串可以存放任意数量的字符。</p>
<p style="margin-left:0in; text-align:left">&nbsp;同元组一样,字符串是一个:无法修改的数据容器。</p>

<h4 style="margin-left:0in; text-align:left">6.4.1 字符串的下标索引</h4>

<p style="margin-left:0in; text-align:left">和其它容器如:列表、元组一样,字符串也可以通过下标进行访问</p>

   •
  从前向后,下标从
  0
  开始
  

   •
  从后向前,下标从
  -1
  开始
  

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/cb477ea9d31cfaa2aeef0358664cb268.png">
</p>
<h4>6.4.2 字符串的常用操作(方法)</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/1e7e246092d4c35fcbfa4a2584491bb6.png">
</p>
<p><strong>&nbsp;1、根据下标索引取出特定位置字符&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:字符串[下标]</strong></p>
  1. str = "mountain and sea"
  2. str1 = str[3]
  3. str2 = str[-2]
  4. print(f"从字符串{str}取下标为3的元素,值是{str1},取下标为-2的元素,值是{str2}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/d7741a6c3eaaf69cced65da14db05f80.png">
</p>
<p><strong>&nbsp;2、查找给定字符的第一个匹配项的下标&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:字符串.index(字符串)</strong></p>
  1. str = "mountain and sea"
  2. value = str.index("and")
  3. print(f"在字符串{str}中查找“and”,其起始下标是{value}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/2ea5c199c516db7eed0ef90761b70377.png">
</p>
<p><strong>&nbsp;3、将字符串1替换为字符串2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:字符串.replace(字符串1,字符串2)</strong></p>
<p>注意:不是修改字符串本身,而是得到了一个新字符串</p>
  1. str = "itmountain and itsea"
  2. new_str = str.replace("it","程序")
  3. print(f"将字符串 {str},进行替换得到 {new_str}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/61431a49b64cfecfe42c3b70b49027b2.png">
</p>
<p><strong>&nbsp;4、按照指定的分隔符字符串,将字符串划分为多个字符串,并存入列表对象中&nbsp; </strong>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;语法:字符串.spilt(分隔符字符串)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;注意:字符串本身不变,而是得到了一个新的列表对象</p>
  1. my_str = "hello python itCSDN itlove"
  2. my_str_list = my_str.split(" ")
  3. print(f"将字符串{my_str}进行split切分后得到:{my_str_list}, 类型是:{type(my_str_list)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/a5990078321508f49ebefcb94c69b744.png">
</p>
<p><strong>&nbsp;5、字符串的规整操作(移除首尾的空格和换行符或指定字符串)&nbsp;&nbsp;</strong></p>
<p><strong>&nbsp;语法1:字符串.strip()&nbsp; &nbsp; &nbsp; &nbsp; 不传入参数时,去除首尾空格</strong></p>
  1. my_str = "  itCSDN and itlove  "
  2. new_my_str = my_str.strip() # 不传入参数,去除首尾空格
  3. print(f"字符串{my_str}被strip后,结果:{new_my_str}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4dedc12aa9bea0232778df12351102e6.png">
</p>
<p><strong>&nbsp;语法2:字符串.strip(字符串)</strong></p>
<p><strong>注意:传入的是"12",其实就是:"1"和"2"都会移除,是按照单个字符</strong></p>
  1. my_str = "12itCSDN and itlove21"
  2. new_my_str = my_str.strip("12")
  3. print(f"字符串{my_str}被strip('12')后,结果:{new_my_str}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/82ff49f2984d13e373650d7f60c3c051.png">
</p>
<p><strong>&nbsp;6、统计字符串内某字符或字符串的出现次数&nbsp; &nbsp; &nbsp; 语法:字符串.count(字符串)</strong></p>
  1. my_str = "itCSDN and itlove"
  2. count = my_str.count("it")
  3. print(f"字符串{my_str}中it出现的次数是:{count}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/1979674eba8a0821ee01bc702570fb6a.png">
</p>
<p><strong>&nbsp;7、统计字符串的字符个数&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:len(字符串)</strong></p>
  1. my_str = "itCSDN and itlove"
  2. num = len(my_str)
  3. print(f"字符串{my_str}的长度是:{num}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/120ed52f5a73847bed69f8a2ced6806e.png">
</p>
<h4>6.4.3 总结字符串的特点</h4>

<p style="margin-left:0in; text-align:left">作为数据容器,字符串有如下特点:</p>

   •
  只可以存储字符串
  

   •
  长度任意(取决于内存大小)
  

   •
  支持下标索引
  

   •
  允许重复字符串存在
  

   •
  不可以修改
  (增加或删除元素等)
  

   •
  支持for循环
  

<hr>
<p></p>
<h3>&nbsp;6.5 序列</h3>

<p style="margin-left:0in; text-align:left">序列是指:内容连续、有序,可使用下标索引的一类数据容器</p>
<p style="margin-left:0in; text-align:left">列表、元组、字符串,均可以可以视为序列。</p>

<h4>&nbsp;6.5.1 序列的常用操作——切片</h4>

<p style="margin-left:0in; text-align:left">序列支持切片,即:列表、元组、字符串,均支持进行切片操作</p>
<p style="margin-left:0in; text-align:left">切片:从一个序列中,取出一个子序列</p>

<p style="margin-left:0in; text-align:left"><strong>语法:序列[起始下标:结束下标:步长]</strong></p>
<p style="margin-left:0in; text-align:left">表示从序列中,从指定位置开始,依次取出元素,到指定位置结束,得到一个新序列:</p>


   •
  起始下标表示从何处开始,可以留空,留空视作从头开始
  

   •
  结束下标(不含)表示何处结束,可以留空,留空视作截取到结尾
  

   •
  步长表示,依次取元素的间隔
  

   •
  <strong>步长</strong>
  <strong>1</strong>
  <strong>表示,一个个取元素</strong>
  

   •
  <strong>步长</strong>
  <strong>2</strong>
  <strong>表示,每次跳过</strong>
  <strong>1</strong>
  <strong>个元素取</strong>
  

   •
  <strong>步长</strong>
  <strong>N</strong>
  <strong>表示,每次跳过</strong>
  <strong>N-1</strong>
  <strong>个元素取</strong>
  

   •
  <strong>步长为负数表示,反向取(注意,起始下标和结束下标也要反向标记)</strong>
  

<p style="margin-left:0in; text-align:left">注意,此操作不会影响序列本身,而是会得到一个新的序列(列表、元组、字符串)</p>
<p>&nbsp;注:可以用此方法倒序字符串等序列(步长设为-1)</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ca302776ac90d682700b4c6f15641ee5.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/f7956543089a64210b13cfdec216a947.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/44ab8c203eab65e464f67b3e8246da77.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/29f2f2b6498c474b75762851dfe02720.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/5d804b0d7b9116ea8d6c8eca807fedaa.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ee91dba69b3b0020cca6d0ddba9097be.png">
</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/f6baa572cab4edd64558336f99725761.png">
</p>
<p style="margin-left:0in; text-align:left"><strong>可以看到,这个操作对列表、元组、字符串是通用的</strong></p>
<p style="margin-left:0in; text-align:left"><strong>同时非常灵活,根据需求,起始位置,结束位置,步长(正反序)都是可以自行控制的</strong></p>
<hr>
<p></p>
<h3>&nbsp;6.6 set 集合</h3>
<h4>6.6.1 集合的特点</h4>


   •
  集合内不允许重复元素(去重)
  

   •
  集合内元素是无序的(不支持下标索引)
  

<h4>&nbsp;6.6.2&nbsp;集合的定义</h4>
<p>&nbsp;
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/8beb657b57a8ca0cee09d1864f8edbc1.png">
</p>
<p>&nbsp;结合集合特点进行代码演示:</p>
  1. my_set_empty = set()        # 定义空集合
  2. print(f"my_set_empty的内容是:{my_set_empty}, 类型是:{type(my_set_empty)}")
  3. my_set = {"CSDN程序员社区", "CSDN灌水乐园", "itCSDN", "CSDN灌水乐园", "CSDN程序员社区", "itCSDN"}
  4. print(f"my_set的内容是:{my_set}, 类型是:{type(my_set)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/6cd191af46b05b8c0d434dcc9975e84d.png">
</p>
<h4>&nbsp;6.6.3 集合的常用操作</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/06731a6e8bb67794d6c62aa20ccbbdd1.png">
</p>
<p><strong>1、添加新元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:集合.add(元素)</strong></p>
  1. my_set = {"CSDN程序员社区", "CSDN灌水乐园", "itCSDN"}
  2. my_set.add("Python")
  3. my_set.add("CSDN程序员社区")
  4. print(f"my_set添加元素后结果是:{my_set}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/da2e19142bd531b60f4942cbe6434f07.png">
</p>

<p>注意1:当集合本身就含有被添加的元素时,则添加无效(去重)</p>
<p>注意2:因为集合本身没有顺序,所以被添加元素位置随机</p>

<p>&nbsp;<strong>2、移除集合内指定的元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:集合.remove(元素)</strong></p>
  1. my_set = {"CSDN程序员社区", "CSDN灌水乐园", "itCSDN"}
  2. my_set.remove("CSDN程序员社区")
  3. print(f"my_set移除黑马程序员后,结果是:{my_set}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/f3eb555f97c1616662b8e18aa76bf9a8.png">
</p>
<p><strong>&nbsp;3、从集合中随机取出一个元素,原集合此元素会被删除&nbsp; &nbsp; &nbsp;语法:集合.pop()</strong></p>
  1. my_set = {"CSDN程序员社区", "CSDN灌水乐园", "itCSDN"}
  2. element = my_set.pop()
  3. print(f"集合被取出元素是:{element}, 取出元素后:{my_set}")
复制代码

<p><strong>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/237593f0ab838ef752bb034c5da178f2.png">
</strong></p>
<p><strong>&nbsp;4、清空集合&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:集合.clear()</strong></p>
  1. my_set = {"CSDN程序员社区", "CSDN灌水乐园", "itCSDN"}
  2. my_set.clear()
  3. print(f"集合被清空啦,结果是:{my_set}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/896fcfdc4a124f1438a228333acd2700.png">
</p>
<p><strong>&nbsp;5、取两个集合的差集(得到一个新集合)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:集合1.difference(集合2)</strong></p>
  1. set1 = {1, 2, 3}
  2. set2 = {1, 5, 6}
  3. set3 = set1.difference(set2)
  4. print(f"取出差集后的结果是:{set3}")
  5. print(f"取差集后,原有set1的内容:{set1}")
  6. print(f"取差集后,原有set2的内容:{set2}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/992868e95b560a122bfde96c0000eaa9.png">
</p>
<p><strong>&nbsp;6、消除两个集合的差集,集合1被修改,集合2不变&nbsp; &nbsp; &nbsp;语法:集合1.difference_update(集合2)</strong></p>
  1. set1 = {1, 2, 3}
  2. set2 = {1, 5, 6}
  3. set1.difference_update(set2)
  4. print(f"消除差集后,集合1结果:{set1}")
  5. print(f"消除差集后,集合2结果:{set2}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/a6a7b5ccf799acf8658c9a96e9c60e14.png">
</p>
<p>7、 <strong>得到一个新的集合,内含两个集合的所有元素,原有的两个集合不变&nbsp; &nbsp; &nbsp; </strong></p>
<p><strong>&nbsp; &nbsp; &nbsp; 语法:集合1.union(集合2)</strong></p>
  1. set1 = {1, 2, 3}
  2. set2 = {1, 5, 6}
  3. set3 = set1.union(set2)
  4. print(f"2集合合并结果:{set3}")
  5. print(f"合并后集合1:{set1}")
  6. print(f"合并后集合2:{set2}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/82f8600eac5f4fe4f9cb41b0dcc04f3c.png">
</p>
<p><strong>&nbsp;8、统计集合的去重后的元素数量&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:len(集合)&nbsp; &nbsp;&nbsp;</strong></p>
  1. set1 = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}
  2. num = len(set1)
  3. print(f"集合内的元素数量有:{num}个")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/43f26aa9872759e7c04b59bcfd830944.png">
</p>
<h4>6.6.4 集合的遍历&nbsp;</h4>

<p style="margin-left:0in; text-align:left">集合同样支持使用for循环遍历</p>
<p style="margin-left:0in; text-align:left">要注意:集合<strong>不支持下标索引</strong>,所以也就<strong>不支持使用</strong><strong>while</strong><strong>循环</strong>。</p>

  1. set1 = {1, 2, 3, 4, 5}
  2. for element in set1:
  3.     print(f"集合的元素有:{element}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/33a66428204ad22b2f89b42c7db18aef.png">
</p>
<h4>6.6.5&nbsp;集合的特点</h4>
<p style="margin-left:0in; text-align:left">经过上述对集合的学习,可以总结出集合有如下特点:</p>


   •
  可以容纳多个数据
  

   •
  可以容纳不同类型的数据(混装)
  

   •
  数据是无序存储的(不支持下标索引)
  

   •
  不允许重复数据存在
  

   •
  可以修改
  (增加或删除元素等)
  

   •
  支持for循环
  

<hr>
<p></p>
<h3>6.7 字典</h3>
<h4>6.7.1 字典的定义</h4>

<p>通过 key 找出对应的 value&nbsp;</p>

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/dc3582e40d38fabdb40394d68f6ee1b7.png">
</p>
<p>&nbsp;字典的定义,同样使用{},不过存储的元素是一个个的:键值对,如下语法:</p>


   •
  使用
  {}
  存储原始,每一个元素是一个键值对
  

   •
  每一个键值对包含
  Key
  和
  Value
  (用冒号分隔)
  

   •
  键值对之间使用逗号分隔
  

   •
  Key
  和
  Value
  可以是任意类型的数据(
  key
  不可为字典)
  

   •
  Key
  不可重复,重复会对原有数据覆盖
  

   •字典不可用下标索引,而是通过Key检索Value
  

<p>&nbsp;键值对&nbsp; &nbsp;key :value 三者结合被称为键值对</p>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/88fa9428fb7f598d6051f671f6e8189b.png">
</p>

<p style="margin-left:0in; text-align:left">字典同集合一样,不可以使用下标索引</p>
<p style="margin-left:0in; text-align:left">但是字典可以通过Key值来取得对应的Value</p>

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/efc1b49a058d6c82f8fa34f92693357d.png">
</p>
<p>&nbsp;演示代码:</p>
  1. # 定义字典
  2. my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77}
  3. # 定义空字典
  4. my_dict2 = {}
  5. my_dict3 = dict()
  6. print(f"字典1的内容是:{my_dict1}, 类型:{type(my_dict1)}")
  7. print(f"字典2的内容是:{my_dict2}, 类型:{type(my_dict2)}")
  8. print(f"字典3的内容是:{my_dict3}, 类型:{type(my_dict3)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/f7663e41f76c50d9a5ee11fe00ece6a4.png">
</p>
<p></p>
  1. # 从字典中基于Key获取Value
  2. my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77}
  3. score = my_dict1["王力鸿"]
  4. print(f"王力鸿的考试分数是:{score}")
  5. score = my_dict1["周杰轮"]
  6. print(f"周杰轮的考试分数是:{score}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/d0b362ffdd4a652ab9e9c94a4beb3183.png">
</p>
<h4>&nbsp;6.7.2 字典的嵌套</h4>

<p style="margin-left:0in; text-align:left">字典的Key和Value可以是任意数据类型(Key不可为字典)</p>
<p style="margin-left:0in; text-align:left">那么,就表明,字典是可以嵌套的</p>

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/15cdeb85db7affb347ac8b60ed442ea2.png">
</p>
<p>演示代码:</p>
  1. # 定义嵌套字典
  2. stu_score_dict = {    #回车符对字典间的元素无影响
  3.     "王力鸿": {
  4.         "语文": 77,
  5.         "数学": 66,
  6.         "英语": 33
  7.     }, "周杰轮": {
  8.         "语文": 88,
  9.         "数学": 86,
  10.         "英语": 55
  11.     }, "林俊节": {
  12.         "语文": 99,
  13.         "数学": 96,
  14.         "英语": 66
  15.     }
  16. }
  17. print(f"学生的考试信息是:{stu_score_dict}")

  18. # 从嵌套字典中获取数据
  19. # 看一下周杰轮的语文信息
  20. score = stu_score_dict["周杰轮"]["语文"]
  21. print(f"周杰轮的语文分数是:{score}")
  22. score = stu_score_dict["林俊节"]["英语"]
  23. print(f"林俊节的英语分数是:{score}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4dd8c5a8be14bca92fba458876adb284.png">
</p>
<h4>&nbsp;6.7.3 字典的常用操作</h4>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/2754d495b4c632bd314d6b266a18be01.png">
</p>
<p><strong>&nbsp;1、获取指定Key对应的Value值&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:字典[key]</strong></p>
  1. my_dict1 = {"王力鸿": 99, "周杰轮": 88, "林俊节": 77}
  2. score = my_dict1["王力鸿"]
  3. print(f"王力鸿的考试分数是:{score}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/bd503057a97f08c8fce009828ab70ba5.png">
</p>
<p><strong>&nbsp;2、添加或更新键值对&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:字典[key] = Value</strong></p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. # 新增元素
  3. my_dict["张信哲"] = 66
  4. print(f"字典经过新增元素后,结果:{my_dict}")
  5. #更新元素
  6. my_dict["周杰轮"] = 33
  7. print(f"字典经过更新后,结果:{my_dict}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/a5cd128d50eb9af5da575e05f1d8af83.png">
</p>
<p><strong>&nbsp;3、取出Key对应的Value并在字典内删除此Key的键值对&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:字典.pop(key)</strong></p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. score = my_dict.pop("周杰轮")
  3. print(f"字典中被移除了一个元素,结果:{my_dict}, 周杰轮的考试分数是:{score}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ec0360ba45d791ce919421b8e01bcb53.png">
</p>
<p><strong>&nbsp;4、清空字典&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:字典.clear</strong></p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. my_dict.clear()
  3. print(f"字典被清空了,内容是:{my_dict}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/672079a88534fb60d4a6246dd4705cc0.png">
</p>
<p><strong>&nbsp;5、获取字典的全部Key,可用于for循环遍历字典&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:字典.key</strong></p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. keys = my_dict.keys()
  3. print(f"字典的全部keys是:{keys}")
  4. # 遍历字典
  5. # 方式1:通过获取到全部的key来完成遍历
  6. for key in keys:
  7.     print(f"字典的key是:{key}")
  8.     print(f"字典的value是:{my_dict[key]}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/873d4ed2edf4d6ef8e93ee5fda613cdb.png">
</p>
<p>&nbsp;6、计算字典内的元素数量</p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. num = len(my_dict)
  3. print(f"字典中的元素数量有:{num}个")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/ddca4efd1305fd1f80fe83d16930c20f.png">
</p>
<h4>&nbsp;6.7.4 字典的遍历</h4>
<p><strong>方法1:通过获取到全部的key来完成遍历</strong></p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. keys = my_dict.keys()
  3. print(f"字典的全部keys是:{keys}")
  4. # 遍历字典
  5. # 方式1:通过获取到全部的key来完成遍历
  6. for key in keys:
  7.     print(f"字典的key是:{key}")
  8.     print(f"字典的value是:{my_dict[key]}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/873d4ed2edf4d6ef8e93ee5fda613cdb.png">
</p>
<p><strong>方法2:直接对字典进行for循环,每一次循环都是直接得到key</strong></p>
  1. my_dict = {"周杰轮": 99, "林俊节": 88, "张学油": 77}
  2. for key in my_dict:
  3.     print(f"字典的key是:{key}")
  4.     print(f"字典的value是:{my_dict[key]}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/e796817c39d3f38e4c3ea1e44b1d4a81.png">
</p>
<h4>&nbsp;6.7.5 字典的特点</h4>
<p style="margin-left:0in; text-align:left">经过上述对字典的学习,可以总结出字典有如下特点:</p>


   •
  可以容纳多个数据
  

   •
  可以容纳不同类型的数据
  

   •
  每一份数据是
  KeyValue
  键值对
  

   •
  可以通过
  Key
  获取到
  Value
  ,
  Key
  不可重复(重复会覆盖)
  

   •
  不支持下标索引
  

   •
  可以修改
  (增加或删除更新元素等)
  

   •
  支持for循环,不支持
  while
  循环
  

<hr>
<p></p>
<h3>6.8 数据容器对比总结</h3>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/78053d2b10e9db4cd431feae0d7338d5.png">
</p>

  
  基于各类数据容器的特点,它们的应用场景如下:
  

   •
  列表:一批数据,可修改、可重复的存储
  场景
  

   •
  元组:一批数据,不可修改、可重复的存储
  场景
  

   •
  字符串:一串字符串的存储
  场景
  

   •
  集合:一批数据,去重存储
  场景
  

   •
  字典:一批数据,可用
  Key
  检索
  Value
  的存储场景
  



   •
  列表使用:
  []
  

   •
  元组使用:
  ()
  

   •
  字符串使用:""
  

   •
  集合使用:
  {}
  

   •字典使用:{}和键值对
  

<hr>
<h3>6.9 数据容器的通用操作及相互转换</h3>
<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4edeff1fdf0b6840fc717cb57d99ade6.png">
</p>
  1. my_list = [1, 2, 3, 4, 5]
  2. my_tuple = (1, 2, 3, 4, 5)
  3. my_str = "abcdefg"
  4. my_set = {1, 2, 3, 4, 5}
  5. my_dict = {"key1": 1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
复制代码

<h4><strong>6.9.1 数据容器的遍历</strong></h4>
<p>此处不再讲,前面非常详细、</p>
<h4><strong>6.9.2 容器内最大元素&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:max(数据容器名)</strong></h4>
  1. # max最大元素
  2. print(f"列表 最大的元素是:{max(my_list)}")
  3. print(f"元组 最大的元素是:{max(my_tuple)}")
  4. print(f"字符串最大的元素是:{max(my_str)}")
  5. print(f"集合 最大的元素是:{max(my_set)}")
  6. print(f"字典 最大的元素是:{max(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/74c74f6575c1b4be4b8fa9c3c090ea4b.png">
</p>
<h4>&nbsp;6.9.3 容器内最小元素&nbsp; &nbsp; &nbsp; &nbsp;语法:<strong>min(数据容器名)</strong></h4>
  1. # min最小元素
  2. print(f"列表 最小的元素是:{min(my_list)}")
  3. print(f"元组 最小的元素是:{min(my_tuple)}")
  4. print(f"字符串最小的元素是:{min(my_str)}")
  5. print(f"集合 最小的元素是:{min(my_set)}")
  6. print(f"字典 最小的元素是:{min(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/e8b3c25746827ee518aee79f82f49eb1.png">
</p>
<h4>&nbsp;6.9.4 计算容器内元素个数&nbsp; &nbsp; &nbsp; &nbsp; 语法:len(数据容器名)</h4>
  1. # len元素个数
  2. print(f"列表 元素个数有:{len(my_list)}")
  3. print(f"元组 元素个数有:{len(my_tuple)}")
  4. print(f"字符串元素个数有:{len(my_str)}")
  5. print(f"集合 元素个数有:{len(my_set)}")
  6. print(f"字典 元素个数有:{len(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/fc33905686059fc3da1244d7898c1875.png">
</p>
<h4>&nbsp;6.9.5 数据容器转列表&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 语法:list(数据容器名)</h4>
<p>字典转列表时会丢掉value值</p>
  1. # 类型转换: 容器转列表
  2. print(f"列表转列表的结果是:{list(my_list)}")
  3. print(f"元组转列表的结果是:{list(my_tuple)}")
  4. print(f"字符串转列表结果是:{list(my_str)}")
  5. print(f"集合转列表的结果是:{list(my_set)}")
  6. print(f"字典转列表的结果是:{list(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/dbdc76efbbf9359dcb84a1659c497529.png">
</p>
<h4>&nbsp;6.9.6 数据容器转元组&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:tuple(数据容器名)</h4>
<p>字典转元组时会丢掉value值</p>
  1. # 类型转换: 容器转元组
  2. print(f"列表转元组的结果是:{tuple(my_list)}")
  3. print(f"元组转元组的结果是:{tuple(my_tuple)}")
  4. print(f"字符串转元组结果是:{tuple(my_str)}")
  5. print(f"集合转元组的结果是:{tuple(my_set)}")
  6. print(f"字典转元组的结果是:{tuple(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/2012deb6ede8204bd1026dc181f07fcb.png">
</p>
<h4>&nbsp;6.9.7&nbsp;数据容器转字符串&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:str(数据容器名)</h4>
  1. # 类型转换: 容器转字符串
  2. print(f"列表转字符串的结果是:{str(my_list)}")
  3. print(f"元组转字符串的结果是:{str(my_tuple)}")
  4. print(f"字符串转字符串结果是:{str(my_str)}")
  5. print(f"集合转字符串的结果是:{str(my_set)}")
  6. print(f"字典转字符串的结果是:{str(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/978e4bda74573ee6565ef2a0123788b8.png">
</p>
<h4>&nbsp;6.9.8 数据容器转集合&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;语法:set(数据容器名)</h4>
  1. # 类型转换: 容器转集合
  2. print(f"列表转集合的结果是:{set(my_list)}")
  3. print(f"元组转集合的结果是:{set(my_tuple)}")
  4. print(f"字符串转集合结果是:{set(my_str)}")
  5. print(f"集合转集合的结果是:{set(my_set)}")
  6. print(f"字典转集合的结果是:{set(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/faf2b64c207619e97243afcf70048651.png">
</p>
<h4>&nbsp;6.9.9 容器排序 可正序也可倒序</h4>
  1. # 进行容器的排序
  2. my_list = [3, 1, 2, 5, 4]
  3. my_tuple = (3, 1, 2, 5, 4)
  4. my_str = "bdcefga"
  5. my_set = {3, 1, 2, 5, 4}
  6. my_dict = {"key3": 1, "key1": 2, "key2": 3, "key5": 4, "key4": 5}
复制代码

<p>1、正序排列&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:sorted(数据容器名)</p>
  1. print(f"列表对象的排序结果:{sorted(my_list)}")
  2. print(f"元组对象的排序结果:{sorted(my_tuple)}")
  3. print(f"字符串对象的排序结果:{sorted(my_str)}")
  4. print(f"集合对象的排序结果:{sorted(my_set)}")
  5. print(f"字典对象的排序结果:{sorted(my_dict)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/4c59eaf7917affaf981fb9ec56471188.png">
</p>
<p>&nbsp;2、逆序排列&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;语法:sorted(数据容器名,reverse = True)</p>
  1. print(f"列表对象的反向排序结果:{sorted(my_list, reverse=True)}")
  2. print(f"元组对象的反向排序结果:{sorted(my_tuple, reverse=True)}")
  3. print(f"字符串对象反向的排序结果:{sorted(my_str, reverse=True)}")
  4. print(f"集合对象的反向排序结果:{sorted(my_set, reverse=True)}")
  5. print(f"字典对象的反向排序结果:{sorted(my_dict, reverse=True)}")
复制代码

<p>
<img alt="" src="https://i-blog.csdnimg.cn/blog_migrate/0a1d7243a8c28c0c9ff8f7e9f7292ed7.png">
</p>
<hr>
<p></p>
<h2>&nbsp;结尾留言</h2>
<p>尊敬的读者,您好!</p>
<p>当你将这篇博客看完并加以实践后,Python的语法到这里也告了一段落</p>
<p>后续博主会不定时对这篇文章加以补充并往后续写Python新的内容</p>
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行