Python入门(二)
切片
字符串截取是十分重要的
我们继续说,我们先看一个例子
<figure style="margin: 0px; padding: 0px; font-size: inherit; color: inherit; line-height: inherit;"><figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
为什么sss[:4:-1]返回的是n?****因为-1代表倒叙切分,从最后以为切到索引为4的位置只有n哦
变量
变量是什么意思?
如果你学过其他语言,使用变量先要定义,或者在定义的同时赋值
而派森的变量不需要单独定义,你直接在赋值的过程中完成了定义
当我们直接运行一个没有赋值过的变量,就报错了
之后我们对变量赋值操作,这时变量就被定义了
之后我们可以查看变量,如果我们对已赋值的变量再次赋值可以发现变量的值改变了,使用了最后一次赋值的值,也就是上一次的值被覆盖了
当我们不需要某个变量时候,我们可以使用del 变量名 来删除
<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
Python的基本运算和表达式
不同类型的数据运算,会发生隐式类型转换。
转换的规则是:低等类型向高等类型转换
前提是可以进行算术运算
等级从低到高是:bool<int<float<complex
<figure style="margin: 0px; padding: 0px; font-size: inherit; color: inherit; line-height: inherit;"> image<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
划重点:在算术运算时,True代表1,False代表0
<figure style="margin: 0px; padding: 0px; font-size: inherit; color: inherit; line-height: inherit;"> image<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
常用的Python的运算内置函数:
内置(意思就是安装好就有,直接能用)
<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
<figure style="margin: 0px; padding: 0px; font-size: inherit; color: inherit; line-height: inherit;"> image<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
初次认识模块
导入模块的操作是:import 模块的名称
我们导入了math这个模块,这个模块是做一些数学运算的
ceil(x)方法是返回大于等于x的最小整数
floor(x)方法是返回小于等于x的最大整数
常用的还有几个:
sqrt(x)返回平方根
exp(x)返回以e为底的指数
log(x)对数操作
还有常用的数学常量
π 和 e
<figure style="margin: 0px; padding: 0px; font-size: inherit; color: inherit; line-height: inherit;"> image<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>
划重点:我们这里使用了import math,则用到这个math模块里的 方法(可以理解为函数)和常量 要加上模块的名称,即:math.e 这样
你要长时间多次使用某个模块的内容
可以使用:
<figure style="margin: 0px; padding: 0px; font-size: inherit; color: inherit; line-height: inherit;"> image<figcaption style="margin: 10px 0px 0px; padding: 0px; line-height: inherit; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">mark</figcaption>
</figure>