Exercises
Welcome to your first set of Python coding problems!
If this is your first time using Kaggle Notebooks, welcome!
Notebooks are composed of blocks (called "cells") of text and code. Each of these is editable, though you'll mainly be editing the code cells to answer some questions.
To get started, try running the code cell below (by pressing the ► button, or clicking on the cell and pressing ctrl+enter on your keyboard).
欢迎来到您的第一组 Python 编码问题!
如果这是您第一次使用 Kaggle Notebooks,欢迎!
笔记本由文本和代码块(称为“单元”)组成。 其中每一个都是可编辑的,尽管您主要是编辑代码单元格来回答一些问题。
首先,尝试运行下面的代码单元格(通过按 ► 按钮,或单击单元格并按键盘上的 ctrl+enter 键)。
print("You've successfully run some Python code")
print("Congratulations!")
You've successfully run some Python code
Congratulations!
Try adding another line of code in the cell above and re-running it.
Now let's get a little fancier: Add a new code cell by clicking on an existing code cell, hitting the escape key, and then hitting the a
or b
key. The a
key will add a cell above the current cell, and b
adds a cell below.
Great! Now you know how to use Notebooks.
Each hands-on exercise starts by setting up our feedback and code checking mechanism. Run the code cell below to do that. Then you'll be ready to move on to question 0.
尝试在上面的单元格中添加另一行代码并重新运行它。
现在让我们变得更奇特一些:通过单击现有代码单元,按Esc
键,然后按“a”或“b”键来添加新的代码单元。 “a”键将在当前单元格上方添加一个单元格,“b”键将在当前单元格下方添加一个单元格。
很棒! 现在您知道如何使用笔记本了。
每个实践练习都从设置我们的反馈和代码检查机制开始。 运行下面的代码单元来执行此操作。 然后您就可以继续讨论问题 0。
from learntools.core import binder; binder.bind(globals())
from learntools.python.ex1 import *
print("Setup complete! You're ready to start question 0.")
Setup complete! You're ready to start question 0.
0.
This is a silly question intended as an introduction to the format we use for hands-on exercises throughout all Kaggle courses.
What is your favorite color?
To complete this question, create a variable called color
in the cell below with an appropriate value. The function call q0.check()
(which we've already provided in the cell below) will check your answer.
0
这是一个愚蠢的问题,旨在介绍我们在所有 Kaggle 课程中用于实践练习的格式。
什么是你最喜欢的颜色?
要完成此问题,请在下面的单元格中创建一个名为“color”的变量并指定适当的值。 函数调用“q0.check()”(我们已经在下面的单元格中提供)将检查您的答案。
# create a variable called color with an appropriate value on the line below
# (Remember, strings in Python must be enclosed in 'single' or "double" quotes)
color = "blue"
# Check your answer
q0.check()
Correct: What?! You got it right without needing a hint or anything? Drats. Well hey, you should still continue to the next step to get some practice asking for a hint and checking solutions. (Even though you obviously don't need any help here.)
Didn't get the right answer? How do you not even know your own favorite color?!
Delete the #
in the line below to make one of the lines run. You can choose between getting a hint or the full answer by choosing which line to remove the #
from.
Removing the #
is called uncommenting, because it changes that line from a "comment" which Python doesn't run to code, which Python does run.
没有得到正确答案? 你怎么连自己最喜欢的颜色都不知道?!
删除下面一行中的“#”以使其中一行运行。 您可以通过选择要从中删除“#”的行来选择获取提示还是完整答案。
删除“#”称为取消注释,因为它将该行从 Python 不运行的“注释”更改为 Python 运行的代码。
#q0.hint()
#q0.solution()
The upcoming questions work the same way. The only thing that will change are the question numbers. For the next question, you'll call q1.check()
, q1.hint()
, q1.solution()
, for question 2, you'll call q2.check()
, and so on.
接下来的问题也以同样的方式进行。 唯一会改变的是问题编号。 对于下一个问题,您将调用 q1.check()
、q1.hint()
、q1.solution()
,对于问题 2,您将调用 q2.check()
,然后继续。
1.
Complete the code below. In case it's helpful, here is the table of available arithmetic operations:
Operator | Name | Description |
---|---|---|
|
||
|
||
|
||
|
||
, removing fractional parts |
||
|
||
|
||
|
1.
完成下面的代码。 如果有帮助,这里是可用算术运算表:
操作符 | 名称 | 描述 |
---|---|---|
的总和 |
||
的差 |
||
的乘积 |
||
的商 |
||
的商,删除小数部分 |
||
后的整数余数 |
||
次幂 |
||
的相反数 |
pi = 3.14159 # approximate
diameter = 3
# Create a variable called 'radius' equal to half the diameter
radius = diameter/2.0
# Create a variable called 'area', using the formula for the area of a circle: pi times the radius squared
area = pi * radius ** 2
# Check your answer
q1.check()
Correct
# Uncomment and run the lines below if you need help.
#q1.hint()
# q1.solution()
2.
Add code to the following cell to swap variables a
and b
(so that a
refers to the object previously referred to by b
and vice versa).
2.
将代码添加到以下单元格以交换变量“a”和“b”(以便“a”引用先前由“b”引用的对象,反之亦然)。
########### Setup code - don't touch this part ######################
# If you're curious, these are examples of lists. We'll talk about
# them in depth a few lessons from now. For now, just know that they're
# yet another type of Python object, like int or float.
a = [1, 2, 3]
b = [3, 2, 1]
q2.store_original_ids()
######################################################################
# Your code goes here. Swap the values to which a and b refer.
# If you get stuck, you can always uncomment one or both of the lines in
# the next cell for a hint, or to peek at the solution.
######################################################################
a,b = b,a
# Check your answer
q2.check()
Correct:
The most straightforward solution is to use a third variable to temporarily store one of the old values. e.g.:
tmp = a
a = b
b = tmp
If you've read lots of Python code, you might have seen the following trick to swap two variables in one line:
a, b = b, a
We'll demystify this bit of Python magic later when we talk about tuples.
#q2.hint()
#q2.solution()
3.
a) Add parentheses to the following expression so that it evaluates to 1.
3.
a) 在以下表达式中添加括号,使其计算结果为 1。
(5 - 3) // 2
1
#q3.a.hint()
# Check your answer (Run this code cell to receive credit!)
q3.a.solution()
Solution:
(5 - 3) // 2
Questions, like this one, marked a spicy pepper are a bit harder.
b) 🌶️ Add parentheses to the following expression so that it evaluates to 0
像这样带有辣椒标记的问题有点难。
🌶️ 将括号添加到以下表达式中,使其计算结果为 0
8 - (3 * 2) - (1 + 1)
0
#q3.b.hint()
# Check your answer (Run this code cell to receive credit!)
q3.b.solution()
Solution: (8 - 3) * (2 - (1 + 1))
is one solution. There may be others.
4.
Alice, Bob and Carol have agreed to pool their Halloween candy and split it evenly among themselves.
For the sake of their friendship, any candies left over will be smashed. For example, if they collectively bring home 91 candies, they'll take 30 each and smash 1.
Write an arithmetic expression below to calculate how many candies they must smash for a given haul.
373 / 5,000
Translation results
Translation result
4.
爱丽丝、鲍勃和卡罗尔同意将他们的万圣节糖果集中起来并平均分配。
为了他们的友谊,剩下的糖果都会被砸碎。 例如,如果他们共同带回家 91 颗糖果,他们会每人拿 30 颗并粉碎 1 颗。
在下面写一个算术表达式来计算他们必须粉碎给定收获的糖果数量。
# Variables representing the number of candies collected by alice, bob, and carol
alice_candies = 121
bob_candies = 77
carol_candies = 109
# Your code goes here! Replace the right-hand side of this assignment with an expression
# involving alice_candies, bob_candies, and carol_candies
to_smash = -1
to_smash = (alice_candies + bob_candies + carol_candies) % 3
# Check your answer
q4.check()
Correct
#q4.hint()
q4.solution()
Solution:
(alice_candies + bob_candies + carol_candies) % 3
Keep Going
Next up, you'll learn to write new functions and understand functions others write. This will make you at least 10 times more productive as a Python programmer.
继续前进
接下来,您将学习编写新函数并理解其他人编写的函数。 作为一名 Python 程序员,这将使您的工作效率提高至少 10 倍。