欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Task2运算

时间:2023-04-28
2、基础

基础部分,我会选择Python的主要部分进行讲解,以及提供深入的练习,以及还有很多指令,大家可以自己选择感兴趣或者要用到的进行补充,这部分学习的思想主要是让大家学会自学编程语言

(1)实现第一行代码和认识注释

写出你的第一行代码向世界问好

print('hallo world')

认识注释,注释是由# 加相关备注,但是不会在代码中运行,可以作为帮助理解的功能

1 # A comment, this is so you can read your program later.2 # Anything after the # is ignored by python、34 print("I could have code like this.") # and the comment after 56 # You can also use a comment to "disable" or comment out code7 # print("This won't run.") 89 print("This will run.")

(2)数学运算 认识运算符

加减乘除等以及特殊符号

• + plus,加号
• - minus,减号
• / slash,斜杠
• * asterisk,星号
• % percent,百分号
• < less-than,小于号
• > greater-than,大于号
• <= less-than-equal,小于等于号
• >= greater-than-equal,大于等于号

1 print("I will now count my chickens:")23 print("Hens", 25 + 30 / 6)4 print("Roosters", 100 - 25 * 3 % 4)56 print("Now I will count the eggs:")78 print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)910 print("Is it true that 3 + 2 < 5 - 7?")1112 print(3 + 2 < 5 - 7)1314 print("What is 3 + 2?", 3 + 2)15 print("What is 5 - 7?", 5 - 7)1617 print("Oh, that's why it's False.")1819 print("How about some more.")2021 print("Is it greater?", 5 > -2)22 print("Is it greater or equal?", 5 >= -2)23 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's why it's False、How about some more.Is it greater? TrueIs it greater or equal? TrueIs it less or equal? False

自我练习

在每一行上面,用 # 写一句注释,向自己解释这行代码的作用。找一些你需要计算的东西,然后写一个新的 .py 文件。用浮点数重新写一下`,让它更精确一些,比如 20.0 就是一个浮点数。 (3)字符串和文本

字符如何引用

1 cars = 1002 space_in_a_car = 4.03 drivers = 304 passengers = 905 cars_not_driven = cars - drivers6 cars_driven = drivers7 carpool_capacity = cars_driven * space_in_a_car8 average_passengers_per_car = passengers / cars_driven 91011 print("There are", cars, "cars available.")12 print("There are only", drivers, "drivers available.")13 print("There will be", cars_not_driven, "empty cars today.")14 print("We can transport", carpool_capacity, "people today.")15 print("We have", passengers, "to carpool today.")16 print("We need to put about", average_passengers_per_car,17 "in each car.")

自己的信息引用

1 my_name = 'Zed A、Shaw'2 my_age = 35 # not a lie3 my_height = 74 # inches4 my_weight = 180 # lbs5 my_eyes = 'Blue'6 my_teeth = 'White'7 my_hair = 'Brown'89 print(f"Let's talk about {my_name}.")10 print(f"He's {my_height} inches tall.")11 print(f"He's {my_weight} pounds heavy.")12 print("Actually that's not too heavy.")13 print(f"He's got {my_eyes} eyes and {my_hair} hair.")14 print(f"His teeth are usually {my_teeth} depending on the coffee.")1516 # this line is tricky, try to get it exactly right17 total = my_age + my_height + my_weight18 print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")

附加练习

尝试改成自己的信息

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

程序员都喜欢使用简短的缩写来节省时间,但是那些缩写在你看来会十分晦涩难懂。所以我们得尽早开始学习阅读和书写这些东西。

1 types_of_people = 102 x = f"There are {types_of_people} types of people."34 binary = "binary"5 do_not = "don't"6 y = f"Those who know {binary} and those who {do_not}."78 print(x)9 print(y)1011 print(f"I said: {x}")12 print(f"I also said: '{y}'")1314 hilarious = False15 joke_evaluation = "Isn't that joke so funny?! {}"1617 print(joke_evaluation.format(hilarious))1819 w = "This is the left side of..."20 e = "a string with a right side."2122 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、复习一遍这个程序,并在每一行上面写上注释来解释它。
2、找到所有把字符串放在字符串里面的地方,一共有 4 处。
3、你确定有 4 处吗?你怎么知道?也许我爱撒谎呢。
4、解释一下为什么把 w 和 e 两个字符串用 + 连起来能够弄成一个更长的字符串。

把代码打乱

你现在已经可以把代码打乱了。把它当成一个游戏,用一种最聪明或者最简单的方式把代码打乱。打乱之后,你需要修复它们。如果你跟你的朋友一起学习,你们可以相互打乱对方的代码,然后再试着修复它。把你的代码发给你的队友,让他们打乱,然后你再试着找出它们的错误,并修复它。记住,如果你已经写了一遍这些代码了,你可以再写一次。如果你打乱得太彻底了,就试着重新写一遍。

常见问题

为什么你在一些字符串外面放的是单引号,而其他的不是?大多数是因为格式。但是如果一个字符串已经用了双引号,我就会在这个字符串里面用单引号,看看第 6 行和第 15 行你就知道了。

如果你觉得一个笑话很好笑,可以写 hilarious = True 吗? 可以的,你会在后面学习到这些布尔值。

(4)列表

List(列表) 是 Python 中使用最频繁的数据类型。

列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。

列表用 [ ] 标识,是 python 最通用的复合数据类型。

列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。

2、基础基础部分,我会选择Python的主要部分进行讲解,以及提供深入的练习,以及还有很多指令,大家可以自己选择感兴趣或者要用到的进行补充,这部分学习的思想主要是让大家学会自学编程语言(1)实现第一行代码和认识注释写出你的第一行代码向世界问好print('hallo world')认识注释,注释是由# 加相关备注,但是不会在代码中运行,可以作为帮助理解的功能1 # A comment, this is so you can read your program later.2 # Anything after the # is ignored by python、34 print("I could have code like this.") # and the comment after 56 # You can also use a comment to "disable" or comment out code7 # print("This won't run.") 89 print("This will run.")(2)数学运算认识运算符加减乘除等以及特殊符号• + plus,加号• - minus,减号• / slash,斜杠• * asterisk,星号• % percent,百分号• < less-than,小于号• > greater-than,大于号• <= less-than-equal,小于等于号• >= greater-than-equal,大于等于号1 print("I will now count my chickens:")23 print("Hens", 25 + 30 / 6)4 print("Roosters", 100 - 25 * 3 % 4)56 print("Now I will count the eggs:")78 print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)910 print("Is it true that 3 + 2 < 5 - 7?")1112 print(3 + 2 < 5 - 7)1314 print("What is 3 + 2?", 3 + 2)15 print("What is 5 - 7?", 5 - 7)1617 print("Oh, that's why it's False.")1819 print("How about some more.")2021 print("Is it greater?", 5 > -2)22 print("Is it greater or equal?", 5 >= -2)23 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's why it's False、How about some more.Is it greater? TrueIs it greater or equal? TrueIs it less or equal? False自我练习在每一行上面,用 # 写一句注释,向自己解释这行代码的作用。找一些你需要计算的东西,然后写一个新的 .py 文件。用浮点数重新写一下`,让它更精确一些,比如 20.0 就是一个浮点数。(3)字符串和文本字符如何引用1 cars = 1002 space_in_a_car = 4.03 drivers = 304 passengers = 905 cars_not_driven = cars - drivers6 cars_driven = drivers7 carpool_capacity = cars_driven * space_in_a_car8 average_passengers_per_car = passengers / cars_driven 91011 print("There are", cars, "cars available.")12 print("There are only", drivers, "drivers available.")13 print("There will be", cars_not_driven, "empty cars today.")14 print("We can transport", carpool_capacity, "people today.")15 print("We have", passengers, "to carpool today.")16 print("We need to put about", average_passengers_per_car,17 "in each car.")自己的信息引用1 my_name = 'Zed A、Shaw'2 my_age = 35 # not a lie3 my_height = 74 # inches4 my_weight = 180 # lbs5 my_eyes = 'Blue'6 my_teeth = 'White'7 my_hair = 'Brown'89 print(f"Let's talk about {my_name}.")10 print(f"He's {my_height} inches tall.")11 print(f"He's {my_weight} pounds heavy.")12 print("Actually that's not too heavy.")13 print(f"He's got {my_eyes} eyes and {my_hair} hair.")14 print(f"His teeth are usually {my_teeth} depending on the coffee.")1516 # this line is tricky, try to get it exactly right17 total = my_age + my_height + my_weight18 print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")附加练习尝试改成自己的信息输入一整段字符串、变量和格式程序员都喜欢使用简短的缩写来节省时间,但是那些缩写在你看来会十分晦涩难懂。所以我们得尽早开始学习阅读和书写这些东西。1 types_of_people = 102 x = f"There are {types_of_people} types of people."34 binary = "binary"5 do_not = "don't"6 y = f"Those who know {binary} and those who {do_not}."78 print(x)9 print(y)1011 print(f"I said: {x}")12 print(f"I also said: '{y}'")1314 hilarious = False15 joke_evaluation = "Isn't that joke so funny?! {}"1617 print(joke_evaluation.format(hilarious))1819 w = "This is the left side of..."20 e = "a string with a right side."2122 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、复习一遍这个程序,并在每一行上面写上注释来解释它。2、找到所有把字符串放在字符串里面的地方,一共有 4 处。3、你确定有 4 处吗?你怎么知道?也许我爱撒谎呢。4、解释一下为什么把 w 和 e 两个字符串用 + 连起来能够弄成一个更长的字符串。把代码打乱你现在已经可以把代码打乱了。把它当成一个游戏,用一种最聪明或者最简单的方式把代码打乱。打乱之后,你需要修复它们。如果你跟你的朋友一起学习,你们可以相互打乱对方的代码,然后再试着修复它。把你的代码发给你的队友,让他们打乱,然后你再试着找出它们的错误,并修复它。记住,如果你已经写了一遍这些代码了,你可以再写一次。如果你打乱得太彻底了,就试着重新写一遍。常见问题为什么你在一些字符串外面放的是单引号,而其他的不是?大多数是因为格式。但是如果一个字符串已经用了双引号,我就会在这个字符串里面用单引号,看看第 6 行和第 15 行你就知道了。如果你觉得一个笑话很好笑,可以写 hilarious = True 吗? 可以的,你会在后面学习到这些布尔值。(4)列表List(列表) 是 Python 中使用最频繁的数据类型。列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。列表用 [ ] 标识,是 python 最通用的复合数据类型。列表中值的切割也可以用到变量 [头下标:尾下标] ,就可以截取相应的列表,从左到右索引默认 0 开始,从右到左索引默认 -1 开始,下标可以为空表示取到头或尾。list = [ 'runoob', 786 , 2.23, 'john', 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.23, 'john', 70.2]runoob[786, 2.23][2.23, 'john', 70.2][123, 'john', 123, 'john']['runoob', 786, 2.23, 'john', 70.2, 123, 'john']附加练习搜索列表的相关知识,看还有什么操作list = [ 'runoob', 786 , 2.23, 'john', 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.23, 'john', 70.2]runoob[786, 2.23][2.23, 'john', 70.2][123, 'john', 123, 'john']['runoob', 786, 2.23, 'john', 70.2, 123, 'john']

附加练习

搜索列表的相关知识,看还有什么操作

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。