文章列表


<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的条件语句和循环语句是控制流程的重要组成部分,可以帮助我们实现不同的逻辑和算法。下面分别介绍一下Python中常见的条件语句和循环语句。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">条件语句</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中常见的条件语句包括if语句和if-else语句。if语句用于测试一个条件,如果条件为真则执行一段代码块,否则不执行。if-else语句在if语句的基础上增加了一个else分支,用于在条件为假时执行另一段代码块。</p><pre class="brush:as3;toolbar:false">#&nbsp;if语句示例 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) #&nbsp;if-else语句示例 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;y&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中还支持使用elif关键字来增加多个条件分支。elif语句在if语句和else语句之间使用,用于测试前面的条件不满足时的另一种情况。</p><pre class="brush:as3;toolbar:false">#&nbsp;if-elif-else语句示例 if&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;greater&nbsp;than&nbsp;y&quot;) elif&nbsp;x&nbsp;==&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;equal&nbsp;to&nbsp;y&quot;) else: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;y&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">循环语句</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中常见的循环语句包括for循环和while循环。for循环用于遍历一个序列或迭代器中的元素,而while循环用于在条件为真时重复执行一段代码块。</p><pre class="brush:as3;toolbar:false">#&nbsp;for循环示例 for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;11): &nbsp;&nbsp;&nbsp;&nbsp;print(i) #&nbsp;while循环示例 x&nbsp;=&nbsp;10 while&nbsp;x&nbsp;&gt;&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;print(x) &nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;-=&nbsp;1</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中还支持使用break和continue关键字来控制循环的执行。break语句用于在循环中终止执行,而continue语句用于跳过当前迭代并继续执行下一次迭代。</p><pre class="brush:as3;toolbar:false">#&nbsp;break语句示例 for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;11): &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;==&nbsp;5: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break &nbsp;&nbsp;&nbsp;&nbsp;print(i) #&nbsp;continue语句示例 for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;11): &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue &nbsp;&nbsp;&nbsp;&nbsp;print(i)</pre><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的逻辑运算符包括and、or和not,用于连接和操作布尔值。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">and运算符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">and运算符用于连接两个布尔表达式,当两个表达式都为真时,and运算符的结果为真,否则结果为假。如果第一个表达式的结果为假,则不会计算第二个表达式。</p><pre class="brush:as3;toolbar:false">#&nbsp;and运算符示例 x&nbsp;=&nbsp;5 y&nbsp;=&nbsp;10 z&nbsp;=&nbsp;15 if&nbsp;x&nbsp;&lt;&nbsp;y&nbsp;and&nbsp;y&nbsp;&lt;&nbsp;z: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;y&nbsp;is&nbsp;between&nbsp;x&nbsp;and&nbsp;z&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">or运算符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">or运算符用于连接两个布尔表达式,当两个表达式中至少有一个为真时,or运算符的结果为真,否则结果为假。如果第一个表达式的结果为真,则不会计算第二个表达式。</p><pre class="brush:as3;toolbar:false">#&nbsp;or运算符示例 x&nbsp;=&nbsp;5 y&nbsp;=&nbsp;10 z&nbsp;=&nbsp;15 if&nbsp;x&nbsp;&lt;&nbsp;y&nbsp;or&nbsp;y&nbsp;&gt;&nbsp;z: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;less&nbsp;than&nbsp;y&nbsp;or&nbsp;y&nbsp;is&nbsp;greater&nbsp;than&nbsp;z&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">not运算符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">not运算符用于对一个布尔表达式取反,即如果表达式的结果为真,则not运算符的结果为假,反之亦然。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"># not运算符示例</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;5 y&nbsp;=&nbsp;10 if&nbsp;not&nbsp;x&nbsp;&gt;&nbsp;y: &nbsp;&nbsp;&nbsp;&nbsp;print(&quot;x&nbsp;is&nbsp;not&nbsp;greater&nbsp;than&nbsp;y&quot;)</pre><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">需要注意的是,逻辑运算符在表达式中的优先级低于比较运算符和算术运算符,因此需要使用括号来明确运算的顺序。</p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的输入输出使用内置函数input()和print()来实现。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">input()函数用于从控制台读取用户输入的数据,并将其作为字符串返回。例如:</p><pre class="brush:as3;toolbar:false">name&nbsp;=&nbsp;input(&quot;请输入您的姓名:&quot;) print(&quot;您好,&quot;&nbsp;+&nbsp;name&nbsp;+&nbsp;&quot;!&quot;)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">输出:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">请输入您的姓名:Tom 您好,Tom!</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print()函数用于将指定的内容输出到控制台。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print(&quot;Hello, world!&quot;)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">输出:</p><pre class="brush:as3;toolbar:false">Copy&nbsp;code Hello,&nbsp;world!</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,可以使用数学运算符来执行各种数学运算。下面是一些常用的数学运算符:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">加法:+</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">减法:-</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">乘法:*</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">除法:/</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">取模:%</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">取整除://</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">例如:</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;10 y&nbsp;=&nbsp;3 print(x&nbsp;+&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;13 print(x&nbsp;-&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;7 print(x&nbsp;*&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;30 print(x&nbsp;/&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;3.3333333333333335 print(x&nbsp;%&nbsp;y)&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;1 print(x&nbsp;//&nbsp;y)&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;3</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">还有一些其他的数学函数可以使用,这些函数都是在Python的数学模块(math)中定义的,例如:</p><pre class="brush:as3;toolbar:false">import&nbsp;math print(math.sqrt(16))&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;4.0 print(math.pow(2,&nbsp;3))&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;8.0 print(math.pi)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;3.141592653589793</pre><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python标识符</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,标识符是指用于命名变量、函数、类或模块等对象的名称。标识符可以包含字母、数字和下划线(_),但是不能以数字开头。标识符还需要遵循以下规则:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">标识符必须是一个有效的标识符名称,不能与Python中的关键字重复(如if、else、for等)。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">标识符区分大小写,因此myVar和myvar是不同的标识符。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">标识符应该使用驼峰命名法或下划线命名法,以提高代码可读性。例如,my_variable或MyVariable。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">下面是一些有效的标识符示例:</p><pre class="brush:as3;toolbar:false">my_variable&nbsp;=&nbsp;42 myClass&nbsp;=&nbsp;MyClass() my_function&nbsp;=&nbsp;lambda&nbsp;x:&nbsp;x&nbsp;*&nbsp;2 Python关键字</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的关键字是一组保留的标识符,用于标识语言的关键部分。这些关键字不能用于命名变量、函数、类或模块等对象。以下是Python的关键字列表:</p><pre class="brush:as3;toolbar:false">False,&nbsp;None,&nbsp;True,&nbsp;and,&nbsp;as,&nbsp;assert,&nbsp;async,&nbsp;await,&nbsp;break, &nbsp;class,&nbsp;continue,&nbsp;def,&nbsp;del,&nbsp;elif,&nbsp;else,&nbsp;except,&nbsp;finally,&nbsp; &nbsp;for,&nbsp;from,&nbsp;global,&nbsp;if,&nbsp;import,&nbsp;in,&nbsp;is,&nbsp;lambda,&nbsp;nonlocal, &nbsp;&nbsp;not,&nbsp;or,&nbsp;pass,&nbsp;raise,&nbsp;return,&nbsp;try,&nbsp;while,&nbsp;with,&nbsp;yield</pre><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python是一种解释型的高级编程语言,它的数据类型包括整型、浮点型、字符串、列表、元组、字典和集合等。在这里,我们将详细介绍Python数据类型列表和元组以及它们的转换。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表(List)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表是Python中最常用的数据类型之一。它是一个有序的可变序列,可以容纳多个不同类型的元素。列表用方括号([])表示,其中的元素用逗号隔开。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits&nbsp;=&nbsp;[&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;]</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表中的元素可以是任何类型的数据,包括整型、浮点型、字符串、甚至是其他的列表。列表是可变的,这意味着我们可以随时添加、删除或修改列表中的元素。例如:</p><pre class="brush:as3;toolbar:false">fruits.append(&#39;pear&#39;)&nbsp;&nbsp;#&nbsp;在列表末尾添加元素 fruits.insert(2,&nbsp;&#39;grape&#39;)&nbsp;&nbsp;#&nbsp;在列表的第三个位置插入元素 fruits.remove(&#39;kiwi&#39;)&nbsp;&nbsp;#&nbsp;删除指定的元素 fruits[0]&nbsp;=&nbsp;&#39;pineapple&#39;&nbsp;&nbsp;#&nbsp;修改列表中的元素</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组(Tuple)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组也是一个有序的序列,但它是不可变的,一旦创建就不能再修改。元组用圆括号(())表示,其中的元素用逗号隔开。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits&nbsp;=&nbsp;(&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组的访问方式和列表相同,但是我们不能对元组进行修改。例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print(fruits[0])&nbsp;&nbsp;#&nbsp;输出元组中的第一个元素</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits[0]&nbsp;=&nbsp;&#39;pineapple&#39;&nbsp;&nbsp;#&nbsp;报错:元组不支持修改操作</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">由于元组是不可变的,所以它在某些情况下比列表更加安全。例如,在函数返回多个值时,可以使用元组来返回这些值,避免意外地修改这些值。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">转换</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表和元组之间可以相互转换,这可以通过使用list()和tuple()函数来实现。例如,将一个元组转换为列表:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruits&nbsp;=&nbsp;(&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">fruit_list&nbsp;=&nbsp;list(fruits)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">print(fruit_list)</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">将一个列表转换为元组:</p><pre class="brush:as3;toolbar:false">fruits&nbsp;=&nbsp;[&#39;apple&#39;,&nbsp;&#39;banana&#39;,&nbsp;&#39;orange&#39;,&nbsp;&#39;kiwi&#39;] fruit_tuple&nbsp;=&nbsp;tuple(fruits) print(fruit_tuple)</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这些函数对于将数据从一个数据结构转换为另一个数据结构非常有用,特别是在处理不同类型的数据时。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 2px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 17px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python是一种流行的编程语言,广泛用于数据分析、机器学习、Web开发等领域。字符串和列表是Python中常用的两种数据类型。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串是一系列字符的序列,可以用单引号、双引号或三引号来表示。例如:</p><pre class="brush:as3;toolbar:false">my_string&nbsp;=&nbsp;&quot;Hello,&nbsp;world!&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串是不可变的,这意味着一旦创建了字符串,就不能修改它。但是,你可以对字符串进行各种操作,例如连接、截取、查找等。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串操作</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">连接字符串</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用 + 运算符将两个字符串连接起来,例如:</p><pre class="brush:as3;toolbar:false">greeting&nbsp;=&nbsp;&quot;Hello&quot; name&nbsp;=&nbsp;&quot;Alice&quot; message&nbsp;=&nbsp;greeting&nbsp;+&nbsp;&quot;,&nbsp;&quot;&nbsp;+&nbsp;name print(message)&nbsp;&nbsp;#&nbsp;输出&nbsp;&quot;Hello,&nbsp;Alice&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">截取字符串</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用切片运算符 [] 来截取字符串的一部分。例如:</p><pre class="brush:as3;toolbar:false">my_string&nbsp;=&nbsp;&quot;Hello,&nbsp;world!&quot; print(my_string[0])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;&quot;H&quot; print(my_string[0:5])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;&quot;Hello&quot; print(my_string[7:])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;&quot;world!&quot; print(my_string[-1])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;&quot;!&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串方法</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python提供了许多字符串方法,可以方便地对字符串进行操作。例如,可以使用 split() 方法将字符串拆分为子字符串:</p><pre class="brush:as3;toolbar:false">my_string&nbsp;=&nbsp;&quot;Hello,&nbsp;world!&quot; words&nbsp;=&nbsp;my_string.split(&quot;,&nbsp;&quot;) print(words)&nbsp;&nbsp;#&nbsp;输出&nbsp;[&quot;Hello&quot;,&nbsp;&quot;world!&quot;]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">更多的字符串方法可以查看Python官方文档。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表是一组有序的元素,可以包含任何类型的对象,例如整数、字符串、甚至其他列表。列表使用方括号 [] 来表示,元素之间使用逗号 , 分隔。例如:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;&quot;four&quot;,&nbsp;&quot;five&quot;]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表操作</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">添加和删除元素</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用 append() 方法向列表末尾添加元素:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3] my_list.append(4) print(my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以使用 pop() 方法从列表中删除元素:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3,&nbsp;4] last_element&nbsp;=&nbsp;my_list.pop() print(last_element)&nbsp;&nbsp;#&nbsp;输出&nbsp;4 print(my_list)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;2,&nbsp;3]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表方法</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python提供了许多列表方法,可以方便地对列表进行操作。例如,可以使用 sort() 方法将列表中的元素排序:</p><pre class="brush:as3;toolbar:false">my_list&nbsp;=&nbsp;[3,&nbsp;2,&nbsp;1] my_list.sort() print(my_list)&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;2,&nbsp;3]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">更多的列表方法可以查看Python官方文档。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表推导式</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表推导式是一种简洁的方式来创建列表。例如,可以使用列表推导式来创建一个包含平方数的列表:</p><pre class="brush:as3;toolbar:false">squares&nbsp;=&nbsp;[x**2&nbsp;for&nbsp;x&nbsp;in&nbsp;range(1,&nbsp;6)] print(squares)&nbsp;&nbsp;#&nbsp;输出&nbsp;[1,&nbsp;4,&nbsp;9,&nbsp;16,&nbsp;25]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表推导式可以包含条件语句,例如:</p><pre class="brush:as3;toolbar:false">even_squares&nbsp;=&nbsp;[x**2&nbsp;for&nbsp;x&nbsp;in&nbsp;range(1,&nbsp;6)&nbsp;if&nbsp;x&nbsp;%&nbsp;2&nbsp;==&nbsp;0] print(even_squares)&nbsp;&nbsp;#&nbsp;输出&nbsp;[4,&nbsp;16]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个列表推导式首先生成1到5之间的所有数,然后过滤掉不是偶数的数,最后对剩余的数求平方。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">总结</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串和列表是Python中常用的两种数据类型。字符串是不可变的,可以使用 + 运算符、切片运算符和字符串方法来对字符串进行操作。列表是可变的,可以使用 append() 方法、pop() 方法和列表方法来对列表进行操作,也可以使用列表推导式来创建列表。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python是一种高级编程语言,用于开发各种类型的应用程序,从Web应用程序到科学计算和人工智能。Python代码是由一系列语句组成的,其中每个语句都执行某种操作。在Python中,我们可以使用注释、变量和数据类型来编写更有效、可读和易于维护的代码。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">注释</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python中的注释是指在代码中添加的描述性文本,用于解释代码的作用、方法或任何其他相关信息。注释可以是单行注释,也可以是多行注释。单行注释使用 # 符号,例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"># 这是一个单行注释</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">多行注释使用三引号 &quot;&quot;&quot; 或 &#39;&#39;&#39; 包裹,例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">&quot;&quot;&quot; 这是一个多行注释 它可以跨越多行 &quot;&quot;&quot;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">注释可以帮助其他开发人员更容易地理解代码的作用,也可以帮助自己在以后回顾代码时更快地理解。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">变量</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">变量是指在程序中用于存储值的名称。在Python中,可以使用变量来存储任何类型的数据,例如数字、字符串、列表等。变量名必须以字母或下划线开头,并且不能包含空格或其他特殊字符。例如:</p><pre class="brush:as3;toolbar:false">x&nbsp;=&nbsp;5 my_name&nbsp;=&nbsp;&quot;Alice&quot; my_list&nbsp;=&nbsp;[1,&nbsp;2,&nbsp;3]</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,变量是动态类型的,这意味着它们不需要显式声明类型,而是根据值自动推断类型。例如,如果将一个整数赋给变量,那么变量就是整数类型。如果将一个字符串赋给变量,那么变量就是字符串类型。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">数据类型</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Python中,每个值都有一个数据类型,例如整数、浮点数、字符串、列表、元组、字典等。下面是一些常见的数据类型:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">整数:表示整数值,例如 3。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">浮点数:表示实数值,例如 3.14。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字符串:表示文本值,例如 &quot;Hello, world!&quot;。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">列表:表示有序的元素集合,例如 [1, 2, 3]。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">元组:表示有序的元素集合,但是元素不能修改,例如 (1, 2, 3)。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">字典:表示键值对的集合,例如 {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Python还支持其他数据类型,例如集合、布尔值等。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">每个数据类型都有其自己的属性和方法,可以用来对数据进行操作和处理。例如,可以使用字符串方法来获取字符串长度、将字符串转换为大写或小写、拆分字符串等。</p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Java中,方法重载是指在同一个类中定义多个方法,这些方法具有相同的名称,但参数列表不同。方法重载的主要优点是,它允许您使用相同的方法名称来执行不同的操作,这使代码更具可读性和灵活性。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">方法重载需要满足两个条件:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">方法名必须相同</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">参数列表必须不同</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">参数列表的不同可以包括参数的数量、类型或顺序。例如,以下两个方法就是重载方法:</p><pre class="brush:as3;toolbar:false">public&nbsp;int&nbsp;add(int&nbsp;x,&nbsp;int&nbsp;y)&nbsp;{ &nbsp;&nbsp;return&nbsp;x&nbsp;+&nbsp;y; } public&nbsp;double&nbsp;add(double&nbsp;x,&nbsp;double&nbsp;y)&nbsp;{ &nbsp;&nbsp;return&nbsp;x&nbsp;+&nbsp;y; }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个例子中,我们定义了两个名为 add 的方法,一个用于整数,一个用于双精度浮点数。这两个方法具有相同的名称,但参数类型不同。Java编译器可以根据传递给方法的参数类型来确定应该调用哪个方法。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">方法重载的优点:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可读性:方法重载可以使代码更易读。在许多情况下,一个名称描述的所有操作都是相关的。使用相同的名称对所有操作进行描述,可以使代码更清晰、更易于阅读和理解。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">灵活性:方法重载可以使代码更灵活。例如,在一个方法中,可以根据传递给它的参数类型和数量执行不同的操作。这使得方法更具可重用性和灵活性。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">避免名称冲突:使用相同的名称和不同的参数列表可以避免名称冲突。如果每个操作都有唯一的名称,那么在代码库中管理这些操作会变得更加复杂。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">需要注意的是,方法重载不能仅仅通过返回类型的不同来区分。Java编译器只会根据方法名称和参数列表来判断方法是否重载,而不会考虑返回类型。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Java中,方法重载是一种非常常见的技术。通过使用相同的名称和不同的参数列表来定义多个方法,可以使代码更灵活、易读和易于维护。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Java中,有参数有返回值的方法是指在调用方法时需要传递参数,并且该方法在执行完成后返回一个值。该值可以是任何基本数据类型、对象类型或数组类型。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">定义有参数有返回值的方法的语法如下:</p><pre class="brush:as3;toolbar:false">public&nbsp;returnType&nbsp;methodName(parameterType&nbsp;parameterName)&nbsp;{ &nbsp;&nbsp;//&nbsp;method&nbsp;body &nbsp;&nbsp;return&nbsp;returnValue; }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">其中:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">public 表示该方法是公共的,可以被其他类访问。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">returnType 表示该方法的返回类型,可以是任何基本数据类型、对象类型或数组类型。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">methodName 表示该方法的名称。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">parameterType 表示该方法的参数类型,可以是任何基本数据类型、对象类型或数组类型。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">parameterName 表示该方法的参数名称。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">method body 表示该方法的具体实现。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">returnValue 表示该方法的返回值。</p></li></ul><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">例如,以下是一个有参数有返回值的方法的示例:</p><pre class="brush:as3;toolbar:false">public&nbsp;int&nbsp;add(int&nbsp;x,&nbsp;int&nbsp;y)&nbsp;{ &nbsp;&nbsp;int&nbsp;sum&nbsp;=&nbsp;x&nbsp;+&nbsp;y; &nbsp;&nbsp;return&nbsp;sum; }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个示例中,add 方法接收两个整数作为参数,将它们相加,并返回它们的和。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以通过以下方式调用这个方法:</p><pre class="brush:as3;toolbar:false">int&nbsp;result&nbsp;=&nbsp;add(2,&nbsp;3); System.out.println(result);&nbsp;//&nbsp;输出&nbsp;5</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个示例中,我们将 2 和 3 作为参数传递给 add 方法,add 方法将它们相加并返回结果 5,然后将结果存储在 result 变量中。最后,使用 System.out.println() 方法输出结果。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">需要注意的是,参数列表必须与方法定义中的参数类型和数量匹配,否则会导致编译时错误。此外,返回值必须与方法定义中的返回类型相匹配,否则也会导致编译时错误。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">有参数有返回值的方法在Java中非常常见。通过将数据作为参数传递给方法,以及从方法中返回数据,可以使代码更灵活、可重用,并使其更易于维护。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

<p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在Java中,有参数无返回值的方法是指在调用方法时需要传递参数,但该方法在执行完成后不返回任何值。这种方法通常用于执行一些操作而不需要返回结果。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">定义有参数无返回值的方法的语法如下:</p><pre class="brush:as3;toolbar:false">public&nbsp;void&nbsp;methodName(parameterType&nbsp;parameterName)&nbsp;{ &nbsp;&nbsp;//&nbsp;method&nbsp;body }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">其中:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">public 表示该方法是公共的,可以被其他类访问。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">void 表示该方法没有返回值。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">methodName 表示该方法的名称。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">parameterType 表示该方法的参数类型,可以是任何基本数据类型、对象类型或数组类型。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">parameterName 表示该方法的参数名称。</p></li><li><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">method body 表示该方法的具体实现。</p></li></ul><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">例如,以下是一个有参数无返回值的方法的示例:</p><pre class="brush:as3;toolbar:false">public&nbsp;void&nbsp;printMessage(String&nbsp;message)&nbsp;{ &nbsp;&nbsp;System.out.println(message); }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个示例中,printMessage 方法接收一个字符串作为参数,并将其打印到控制台上。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">可以通过以下方式调用这个方法:</p><pre class="brush:as3;toolbar:false">printMessage(&quot;Hello,&nbsp;world!&quot;);</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在这个示例中,我们调用 printMessage 方法并将字符串 &quot;Hello, world!&quot; 作为参数传递给它。printMessage 方法将该字符串打印到控制台上,然后返回。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">需要注意的是,参数列表必须与方法定义中的参数类型和数量匹配,否则会导致编译时错误。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">有参数无返回值的方法在Java中非常常见。通过将数据作为参数传递给方法,可以在方法中执行一些操作,例如打印消息、修改对象状态等。这种方法通常用于执行操作而不需要返回结果。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>