【Kaggle Courses】1-编程介绍
wbfwonderful Lv3

Get started with Python, if you have no coding experience. Click here for more information.

计算和变量

Make calculations, and define and modify variables.

打印

打印一条消息。

1
print('hello')

计算

打印计算结果。

1
print(1 + 2)

注释

使用 # 写注释。

函数

Organize your code and avoid redundancy.

一个简单的例子

1
2
3
def add_three(input_var):
output_var = input_var + 3
return output_var

函数包含两个部分: headerbody.

  • Header: 定义函数名和参数。
  • Body: 定义函数如何工作。

变量作用域

函数内部定义的变量无法在函数外访问。

数据类型

Explore integers, floats, booleans, and strings.

int, float, boolean, string

条件和条件声明

Modify how functions run, depending on the input.
if, if-else, if-elif-else

列表介绍

Organize your data so you can work with it efficiently.

索引

  • 获取最后一个元素: [-1]
  • 获取倒数第二个元素: [-2]

切片

切片:获取列表的一个片段。

  • 获取前 x 个元素:[:x]
  • 获取后 y 个元素:[-y:]

此处“元素”的英文为“entry”。

1
2
3
l = [1, 2, 3, 4, 5]
print(l[:2])
print(l[-2:])
1
2
[1, 2]
[4, 5]

删除

使用 .remove()

添加

使用 .add()