Task2 数学运算、字符串和文本、列表
一、基础部分
1、实现第一行代码和认识注释
print('hello world')
注释是由# 加相关备注,其目的就是其他开发者能够轻松的了解代码,注释不影响代码程序的编译和运行
print("I could have code like this.") # and the comment after 5
2、数学运算
首先,认识运算符
加减乘除等以及特殊符号
• + plus,加号
• - minus,减号
• / slash,斜杠
• * asterisk,星号
• % percent,百分号
• < less-than,小于号
• > greater-than,大于号
• <= less-than-equal,小于等于号
• >= greater-than-equal,大于等于号
下面是一些补充:
• % 取模 - 返回除法的余数
•** 幂 - 返回x的y次幂
•// 取整除 - 返回商的整数部分(向下取整)
练习
print('i will now count my chickens:')print('hens',25+30/6)print('Roosters',100-26*3%4)print('now i will count the eggs:')print(3)
输出
i will now count my chickens:hens 30.0Roosters 98now i will count the eggs:3
print("I will now count my chickens:")print("Hens", 25 + 30 / 6)print("Roosters", 100 - 25 * 3 % 4)print("Now I will count the eggs:")print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)print('is it true that 3+2<5-7?')print(3+2<5-7)print('what is 3+2?',3+2)print('what is 5-7?',5-7)print("oh, that is why it's false.")print('how about some more.')print('is it greater?',5>-2)print('is it greater or equal?',5>=-2)print('is it less or equal?',5<=2)
输出
I will now count my chickens:Hens 30.0Roosters 97Now I will count the eggs:6.75is it true that 3+2<5-7?Falsewhat is 3+2? 5what is 5-7? -2oh, that is why it's false.how about some more.is it greater? Trueis it greater or equal? Trueis it less or equal? False
自我练习
(1)在上面每一行上,用#写一句注释,向自己解释这行代码的作用。
print("I will now count my chickens:")#print() 方法用于打印输出,是python中最常见的一个函数。print("Hens", 25 + 30 / 6)#输出Hens,以及计算结果,数值类型可以直接输出print("Roosters", 100 - 25 * 3 % 4)#输出Roosters以及计算结果print("Now I will count the eggs:")#输出字符串Now I will count the eggs:print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)#输出计算结果print('Is it true that 3+2<5-7?')#输出字符串Is it true that 3+2<5-7?print(3+2<5-7)#输出计算结果print('What is 3+2?',3+2)#输出字符串及运算结果,输出多个对象时,需要用 , 分隔print('What is 5-7?',5-7)#输出字符串及运算结果print("Oh, that is why it's false.")#输出字符串print('How about some more.')#输出字符串print('Is it greater?',5>-2)#输出字符串及判断结果print('Is it greater or equal?',5>=-2)#输出字符串及判断结果print('Is it less or equal?',5<=2)#输出字符串及判断结果
(2)找一些你需要计算的东西,然后写一个新的.py文件
print(26/5)print(22//5)#整除操作符print(20*5)print(99/30*6%1)print(>=25*90+30)print(>25*90+228)print(<=25*90+228)
输出
5.241000.7999999999999972FalseFalseTrue
(3)用浮点数重新写一下`,让它更精确一些,比如 20.0 就是一个浮点数。
print(26.0/5)print(22.9//5)print(20*5)print(99.3/30*6%1)print(.8>=25.0*90.0+30.22)print(<25.9*90+228)print(<=25.32*90+228.5)
输出
5.24.01000.8599999999999994FalseTrueTrue
二、字符串和文本
1、字符的引用
cars = 100space_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_car = passengers / cars_drivenprint("There are", cars, "cars available.")print("There are only", drivers, "drivers available.")print("There will be", cars_not_driven, "empty cars today.")print("We can transport", carpool_capacity, "people today.")print("We have", passengers, "to carpool today.")print("We need to put about", average_passengers_per_car, "in each car.")
附加练习,针对教程中给出的例子改写为自己的信息
my_name='Xinran Wang'my_age=23my_height=168#centimetermy_weight=57#kilogrammy_eyes='black'my_teeth='white'my_hair='black'print(f"let's talk about {my_name}.")print(f"She is {my_height} centimeters tall.")print(f"She is {my_weight} kilogram heavy.")print("Actually that is not too heavy.")print(f"She's got {my_eyes} eyes and {my_hair} hair.")print(f"Her teeth are usually {my_teeth} depending on the coffee")total=my_age+my_weight+my_heightprint(f"If I add {my_age},{my_height},and {my_weight} I got {total}.")
输出
let's talk about Xinran Wang.She is 168 centimeters tall.She is 57 kilogram heavy.Actually that is not too heavy.She's got black eyes and black hair.Her teeth are usually white depending on the coffeeIf I add 23,168,and 57 I got 248.
2、输入一整段字符串、变量和格式
通过练习例子逐步了解程序员的缩写和代码习惯
types_of_people = 10x = f"There are {types_of_people} types of people."binary = "binary"do_not = "don't"y = f"Those who know {binary} and those who {do_not}."print(x)print(y)print(f"I said: {x}")print(f"I also said: '{y}'")hilarious = Falsejoke_evaluation = "Isn't that joke so funny?! {}"print(joke_evaluation.format(hilarious))w = "This is the left side of..."e = "a string with a right side."print(w + e)
输出
There are 10 types of people.Those who know binary and those who don't.I said:There are 10 types of people.I also said:'Those who know binary and those who don't'Isn't that joke so funny?!FalseThis is the left side of...a string with a right side.
附加练习
(1)复习一遍这个程序,并在每一行上面写上注释来解释它。
types_of_peple=10#给变量types_of_peple赋值x=f"There are {types_of_peple} types of people."#给变量x赋值字符串binary="binary"#给变量binary赋值字符串do_not="don't"#给变量do_not赋值字符串y=f"Those who know {binary} and those who {do_not}"##给变量y赋值字符串print(x)#输出变量xprint(y)#输出变量yprint(f"I said:{x}")#输出字符串,字符串中含有变量xprint(f"I also said:'{y}'")#输出字符串,字符串中含有变量yhilarious=False#给变量hilarious赋值布尔类型的值joke_evaluation="Isn't that joke so funny?!{}"#给变量joke_evaluation赋值为字符串print(joke_evaluation.format(hilarious))#format用于字符串的格式化w="This is the left side of..."#给变量w赋值字符串e="a string with a right side."#给变量e赋值字符串print(w+e)#输出两个变量相加的结果
输出
There are 10 types of people.Those who know binary and those who don'tI said:There are 10 types of people.I also said:'Those who know binary and those who don't'Isn't that joke so funny?!FalseThis is the left side of...a string with a right side.
(2)找到所有把字符串放在字符串里面的地方,一共有 4 处。
#y=f"Those who know {binary} and those who {do_not}"#两个{}是两处#print(f"I said:{x}")#输出字符串,字符串中含有变量x,x也是由字符串赋值#print(f"I also said:'{y}'")#输出字符串,字符串中含有变量y,y也是由字符串赋值
(3)你确定有 4 处吗?你怎么知道?也许我爱撒谎呢。
首先确定变量赋值为字符串的,分别为五处,分别是x、y、binary、do_not、joke_evaluation然后观察{}出现的位置,观察{}中的数据类型即可判断。
(4)解释一下为什么把 w 和 e 两个字符串用 + 连起来能够弄成一个更长的字符串。
相当于使用了add函数,用于给集合添加元素。
常见问题
为什么你在一些字符串外面放的是单引号,而其他的不是?
大多数是因为格式。但是如果一个字符串已经用了双引号,我就会在这个字符串里面用单引号,看看第 6 行和第 15 行你就知道了。
如果你觉得一个笑话很好笑,可以写 hilarious = True 吗?
可以的,你会在后面学习到这些布尔值。
三、列表
列表 List(列表) 是 Python 中使用最频繁的数据类型。
•列表可以完成大多数集合类的数据结构实现。支持字符,数字,字符串甚至可以包含列表(即嵌套)。
•列表用 [ ] 标识,是 python 最通用的复合数据类型。
•列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。
下面通过教程中的例子进行练习
list=['runoob',786,2.33,'join',70.2]tinylist=[123,'john']print(list)#输出完整列表print(list[0])#输出列表的第一个元素print(list[1:3])#输出列表的第二至第三列元素print(list[2:])#输出从第三个开始至列表末尾的所有元素print(tinylist*2)#输出列表两次print(list+tinylist)#打印组合的列表
输出
['runoob', 786, 2.33, 'join', 70.2]runoob[786, 2.33][2.33, 'join', 70.2][123, 'john', 123, 'john']['runoob', 786, 2.33, 'join', 70.2, 123, 'john']
附加练习
结合列表的相关知识,对列表的操作进行练习
首先定义一个list,下面的操作也由此进行演示。
#首先定义一个list,下面的操作也将用这个list展开list=['guailing','suyiming','wudajing','xumengtao','renziwei']
(1)索引
print(list[1]) ##输出第二个元素print(list[-1]) ##输出最后一个元素
输出
suyimingrenziwei
(2)切片
print(list[1:]) ##打印第一个元素之后的内容print(list[:-1]) ##打印最后一个元素之前的内容print(list[0:-1]) #打印最后一个元素之前的内容print(list[2:5]) #打印第三个到第六个元素的内容print(list[::-1]) ##倒序输出
输出
['suyiming', 'wudajing', 'xumengtao', 'renziwei']['guailing', 'suyiming', 'wudajing', 'xumengtao']['guailing', 'suyiming', 'wudajing', 'xumengtao']['wudajing', 'xumengtao', 'renziwei']['renziwei', 'xumengtao', 'wudajing', 'suyiming', 'guailing']
(3)连接
list1 = ['gaotingyu','liwenlong']print(list+list1)
输出
['guailing', 'suyiming', 'wudajing', 'xumengtao', 'renziwei', 'gaotingyu', 'liwenlong']
(4)成员操作赋
print('wangmeng' in list) #判断wangmeng是否存在list中print('suyiming'in list)#判断suyiming是否在list中
输出
FalseTrue
(5)迭代,使用for循环
for i in list:print(i)#遍历输出每个元素
输出
guailingsuyimingwudajingxumengtaorenziwei
(6)列表嵌套
list2 = [list,list1]print(list2[1][1]) ##第二个元素中的第二个元素print(list2[:][1]) ##第二个元素print(list2[:][1][1]) ##第二个元素中的第二个元素
输出
liwenlong['gaotingyu', 'liwenlong']liwenlong
(7)列表的增加,下面用了两种方法
print(list + ['gaotingyu']) ##用连接的方式list2.append('Yuzuru Hanyu') print(list2) ##append:追加一个元素到列表中
输出
['guailing', 'suyiming', 'wudajing', 'xumengtao', 'renziwei', 'gaotingyu'][['guailing', 'suyiming', 'wudajing', 'xumengtao', 'renziwei'], ['gaotingyu', 'liwenlong'], 'Yuzuru Hanyu']
(8)删除
list.remove('wudajing') ##指定删除对象的名字 listdel list2 ##删除列表
(9)乱序
import randomrandom.shuffle(list) ##随机打乱print(list)
输出
['renziwei', 'guailing', 'xumengtao', 'suyiming']
(10)查看出现次数
list.count('guailing')
输出
1
四、总结
本次任务主要对python中的基本数学运算、字符串、文本、列表的相关知识进行了梳理,完成了教程中的练习,最大的感受是一些我以为很基本常见的操作,其实还是不熟练的,有的打出来还会报错,原因就在于之前的一些代码并没有上手练习,这也让我感受到了task1中“必须手动将每个练习打出来”的意义。
参考链接:/datawhale-homepage/index.html#/learn/detail/6