Python基础小结
<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>一、执行Python程序的两种方式<ul><li>1.1 交互式</li><li>1.2 命令行式</li></ul></li><li>二、执行Python程序的两种IDE<ul><li>2.1 Pycharm</li><li>2.2 Jupyter</li></ul></li><li>三、变量<ul><li>3.1 什么是变量?</li><li>3.2 变量的组成?</li><li>3.3 变量名的定义规范</li><li>3.4 定义变量的两种方式</li><li>3.5 常量</li></ul></li><li>四、注释<ul><li>4.1 单行注释</li><li>4.2 多行注释</li><li>4.3 引用计数</li><li>4.4 垃圾回收机制</li><li>4.5 小整数池</li></ul></li><li>五、花式赋值<ul><li>5.1 链式赋值</li><li>5.2 交叉赋值</li></ul></li><li>六、与用户交互</li><li>七、input</li><li>八、格式化输出<ul><li>8.1 占位符</li><li>8.2 format格式化</li><li>8.3 f-string格式化</li></ul></li><li>九、基本运算符<ul><li>9.1 算术运算符</li><li>9.2 逻辑运算符</li><li>9.3 比较运算符</li><li>9.4 赋值运算符</li><li>9.5 身份运算符</li><li>9.6 运算符优先级(略)</li><li>9.7 解压缩</li></ul></li><li>一十、流程控制<ul><li>10.1 流程控制之if判断<ul><li>10.1.1 if</li><li>10.1.2 if...else</li><li>10.1.3 if...elif....elif...else</li></ul></li><li>10.2 流程控制之while循环<ul><li>10.2.1 while</li><li>10.2.2 while + break</li><li>10.2.3 while + continue</li><li>10.2.4 while + else</li></ul></li><li>10.3 流程控制之for循环<ul><li>10.3.1 for</li><li>10.3.2 for + break</li><li>10.3.3 for + continue</li><li>10.3.4 for + else</li></ul></li></ul></li></ul></div><br><span style="color: rgba(255, 0, 0, 1)">Python从入门到放弃完整教程目录:https://www.cnblogs.com/nickchen121/p/10718112.html</span><p></p>
<h1 id="一执行python程序的两种方式">一、执行Python程序的两种方式</h1>
<h2 id="11-交互式">1.1 交互式</h2>
<p>在终端内输入python3,然后输入python代码</p>
<h2 id="12-命令行式">1.2 命令行式</h2>
<p>在终端内输入python3 文本文件路径</p>
<h1 id="二执行python程序的两种ide">二、执行Python程序的两种IDE</h1>
<h2 id="21-pycharm">2.1 Pycharm</h2>
<h2 id="22-jupyter">2.2 Jupyter</h2>
<h1 id="三变量">三、变量</h1>
<h2 id="31-什么是变量">3.1 什么是变量?</h2>
<p>变量一般用来用来描述世间万物变化的状态</p>
<h2 id="32-变量的组成">3.2 变量的组成?</h2>
<p>变量名 = 变量值</p>
<p><strong style="color: rgba(255, 0, 0, 1)">变量名是用来接收变量值的</strong></p>
<h2 id="33-变量名的定义规范">3.3 变量名的定义规范</h2>
<ol>
<li>变量名具有某种意义</li>
<li>由数字/字母/下划线组成,且不能由数字开头,(也不能由下划线开头)</li>
<li>不能用Python关键字</li>
</ol>
<pre><code class="language-python">['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
</code></pre>
<h2 id="34-定义变量的两种方式">3.4 定义变量的两种方式</h2>
<ul>
<li>驼峰体:<code>NameOfNick</code></li>
<li>下划线:<code>name_of_nick</code>(推荐)</li>
</ul>
<h2 id="35-常量">3.5 常量</h2>
<p>常量是约定俗成的一种规范,常量名的定义方式为全大写.实际上可以被修改.</p>
<h1 id="四注释">四、注释</h1>
<h2 id="41-单行注释">4.1 单行注释</h2>
<p><code>#注释的语句</code>,#后面的字符不执行语法,即只为普通字符,一般用来解释某一段代码</p>
<h2 id="42-多行注释">4.2 多行注释</h2>
<p>三单引号/三双引号</p>
<h2 id="43-引用计数">4.3 引用计数</h2>
<p>变量值的引用次数</p>
<pre><code class="language-python">x = 257# 257的引用计数为1
y = x # 257的引用计数为2
del x# 257的引用计数为1
</code></pre>
<h2 id="44-垃圾回收机制">4.4 垃圾回收机制</h2>
<p>当变量值的引用计数为0的时候,该变量值会被Python自动回收它的内存占用</p>
<h2 id="45-小整数池">4.5 小整数池</h2>
<p>[-5,256]之间的整数会在Python解释器启动的时候,自动开辟一块内存存入这些整数.也就是说这些整数不会因为引用计数为0而被删除</p>
<h1 id="五花式赋值">五、花式赋值</h1>
<h2 id="51-链式赋值">5.1 链式赋值</h2>
<p><code>x = y = z = 10</code></p>
<h2 id="52-交叉赋值">5.2 交叉赋值</h2>
<pre><code class="language-python"># 交叉赋值
x = 10
y = 20
x, y = y, x
print(x, y)
# 使用临时变量
x = 10
y = 20
temp = x
x = y
y = temp
print(x, y)
</code></pre>
<h1 id="六与用户交互">六、与用户交互</h1>
<h1 id="七input">七、input</h1>
<p>input输入的都是字符串形式</p>
<h1 id="八格式化输出">八、格式化输出</h1>
<h2 id="81-占位符">8.1 占位符</h2>
<p>%s接收任意数据类型的数据<br>
%d接收数字类型的数据</p>
<pre><code class="language-python">name = 'nick'
'nick name is %s'%name
</code></pre>
<h2 id="82-format格式化">8.2 format格式化</h2>
<p>{}接收任意数据类型的数据</p>
<pre><code class="language-python">name = 'nick'
'nick name is {}'.format(name)
</code></pre>
<h2 id="83-f-string格式化">8.3 f-string格式化</h2>
<p>在字符串前面加上f或F,然后使用{}接收任意数据类型的数据</p>
<pre><code class="language-python">name = 'nick'
f'nick name is {name}'
</code></pre>
<h1 id="九基本运算符">九、基本运算符</h1>
<h2 id="91-算术运算符">9.1 算术运算符</h2>
<p><code>+ - * / % // **</code></p>
<h2 id="92-逻辑运算符">9.2 逻辑运算符</h2>
<p><code>and or not</code></p>
<h2 id="93-比较运算符">9.3 比较运算符</h2>
<p><code>> >= < <= == !=</code></p>
<h2 id="94-赋值运算符">9.4 赋值运算符</h2>
<p><code>= += -= *= /= //= **= %=</code></p>
<h2 id="95-身份运算符">9.5 身份运算符</h2>
<p><code>is is not</code></p>
<ul>
<li>id相同的值一定相同,值相同的id不一定相同</li>
</ul>
<h2 id="96-运算符优先级略">9.6 运算符优先级(略)</h2>
<p>如果需要某个运算符优先运算,则加个括号,使用<code>a and b is c == d</code>的是傻逼</p>
<h2 id="97-解压缩">9.7 解压缩</h2>
<pre><code class="language-python">hobby_list = ['read','run','sleep','fishing','piao']
# 如果取第2-3个爱好
_,hobby2,hobby3,*_ = hobby_list
print(hobby2, hobby3)
</code></pre>
<h1 id="一十流程控制">一十、流程控制</h1>
<h2 id="101-流程控制之if判断">10.1 流程控制之if判断</h2>
<h3 id="1011-if">10.1.1 if</h3>
<pre><code class="language-python">if 条件:
代码块
</code></pre>
<h3 id="1012-ifelse">10.1.2 if...else</h3>
<pre><code class="language-python">if 条件:
代码块
else:
代码块
</code></pre>
<h3 id="1013-ifelifelifelse">10.1.3 if...elif....elif...else</h3>
<pre><code class="language-Python">if 条件:
代码块
elif 条件:
代码块
elif 条件:
代码块
...(可以写任意个elif)
else:
代码块
</code></pre>
<h2 id="102-流程控制之while循环">10.2 流程控制之while循环</h2>
<h3 id="1021-while">10.2.1 while</h3>
<pre><code class="language-python">while 条件:
代码块
</code></pre>
<h3 id="1022-while--break">10.2.2 while + break</h3>
<pre><code class="language-python">while 条件:
代码块
break# 结束本层循环,跳出循环
</code></pre>
<h3 id="1023-while--continue">10.2.3 while + continue</h3>
<pre><code class="language-python">while 条件:
代码块
if 条件:
代码块
cotinue# 不执行下面代码,然后继续循环,即跳出本次循环
代码块
</code></pre>
<h3 id="1024-while--else">10.2.4 while + else</h3>
<pre><code class="language-python">while 条件:
代码块
else:
print('如果我没有被break,我就会被打印出来')
</code></pre>
<h2 id="103-流程控制之for循环">10.3 流程控制之for循环</h2>
<h3 id="1031-for">10.3.1 for</h3>
<pre><code class="language-python">for i in range/str/list/tuple/dict/set(可迭代对象):
print(i)
</code></pre>
<h3 id="1032-for--break">10.3.2 for + break</h3>
<pre><code class="language-python">for i in range/str/list/tuple/dict/set(可迭代对象):
print(i)
break# 结束本层循环
</code></pre>
<h3 id="1033-for--continue">10.3.3 for + continue</h3>
<pre><code class="language-python">for i in range/str/list/tuple/dict/set(可迭代对象):
print(i)
if 条件:
continue# 结束本次循环,即不执行下面代码,继续循环
代码块
</code></pre>
<h3 id="1034-for--else">10.3.4 for + else</h3>
<pre><code class="language-python">for i in range/str/list/tuple/dict/set(可迭代对象):
print(i)
else:
print('如果我没有被break,我就会被打印出来')
</code></pre><br><br>
来源:https://www.cnblogs.com/nickchen121/p/11069987.html
頁:
[1]