#INT104 #Python
这次是 INT104 的第二节tutorial,
主要讲了一些操作符,if-else,循环和函数。
还是仅总结py特色的一些语法,比较简单不做详细解释。
(其实老师给的notebook上面都有,看那个也行)
Arithmetic operations(算术操作符)

需要注意的是,python中求次方是用 **
,还有一个操作符://
,用于执行 整数除法 操作。它将两个操作数(被除数和除数)转换为整数,然后进行除法运算,并返回 商的整数部分 。
python
1 | 4 ** 2 # 16 |
Logical operators(逻辑运算符)
python
1 | x or y # 或 |
if-else
python
1 | if condition1: # the condition should be bool |
For loop (For 循环)
python
1 | for item in itemList: |
While loop (While 循环)
python
1 | n = 0 |
break & continue
- Break: 当代码运行到
break
时,直接终止循环。
python
1 | n = 0 |
- Continue: 当代码运行到
continue
时,终止本次循环,进入下一轮循环。
python
1 | num = 0 |
Functions (函数)
python
1 | def functionName([argument1, argument2, ..., argumentN]): # argument is [opentional] |
- Default and Optional arguments:
- Default 参数,也称为默认值参数,允许函数在调用时不提供某些参数值。如果调用时没有提供这些参数值,则使用函数定义时指定的默认值。
python
1 | def my_function(a, b=10): |
- Optional 参数,也称为可选参数,允许函数在调用时不提供某些参数值。如果调用时没有提供这些参数值,则这些参数的值将为 None。
python
1 | from typing import Optional |
- Return multiple value:
- python中,一个函数可以返回多个值,作为一个元组或者一个字典。
python
1 | def my_function(): |
different print
python
1 | str1 = "the string class has" |
Placeholders(占位符) & class(类)
其实这些notebook上都有……
直接看老师给的notebook吧(逃
如有错误,请及时指出~评论发邮件均可,欧内盖!
0/500