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.0
Roosters 98
now 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.0
Roosters 97
Now I will count the eggs:
6.75
is it true that 3+2<5-7?
False
what is 3+2? 5
what is 5-7? -2
oh, that is why it's false.
how about some more.
is it greater? True
is it greater or equal? True
is 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(2022>=25*90+30)
print(2022>25*90+228)
print(2022<=25*90+228)

输出

5.2
4
100
0.7999999999999972
False
False
True

(3)用浮点数重新写一下`,让它更精确一些,比如 20.0 就是一个浮点数。

print(26.0/5)
print(22.9//5)
print(20*5)
print(99.3/30*6%1)
print(2022.8>=25.0*90.0+30.22)
print(2022<25.9*90+228)
print(2022<=25.32*90+228.5)

输出

5.2
4.0
100
0.8599999999999994
False
True
True

二、字符串和文本

1、字符的引用

cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven     print("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=23
my_height=168#centimeter
my_weight=57#kilogram
my_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_height
print(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 coffee
If I add 23,168,and 57 I got 248.

2、输入一整段字符串、变量和格式

通过练习例子逐步了解程序员的缩写和代码习惯

types_of_people = 10
x = 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 = False
joke_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?!False
This 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)#输出变量x
print(y)#输出变量y
print(f"I said:{x}")#输出字符串,字符串中含有变量x
print(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'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?!False
This 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])   ##输出最后一个元素

输出

suyiming
renziwei

(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中

输出

False
True

(5)迭代,使用for循环

for i in list:print(i)#遍历输出每个元素

输出

guailing
suyiming
wudajing
xumengtao
renziwei

(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') ##指定删除对象的名字
list
del list2  ##删除列表

(9)乱序

import random
random.shuffle(list)   ##随机打乱
print(list)

输出

['renziwei', 'guailing', 'xumengtao', 'suyiming']

(10)查看出现次数

list.count('guailing')

输出

1

四、总结

本次任务主要对python中的基本数学运算、字符串、文本、列表的相关知识进行了梳理,完成了教程中的练习,最大的感受是一些我以为很基本常见的操作,其实还是不熟练的,有的打出来还会报错,原因就在于之前的一些代码并没有上手练习,这也让我感受到了task1中“必须手动将每个练习打出来”的意义。
参考链接:https://linklearner.com/datawhale-homepage/index.html#/learn/detail/6

零基础学python编程思维---Task2 数学运算、字符串和文本、列表相关推荐

  1. 【组队学习】【34期】零基础学python编程思维

    零基础学python编程思维 航路开辟者:邓林权 领航员:沈一 航海士:覃嘉俊.马子阳.左凯文 基本信息 开源内容:https://linklearner.com/datawhale-homepage ...

  2. 零基础学python编程思维(九) | 面向对象的编程——类

    对象可以包含任意数量和类型的数据.Python在尽可能不增加新的语法和语义的情况下加入了类机制,类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以 ...

  3. 0基础学python看什么书-零基础学python编程需要看什么书?

    原标题:零基础学python编程需要看什么书? 随着互联网迅速发展,python也呈现出水涨船高的态势.近年来,python在编程语言中一直名列前茅.编程初学者纷纷选择Python作为第一语言.Pyt ...

  4. python语言程序设计难不难_零基础学Python编程开发难度大吗?从哪学起?

    转行零基础学Python编程开发难度大吗?从哪学起? 近期很多小伙伴问我,如果自己转行学习Python,完全0基础能否学会呢?Python的难度到底有多大? 今天,小编就来为大家详细解读一下这个问题. ...

  5. python 少儿趣味编程下载_零基础学Python编程(少儿趣味版)

    本书是一本少儿编程入门书,适合零基础的读者.本书以"派森号"飞船和西西船长等人的童话故事为载体,从头开始介绍了Python语言的基础语法.全书共有6个章节.每章都有约十个独立的内容 ...

  6. 编程软件python-零基础学Python编程需要安装什么软件?

    前言 Python现在非常火,语法简单而且功能强大,很多同学都想学Python!所以小的给各位看官们准备了高价值Python学习视频教程及相关电子版书籍,都放在了文章结尾,欢迎前来领取! 今天想要跟大 ...

  7. 零基础学python大概要多久-零基础学Python要多久

    随着Python就业的逐渐火热,我们会看见很多转行零基础学Python编程开发拿上万月薪的新闻,这让很多IT专业甚至非IT专业的同学蠢蠢欲动,想自己也去尝试一下.但是在转行学习Python之前,同学们 ...

  8. 编程没基础学python多长时间-零基础学Python的过程有多难?需要多久?

    Python是一门简单高效,应用范围广泛的计算机语言.先我们要知道Python已经算是一门相对其他编程语言而言,最适合零基础新手学习的开发语言,换句话说,如果连Python都学不进去就不要考虑进入IT ...

  9. 编程没基础学python多长时间--零基础学Python,从入门到精通需要多长时间

    求一份小甲鱼的<零基础入门学习Python>视频教程 评论 本系列教程面向础的同学,是一个深入浅通俗易懂的Python3视频教程. 适群 完全零基础入门,不需要任何前置知识. 教程概述 前 ...

最新文章

  1. vue.js+socket.io打造一个好玩的新闻社区
  2. C语言 游戏远程call调用,创建远程线程 调用游戏所有call
  3. idou老师教你学Istio 04:Istio性能及扩展性介绍
  4. MySQL中实现rownum功能类似的语句
  5. 蒙文字体怎么安装_蒙文搜索APP内置键盘布局图
  6. JDK和CGLIB生成动态代理类的区别
  7. python cmath模块_python-cmath模块
  8. 5.1 Lilypond五线谱
  9. 如何用SolidWorks软件绘制三维模型?
  10. 如何免费将PDF旋转并保存成功?
  11. 美通企业日报 | 阿里收购网易考拉入股网易云音乐;宁德时代与博世达成战略合作...
  12. 并发和并行的区别(图解)
  13. 【小强推歌】---奥斯卡金曲MP3[下载]
  14. [网络]网速很快但是访问网页很慢的两种解决办法
  15. 微信小程序wx.getUserInfo获取用户所在地区将拼音转换为中文的方法
  16. 【夏季养生以心为大】
  17. hdu1429 胜利大逃亡(续)
  18. 电话测试压力软件,1-60T简单压力测试仪器/简单的测试压力工具
  19. 本科课程【java程序设计】实验2 - 类与对象编程练习
  20. 说到修图这件事,你还真是比不上AI

热门文章

  1. 我的Python程序太慢了。如何加快速度?
  2. 俄罗斯小吃奥利维耶沙拉是怎么做的
  3. easyUI下拉框默认选中和联动、内容回显
  4. Protein Actions Principles and Modeling》-《蛋白质作用原理和建模》中文分享(3)
  5. 对话框 QDialog
  6. 【Renesas RA6M4开发板之按键和LED的GPIO】
  7. Linux shell中使用sed 替换\n换行符 (多行边一行、一行变多行)
  8. 华为5731交换机设置telnet登录
  9. pcb过孔漏铜_PCB板孔无铜的缺陷及原因分析
  10. 2011,我的求职攻略