Python 基本语法,操作,列表生成式;兰州大学2019级编程考试复习(一)
python 程序通常以函数形式组织:
def hello():
print('Hello World!')
Many expressions and operators
- 算术运算符
-
-
-
- /
- % 取模 - 返回除法的余数
- ** 幂 - 返回x的y次幂
- // 取整除 - 返回商的整数部分(向下取整)
- 比较运算符
- ==
- !=
- <
- <=
-
=
- and
- or
- not
If,While,For循环
for VS while:
for :to transverse every element in a range of values,list,dictionary,collections in general.
遍历列表,字典,collection,可迭代对象中的每个元素
while:repeat a sequence of statements while a certain conditions is true.
当条件满足时,循环执行同样一个语句块
列表生成式(摘自[https://blog.csdn.net/qq_40628106/article/details/85039366])
列表生成式是用来创建列表的生成式。
格式
variable = ['列表里面的元素' for out_exp in '可以迭代的对象,如列表' if out_exp == 2]
例子:
li2 = [i for i in range(0,10,2)]#[0,2,4,6,8]
li3 = [i +j,for i in range(0,10,2