- #1 数字类型">#1 数字类型
- #2 布尔型">#2 布尔型
- #2.1
<font style="color:rgb(71, 101, 130);">and</font>运算:">#2.1<font style="color:rgb(71, 101, 130);">and</font>运算: - #2.2
<font style="color:rgb(71, 101, 130);">or</font>运算:">#2.2<font style="color:rgb(71, 101, 130);">or</font>运算: - #2.3
<font style="color:rgb(71, 101, 130);">not</font>运算:">#2.3<font style="color:rgb(71, 101, 130);">not</font>运算:
- #2.1
- #3 字符串类型">#3 字符串类型
- #3.1 索引">#3.1 索引
- #3.2 切片">#3.2 切片
- #3.3 字符串长度">#3.3 字符串长度
- #3.4 字符串方法">#3.4 字符串方法
- #3.4.1 strip() 删除开头和结尾的空白字符">#3.4.1 strip() 删除开头和结尾的空白字符
- #3.4.2 lower() 返回小写的字符串">#3.4.2 lower() 返回小写的字符串
- #3.4.3 upper() 返回大写的字符串">#3.4.3 upper() 返回大写的字符串
- #3.4.4 replace() 用另一段字符串来替换字符串">#3.4.4 replace() 用另一段字符串来替换字符串
- #3.4.5 split() 按指定的分隔符分隔字符串">#3.4.5 split() 按指定的分隔符分隔字符串
- #3.4.6 使用 in 或 not in 来检查字符串中是否存在特定短语或字符">#3.4.6 使用 in 或 not in 来检查字符串中是否存在特定短语或字符
- #3.4.7 使用 + 运算符对字符串进行拼接">#3.4.7 使用 + 运算符对字符串进行拼接
- #3.4.8 字符串中引用变量的值">#3.4.8 字符串中引用变量的值
- #3.5 转义">#3.5 转义
- #4 获取数据类型和数据类型转换">#4 获取数据类型和数据类型转换
- #5 小结">#5 小结
- #6 练习题">#6 练习题
- #6.1 将 x 转换成浮点数,并进行输出">#6.1 将 x 转换成浮点数,并进行输出
- #6.2 将 x 转换成整数,并进行输出">#6.2 将 x 转换成整数,并进行输出
- #6.3 获取字符串 str 的长度,并进行输出">#6.3 获取字符串 str 的长度,并进行输出
- #6.4 获取字符串 str 的第一个字符 H,并进行输出">#6.4 获取字符串 str 的第一个字符 H,并进行输出
- #6.5 获取字符串 str 的子字符串 llo,并进行输出">#6.5 获取字符串 str 的子字符串 llo,并进行输出
- #6.6 去除字符串 str 中的空格,并进行输出">#6.6 去除字符串 str 中的空格,并进行输出

- Python 基础教程

1
name,age,weight = '女娲',18,49.99print(name)print(age)print(weight)
2
3
4 我们仔细来看这个语句:

<font style="color:rgb(71, 101, 130);">int</font> 来表示整数;49.99 是浮点数,在 Python 中用 <font style="color:rgb(71, 101, 130);">float</font> 表示;也就是说,我们人类所理解的整数,在计算机的认知中,是整型 <font style="color:rgb(71, 101, 130);">int</font>,而人类所理解的小数,在计算机中则是用浮点型 <font style="color:rgb(71, 101, 130);">float</font> 表示。
#1 数字类型

#1.1 int
相传在上古时期,老百姓无法分辨粮食和草药,只能依靠打猎维持生存,即使生病也没办法医治,神农氏因为想改变这个局面,亲自上山采药,亲口尝各类草药,辨别药性。 神农氏踏遍了山野,尝了七七四十九天,发现了麦、稻、谷子、高粱能充饥,并且尝出了 365 种草药,还编写了《神农本草》。 这个故事里面,我们看到了 2 个整数,七七四十九天和 365 种草药:1
day = 49number = 365print(day)print(number)
2
3
4 根据变量命名规则,我们将整数 49 赋值给变量 day,整数 365 赋值给变量 number。 整数为正数或负数,没有小数,长度不限。 这些都是整数哟!
1
#光速,每秒30万公里speed_of_light = 300000#地球质量,5.97×10²⁴kgmass_of_earth = 5970000000000000000000000#地球最深峡谷的海拔,10994m,因在地下,我们这里用负数表示altitude_of_mariana_trench = -10994print(speed_of_light)print(mass_of_earth)print(altitude_of_mariana_trench)
2
3
4
5
6
7
8
9 宋老师在这里用了比较复杂的变量名称,就是给大家展示下变量名的意义表达,通过下划线
<font style="color:rgb(71, 101, 130);">_</font> 可以将有含义的英文串联起来,做到我们的代码规范哟!
下面我们一起看浮点数:
#1.2 float
浮点数是包含小数的正数或负数,就是我们人类世界中的小数。 比如大家非常熟悉的 π:<font style="color:rgb(71, 101, 130);">pi = 3.1415926</font>,π 是特殊字符,不能用作变量名,因此我们这里用谐音 <font style="color:rgb(71, 101, 130);">pi</font> 来代替哦!(宋老师时刻不忘提醒大家注意变量的命名规则,大家一定要从基础开始养成好的代码习惯!是受益终身的习惯哦!)
1
pi = 3.1415926negative_pi = -3.1415926print(pi)print(type(negative_pi))
2
3
4
5
#1.3 运算符
我们大家都知道数字涉及到加减乘除、比大小等各种运算,Python 中也有这些运算哦,这就是 Python 的运算符,Python 中运算符包括算术运算符、赋值运算符、比较运算符和逻辑运算符,我们逐一来介绍哈。
#1.3.1 算术运算符
我们再来回顾下神农尝百草的故事,神农氏踏遍了山野,尝了七七四十九天,发现了麦、稻、谷子、高粱能充饥。 这里面的七七十九天,就可以用算术运算符来表示哦,Python 中用<font style="color:rgb(71, 101, 130);">*</font>来表示两数相乘,具体语句是:
1
day1 = 7day2 = 7day = day1 * day2print(day)
2
3
4 我们看到最终打印出的是 49,Python 中的算术运算符,我们收藏下方表格哦!

#1.3.1.1 +(加) 运算符
1
a = 1b = 1c = 3.14d = 2.71print(a + b)print(c + d)
2
3
4
5
6
7
8
#1.3.1.2 -(减) 运算符
1
a = 1b = 1c = 3.14d = 2.71print(a - b)print(c - d)
2
3
4
5
6
7
8
#1.3.1.3 *(乘) 运算符
1
a = 2b = 1c = 3.14d = 2.71print(a * b)print(c * d)
2
3
4
5
6
7
8
#1.3.1.4 /(除) 运算符
1
a = 2b = 1c = 3.14d = 2.71print(a / b)print(c / d)
2
3
4
5
6
7
8
#1.3.1.5 %(取模) 运算符
1
a = 10b = 3print(a % b)
2
3
4
#1.3.1.6 **(幂) 运算符
1
a = 10b = 3print(a ** b)
2
3
4
#1.3.1.7 //(取整) 运算符
1
a = 10b = 3print(a // b)
2
3
4
#1.3.2 赋值运算符
下面,我们一起来看下赋值运算符,在变量的赋值中,我们提到了<font style="color:rgb(71, 101, 130);">=</font> 的含义是赋值,在 Python 中赋值运算符 <font style="color:rgb(71, 101, 130);">=</font> 可以和算术运算符进行结合,具体如下表,还是建议大家保存下,有需要的时候方便查看!

#1.3.2.1 = 运算符
<font style="color:rgb(71, 101, 130);">=</font> 运算符和变量的赋值是一样哒。
1
a = 3print(a)
2
3
#1.3.2.2 +=
神农在品尝了 365 种草药之后,并没有放弃寻找更多草药和粮食的脚步,现在他又新品尝了 30 种草药,你能使用<font style="color:rgb(71, 101, 130);">+=</font> 运算符帮他计算出他一共品尝了多少种草药么?
1
number = 365number += 30print(number)
2
3
4 其他的赋值运算符,宋老师也都一一给大家准备了示例,大家可以对照示例自己体会下赋值运算符的使用: (自己动手,丰衣足食!!!小伙伴们,加油!!!)
#1.3.2.3 -=
1
a = 3a -= 3print(a)
2
3
4
#1.3.2.4 *=
1
a = 3a *= 3print(a)
2
3
4
#1.3.2.5 /=
1
a = 3a /= 3print(a)
2
3
4
#1.3.2.6 %=
1
a = 10a %= 3print(a)
2
3
4
#1.3.2.7 **=
1
a = 10a **= 3print(a)
2
3
4
#1.3.2.8 //=
1
a = 10a //= 3print(a)
2
3
4
#1.3.3 比较运算符
在神农品尝的 365 种草药中,假设有 50 种可以作为粮食食用,有 200 种是对人类疾病有益的药材,那么到底是药材的数量多还是粮食的数量多呢,这就涉及到比较啦,在人类的认知中,这种比较非常简单:200 > 50,那么在 Python 的世界中,这种比较是如何表达的呢,比较运算符就可以解决这个问题哦!1
food_number = 50drug_number = 200print(food_number < drug_number)
2
3 因为 200 是大于 50 的,因此
<font style="color:rgb(71, 101, 130);">print()</font> 打印的是 <font style="color:rgb(71, 101, 130);">True</font>,就是这种比较的结果为真!
比较运算符返回的值是 <font style="color:rgb(71, 101, 130);">True</font> 或者 <font style="color:rgb(71, 101, 130);">False</font>。
下图是一些常用的比较运算符,还是建议各位小伙伴收藏哈!

#1.3.3.1 ==(等于)
【敲重点】宋老师在这里要和大家重点强调下,` 比较运算符 和</font><font style="color:rgb(44, 62, 80);"> </font>=` 赋值运算符的区别:

` 来表达,单个等号 </font>=` 的含义是赋值哦,这一点,大家要熟记哈!
我们也为大家准备了比较运算符的例子,大家可以逐一体验下:
1
a = 1b = 2print(a == b)
2
3
4 a 是 1,b 是 2,显然 a 不等于 b,因此打印的结果是
<font style="color:rgb(71, 101, 130);">False</font>。
#1.3.3.2 !=(不等于)
1
a = 1b = 2print(a != b)
2
3
4 a 是 1,b 是 2,a 不等于 b,因此打印的结果是
<font style="color:rgb(71, 101, 130);">True</font>。
#1.3.3.3 >(大于)
1
a = 1b = 2print(a > b)
2
3
4 a 是 1,b 是 2,a < b,因此打印的结果是
<font style="color:rgb(71, 101, 130);">False</font>。
#1.3.3.4 <(小于)
1
a = 1b = 2print(a < b)
2
3
4 a 是 1,b 是 2,a < b,因此打印的结果是
<font style="color:rgb(71, 101, 130);">True</font>。
#1.3.3.5 >=(大于或等于)
1
a = 2b = 2print(a >= b)
2
3
4 a 是 2,b 是 2,a 等于 b,符合 a 大于或等于 b,因此打印的结果是
<font style="color:rgb(71, 101, 130);">True</font>。
#1.3.3.6 <=(小于或等于)
1
a = 2b = 2print(a <= b)
2
3
4 a 是 2,b 是 2,a 等于 b,符合 a 小于或等于 b,因此打印的结果是
<font style="color:rgb(71, 101, 130);">True</font>。
#1.3.4 逻辑运算符
神农告诉人们,麦、稻、谷子、高粱等 50 种植物可以作为粮食充饥,蒲公英、黄芪、三七等 200 种植物可以作为药材,现在我们想判断下粮食的数量小于 60 并且药材的品种大于 150,是否是真实的,该如何判断呢? 在 Python 中是通过逻辑运算符<font style="color:rgb(71, 101, 130);">and</font> 来实现的,我们来看下方语句:
1
food_number = 50drug_number = 200print(food_number < 60 and drug_number > 150)
2
3 我们看到打印出的内容是
<font style="color:rgb(71, 101, 130);">True</font>,<font style="color:rgb(71, 101, 130);">and</font> 运算符表示当 <font style="color:rgb(71, 101, 130);">food_number < 60</font> 和 <font style="color:rgb(71, 101, 130);">drug_number > 150</font> 都为真时,返回 <font style="color:rgb(71, 101, 130);">True</font>,下图是常见的逻辑运算符,大家可以保存下哦!

#1.3.4.1 and
1
a = 4b = 8print(a > 3 and b < 10)print(a > 3 and b < 6)
2
3
4
5 我们看到当
<font style="color:rgb(71, 101, 130);">a</font> 赋值为 4,<font style="color:rgb(71, 101, 130);">a > 3</font> 为真,<font style="color:rgb(71, 101, 130);">b</font> 赋值为 8,<font style="color:rgb(71, 101, 130);">b < 10</font> 为真,<font style="color:rgb(71, 101, 130);">b < 6</font> 为假,逻辑运算符 <font style="color:rgb(71, 101, 130);">and</font> 当 <font style="color:rgb(71, 101, 130);">a</font> 和 <font style="color:rgb(71, 101, 130);">b</font> 都为真的时候,才会返回 <font style="color:rgb(71, 101, 130);">True</font>,因此第一个<font style="color:rgb(71, 101, 130);">print()</font> 返回值为 <font style="color:rgb(71, 101, 130);">True</font>,第二个 <font style="color:rgb(71, 101, 130);">print()</font> 返回值为 <font style="color:rgb(71, 101, 130);">False</font>。
其他两个运算符的例子大家可以自己运行体验下哈:
#1.3.4.2 or
1
a = 4b = 8print(a > 3 or b < 10)print(a > 3 or b < 6)print(a > 5 or b < 6)
2
3
4
5
6
#1.3.4.3 not
1
a = 4b = 8print(not(a > 3 or b < 10))print(not(a > 3 or b < 6))print(not(a > 5 or b < 6))
2
3
4
5
6
#2 布尔型
我们在进行逻辑运算和比较运算的时候,得到的结果为<font style="color:rgb(71, 101, 130);">True</font> 或 <font style="color:rgb(71, 101, 130);">False</font>。在 Python 中,用 <font style="color:rgb(71, 101, 130);">True</font>、<font style="color:rgb(71, 101, 130);">False</font> 表示布尔值。
布尔值可以用 <font style="color:rgb(71, 101, 130);">and</font>、<font style="color:rgb(71, 101, 130);">or</font> 和 <font style="color:rgb(71, 101, 130);">not</font> 运算。
#2.1 <font style="color:rgb(71, 101, 130);">and</font> 运算:
布尔值的 <font style="color:rgb(71, 101, 130);">and</font> 运算就是真真为真,当 <font style="color:rgb(71, 101, 130);">and</font> 连接的两个布尔型都是 <font style="color:rgb(71, 101, 130);">True</font> 的时候,为 <font style="color:rgb(71, 101, 130);">True</font> ,一真一假和两假都为假。
1
print(True and True)print(True and False)print(False and False)
2
3
#2.2 <font style="color:rgb(71, 101, 130);">or</font> 运算:
布尔值的 <font style="color:rgb(71, 101, 130);">or</font> 运算就是假假为假,当 <font style="color:rgb(71, 101, 130);">and</font> 连接的两个布尔型都是 <font style="color:rgb(71, 101, 130);">False</font> 的时候,为 <font style="color:rgb(71, 101, 130);">False</font> ,一真一假和两真都为真。
1
print(True or True)print(True or False)print(False or False)
2
3
#2.3 <font style="color:rgb(71, 101, 130);">not</font> 运算:
布尔值的 <font style="color:rgb(71, 101, 130);">not</font> 运算则表示非,非真就是假,非假就是真!
各位小伙伴加油哦,用我们的慧眼来分辨这大千世界的真真假假!
1
print(not True)print(not False)
2
#3 字符串类型
我们还是回到这张图片:
<font style="color:rgb(71, 101, 130);">'</font> 或双引号 <font style="color:rgb(71, 101, 130);">"</font> 括起来的任意文本就是字符串啦,上图中 <font style="color:rgb(71, 101, 130);">'女娲'</font> 的数据类型就是字符串,大家这里需要注意的是,只要被引号包含的任意文本都是字符串!例如:<font style="color:rgb(71, 101, 130);">'123456'</font> <font style="color:rgb(71, 101, 130);">"hello world!"</font>。<font style="color:rgb(71, 101, 130);">'</font> 和 <font style="color:rgb(71, 101, 130);">"</font> 不是字符串的一部分,只是一种表示方式。
1
print('hello world!')print("hello world!")
2 如果字符串横跨多行,可以使用三个单引号或三个双引号将字符串括起来。 三个单引号:
1
print('''Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.''')
2
3
4
5
6 三个双引号:
1
print("""Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.""")
2
3
4
5
6 整型和浮点数可以赋值给变量,字符串也是可以赋值给变量的:
1
say_hi = "hello world!"print(say_hi)
2
3 宋老师出个题目考考大家,看看大家是否已经灵活掌握了本部分知识: 下列数据类型属于字符串的是: A.weight = 90 B.gift = ‘鲜花’ C.gift_spend = 300 D.math_grade = 96.5 正确答案:B 选A提示:正确答案为B!引号包含的数据类型是字符串哦! 选B提示:回答正确!引号包含的数据类型是字符串! 选C提示:正确答案为B!引号包含的数据类型是字符串哦! 选D提示:正确答案为B!引号包含的数据类型是字符串哦!
#3.1 索引
中国古人一直有观天象的习俗,通过观天象,来获得对季节、气候、甚至是国家命运的认知。现代,通过科学家们的努力,我们知道了浩瀚宇宙的深邃,知道了八大行星的存在,海王星(neptune),八大行星之一,假设我们在Python 的世界中,可以重见神农,让我们用 Python 的语言为神农介绍下海王星的字母构成吧!这就要运用我们下方为大家介绍的内容:索引。 在 Python 中,字符串中的字符可以通过索引来提取。可以从前往后索引,也可以从后往前索引。 索引语法是:<font style="color:rgb(71, 101, 130);">变量[下标]</font>,这里的下标是由数字表示,代表所要索引的字符在变量中的位置。
当从前往后索引时,下标从 0 开始。
当从后往前索引时,下标从 -1 开始,如下图所示!

1
star = "NEPTUNE"print(star[0])print(star[-1])print(star[6])print(star[-7])
2
3
4
5
6 大家来自己试一下下面这个题目: introduce_star = ‘This is the neptune’,将变量中所有的 e 打印出来! (可以试下从前向后索引,也可以试下从后向前哦)
#3.2 切片
我们看到通过索引,只能提取单个字符,神农说,难得来一趟,想多认识一些字符,这个时候怎么办呢?我们可以通过切片来提取变量的多个字符,我们一起来看下切片怎么使用。 切片语法是:<font style="color:rgb(71, 101, 130);">变量[头下标:尾下标]</font>(不包括尾下标对应的元素)。
当不指定头下标和尾下标时,获取的是整个字符串:<font style="color:rgb(71, 101, 130);">star[:]</font>。
当只指定头下标时,获取的是从头下标到字符串结尾的所有字符。
当只指定尾下标时,获取的是字符串的开头到尾下标的字符串(不包括尾下标对应的元素)。
头下标和尾下标也可以使用负值。
大家先在心中想一下下方语句的运行结果,再运行一下,看看结果是否一致呢?
1
star = "NEPTUNE"# 获取 NEPprint(star[0:3])# 获取 NEPTUNEprint(star[:])# 获取 NEprint(star[-7:-5])# 获取 EPTUNEprint(star[1:])# 获取 NEPTprint(star[:4])
2
3
4
5
6
7
8
9
10
11
12
#3.3 字符串长度
神农:“现代社会真神奇啊,不仅能认识海王星,还能读取海王星的另一种语言表达,可是我还有一个疑问,海王星的英文是几个字母构成的呢”? 我们:“这个问题简单,Python 中<font style="color:rgb(71, 101, 130);">len()</font> 可以获取字符串的长度哦”
1
star = "NEPTUNE"print(len(star))
2
3 在 Python 中,我们使用
<font style="color:rgb(71, 101, 130);">len(变量名)</font> 来获取变量的长度,len 是长度 length 的前三个字母,也是比较好记忆和理解的!
#3.4 字符串方法
Python 有一组可用于字符串的内置方法,可以实现字符串的各种变换,宋老师在这里还是建议大家收藏下方表格哟!
#3.4.1 strip() 删除开头和结尾的空白字符
神农:“现代的信息技术太精彩了!” 我们:“这都不算什么哟,还有更精彩的,同样的NEPTUNE(海王星),我们也可以做出一些变换的哦” 神农:“哦?快快,教教我是怎么做的” 有些时候,我们会在代码的编写中做一些空格的操作,这些空格在 Python 中就是空白字符,我们可以使用<font style="color:rgb(71, 101, 130);">strip()</font>来删除字符串开头和结尾的空白字符,使用方法就是<font style="color:rgb(71, 101, 130);">变量名.strip()</font>,我们来看下方语句:
1
star = " NEPTUNE "print(star)print(len(star))print(star.strip())print(len(star.strip()))
2
3
4
5
6 我们看到在没有删除开头和结尾的空白字符串之前,变量 star 的长度是 9,使用
<font style="color:rgb(71, 101, 130);">strip()</font> 之后,长度是 7,空白字符串被删除了!
神农:“真是很神奇啊!”
其余方法的使用是一样的,都是<font style="color:rgb(71, 101, 130);">变量.方法</font>,下方的几个方法大家自己运行体会下哈!
#3.4.2 lower() 返回小写的字符串
1
star = "NEPTUNE"print(star.lower())
2
3
#3.4.3 upper() 返回大写的字符串
1
star = "neptune"print(star.upper())
2
3
#3.4.4 replace() 用另一段字符串来替换字符串
1
say_hi = "Hello World!"print(say_hi.replace("World", "Kitty"))
2
3
#3.4.5 split() 按指定的分隔符分隔字符串
1
say_hi = "Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Pluto"print(say_hi.split(","))
2
3
#3.4.6 使用 in 或 not in 来检查字符串中是否存在特定短语或字符
1
book_name = "Men Are from Mars, Women Are from Venus"is_exist = "Mars" in book_nameprint(is_exist)
2
3
4
1
book_name = "Men Are from Mars, Women Are from Venus"is_exist = "Mars" not in book_nameprint(is_exist)
2
3
4
#3.4.7 使用 + 运算符对字符串进行拼接
1
first_part = "Men Are from Mars"second_part = "Women Are from Venus"print(first_part + ', ' + second_part)
2
3
4
#3.4.8 字符串中引用变量的值
- 在字符串前面加小写字母
<font style="color:rgb(71, 101, 130);">'f'</font> - 在字符串中,将需要引用的变量,用花括号包起来
<font style="color:rgb(71, 101, 130);">{name}</font>
1
name = 'Earth'age = 4.543E9print(f"My name is {name}, {age} years old")
2
3
4
#3.5 转义
在第一章中,我们曾经给大家介绍过转义字符,今天在进行字符串知识的学习之后,我们再给大家仔细介绍下转义字符中的<font style="color:rgb(71, 101, 130);">\</font>。
首先,我们先来回顾下字符串的一个要点,字符串是由单引号 <font style="color:rgb(71, 101, 130);">'</font> 或双引号 <font style="color:rgb(71, 101, 130);">"</font> 括起来的任意文本。也就是说,在字符串的变量赋值中,是包含单引号或双引号的,那么对于有些语句本身就包含引号的,Hi, shennong, this ‘s the neptune! 这种情况 Python 是怎么处理的呢?
在 Python 中,对于本身带有引号的字符串,有以下几种情况:
(1)由双引号 <font style="color:rgb(71, 101, 130);">"</font> 括起来的任意文本包含单引号 <font style="color:rgb(71, 101, 130);">'</font> ,例如:
1 运行起来,并不会出现问题,正常打印出字符串。 (2)由单引号
print("You're uinique, nothing can replace you.")
<font style="color:rgb(71, 101, 130);">'</font> 括起来的任意文本包含双引号 <font style="color:rgb(71, 101, 130);">"</font>,例如:
1 运行起来,也都是正确的,但是当我们把上述例子中的双引号
print('The man who has made up his mind to win will never say "impossible".')
<font style="color:rgb(71, 101, 130);">"</font> 换成单引号 <font style="color:rgb(71, 101, 130);">'</font>,把单引号 <font style="color:rgb(71, 101, 130);">'</font> 换成双引号 <font style="color:rgb(71, 101, 130);">"</font> 时,便会报错。这个时候就需要对 <font style="color:rgb(71, 101, 130);">'</font> 或 <font style="color:rgb(71, 101, 130);">"</font> 进行转义:
1
print('You're uinique, nothing can replace you.')print("The man who has made up his mind to win will never say "impossible"")
2 (3)单引号括起来的字符串中使用单引号,对字符串中的单引号进行转义,使用转义字符
<font style="color:rgb(71, 101, 130);">\</font>,在每一个字符串中的单引号前使用<font style="color:rgb(71, 101, 130);">\</font>。
1 (4)双引号括起来的字符串中使用双引号,需要对字符串中的双引号进行转义,转义使用字符
print('You\'re uinique, nothing can replace you.')
<font style="color:rgb(71, 101, 130);">\</font>,在每一个字符串中的双引号前使用<font style="color:rgb(71, 101, 130);">\</font>。
1
print("The man who has made up his mind to win will never say \"impossible\"")
#4 获取数据类型和数据类型转换
神农:“这趟来的太值了,我认识了整型 int、浮点数 float、还有布尔值和字符串,可是怎么我感觉还是有些晕啊,尤其是遇到整数的数字时,到底哪些是整型?哪些是字符串呢?” 我们:“神农前辈不要担心,这个 Python 也有办法解决哦!” 在 Python 中,对于有些变量的数据类型,我们不太确定的时候,可以使用<font style="color:rgb(71, 101, 130);">type()</font>,来获取变量的数据类型,<font style="color:rgb(71, 101, 130);">type()</font> 的使用方法是 <font style="color:rgb(71, 101, 130);">type(变量名)</font>,我们一起来看下面的例子:
1
#变量weight1,赋值为整数50weight1= 50#变量weight2,赋值为字符串50weight2 = '50'#变量weight3,赋值为浮点数50.00weight3= 50.00#打印变量weight1的数据类型print(type(weight1))#打印变量weight2的数据类型print(type(weight2))#打印变量weight3的数据类型print(type(weight3))
2
3
4
5
6
7
8
9
10
11
12 我们看到分别打印出了整型、字符串和浮点数。 在 Python 中,不同的数据类型是可以互相转换的,通过类型转换函数来实现,下图为常见的类型转换函数:

<font style="color:rgb(71, 101, 130);">int()</font>,将 50.00 转换为 50,也可以使用 <font style="color:rgb(71, 101, 130);">float()</font>,将 50 转换为 50.00,具体语句如下:
1
weight1= 50weight3= 50.00weight4= int(weight3)weight5=float(weight1)print(type(weight1))print(type(weight3))print(type(weight4))print(type(weight5))
2
3
4
5
6
7
8 神农:“嗯嗯,真是太精彩了,变幻无穷!” 我们:“和我们一起学 Python 吧!” 下面,我们运用
<font style="color:rgb(71, 101, 130);">str()</font> 和字符串的<font style="color:rgb(71, 101, 130);">+</font>运算符打印出下列内容:
神农每天品尝 3 种草药,品尝了 50 天,一共品尝了 150 种草药
1
num1 = 3num2 = 50num3 = 150print('神农每天品尝 '+str(num1)+' 种草药,品尝了 '+str(num2)+' 天,一共品尝了 '+str(num3) + ' 种草药')
2
3
4
#5 小结

#6 练习题
#6.1 将 x 转换成浮点数,并进行输出
1
x = 5
#6.2 将 x 转换成整数,并进行输出
1
x = 5.5
#6.3 获取字符串 str 的长度,并进行输出
1
str = "Hello World!"
#6.4 获取字符串 str 的第一个字符 H,并进行输出
1
str = "Hello World!"
#6.5 获取字符串 str 的子字符串 llo,并进行输出
1
str = "Hello World!"
#6.6 去除字符串 str 中的空格,并进行输出
1
str = " Hello World! "
更新于: 12/30/2021, 2:46:39 AM
