range(1, 10)
stop = 10
range(stop=stop) ## it means range(0,10)step = 2
range(1, stop=stop, step=step) # it means range from 1 to 10 step by 2
1
2
3
4
5
6. 条件语句
## python 用缩进表示代码块
age = input("input your age\n")
if int(age) > 18:
print("already 成年")
elif int(age) > 30:
print("30 more")
else:
print('<18')
print('out of if else ')
# A exercise #
L = [
['Apple', 'Google', 'Microsoft'], ['Java', 'Python', 'Ruby', 'PHP'], ['Adam', 'Bart', 'Lisa']
]
print(L[0][0])
print(L[1][1])
print(L[2][2])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
7.循环语句
for i in range(0, 7):
print("hello world")
# while a<10 and b<20
t = turtle.Pen()
for i in range(0, 4):
t.forward(100)
t.left(90)
t = (1, 2)
## while 打印0-9 的数据'''
while True:
print("always print True")
'''
conditions = 0while conditions < 10:
print(conditions)
conditions += 1
''' 如果执行该脚本的时候,该 if 判断语句将会是 True,那么内部的代码将会执行。 如果外部调用该脚本,if 判断语句则为 False,内部代码将不会执行。 '''
if __name__ == '__main__':
#code_here print("测试使用的")
1
2
3
4
5
6
7
14.3 可变参数
defreport(name, *grades):
total_grade = 0for grade in grades:
total_grade += grade
print(name, 'total grade is ', total_grade)
report('wang', 12,14,16,20)
1
2
3
4
5
6
14.4 关键字参数
## universal_func(*args, **kw) ==> 可以代表任何函数defportrait(name, **kw):
print('name is', name)
for k, v in kw.items():
print(k, v)
print(portrait('Mike', age=24, country='China', education='bachelor'))
1
2
3
4
5
6
15. 全局变量
global_param = 100deffor_local():
local_parm = 20
print("value of local_parm is %d" % local_parm)
print("value of global_parm is %d" % global_param)
# print("local_parm does not exit" % local_parm)
print("value of global_parm is %d" % global_param)
[x * x for x in range(1, 11) if x % 2 == 0]
[m + n for m in'ABC'for n in'XYZ']
import os # 导入os模块,模块的概念后面讲到
[d for d inos.listdir('.')] # os.listdir可以列出文件和目录
1
2
3
4
5
6
16.2 面对对象编程
classCalculator:#首字母要大写,冒号不能缺
name = 'Good Calculator'#该行为class的属性
price = 18defadd(self,x,y):
print(self.name)
result = x + y
print(result)
defminus(self,x,y):
result=x-y
print(result)
deftimes(self,x,y):
print(x*y)
defdivide(self,x,y):
print(x/y)
'''
cal=Calculator() #注意这里运行class的时候要加"()",否则调用下面函数的时候会出现错误,导致无法调用.
cal.name
cal.add(10,20)
'''# __init__可以理解成初始化class的变量,取自英文中initial 最初的意思.可以在运行时,给初始值附值,# 这里的下划线是双下划线### 运行c=Calculator('bad calculator',18,17,16,15),然后调出每个初始值的值。看如下代码。classCalculator:
name = 'good calculator'
price = 18def__init__(self,name,price,height,width,weight):# 注意,这里的下划线是双下划线
self.name=name
self.price=price
self.h=height
self.wi=width
self.we=weight
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
17. 异常处理
try:
file=open('eeee.txt','r') #会报错的代码
except Exceptionas e: # 将报错存储在 e 中print(e)
range(1, 10)
stop = 10
range(stop=stop) ## it means range(0,10)step = 2
range(1, stop=stop, step=step) # it means range from 1 to 10 step by 2
1
2
3
4
5
6. 条件语句
## python 用缩进表示代码块
age = input("input your age\n")
if int(age) > 18:
print("already 成年")
elif int(age) > 30:
print("30 more")
else:
print('<18')
print('out of if else ')
# A exercise #
L = [
['Apple', 'Google', 'Microsoft'], ['Java', 'Python', 'Ruby', 'PHP'], ['Adam', 'Bart', 'Lisa']
]
print(L[0][0])
print(L[1][1])
print(L[2][2])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
7.循环语句
for i in range(0, 7):
print("hello world")
# while a<10 and b<20
t = turtle.Pen()
for i in range(0, 4):
t.forward(100)
t.left(90)
t = (1, 2)
## while 打印0-9 的数据'''
while True:
print("always print True")
'''
conditions = 0while conditions < 10:
print(conditions)
conditions += 1
''' 如果执行该脚本的时候,该 if 判断语句将会是 True,那么内部的代码将会执行。 如果外部调用该脚本,if 判断语句则为 False,内部代码将不会执行。 '''
if __name__ == '__main__':
#code_here print("测试使用的")
1
2
3
4
5
6
7
14.3 可变参数
defreport(name, *grades):
total_grade = 0for grade in grades:
total_grade += grade
print(name, 'total grade is ', total_grade)
report('wang', 12,14,16,20)
1
2
3
4
5
6
14.4 关键字参数
## universal_func(*args, **kw) ==> 可以代表任何函数defportrait(name, **kw):
print('name is', name)
for k, v in kw.items():
print(k, v)
print(portrait('Mike', age=24, country='China', education='bachelor'))
1
2
3
4
5
6
15. 全局变量
global_param = 100deffor_local():
local_parm = 20
print("value of local_parm is %d" % local_parm)
print("value of global_parm is %d" % global_param)
# print("local_parm does not exit" % local_parm)
print("value of global_parm is %d" % global_param)
[x * x for x in range(1, 11) if x % 2 == 0]
[m + n for m in'ABC'for n in'XYZ']
import os # 导入os模块,模块的概念后面讲到
[d for d inos.listdir('.')] # os.listdir可以列出文件和目录
1
2
3
4
5
6
16.2 面对对象编程
classCalculator:#首字母要大写,冒号不能缺
name = 'Good Calculator'#该行为class的属性
price = 18defadd(self,x,y):
print(self.name)
result = x + y
print(result)
defminus(self,x,y):
result=x-y
print(result)
deftimes(self,x,y):
print(x*y)
defdivide(self,x,y):
print(x/y)
'''
cal=Calculator() #注意这里运行class的时候要加"()",否则调用下面函数的时候会出现错误,导致无法调用.
cal.name
cal.add(10,20)
'''# __init__可以理解成初始化class的变量,取自英文中initial 最初的意思.可以在运行时,给初始值附值,# 这里的下划线是双下划线### 运行c=Calculator('bad calculator',18,17,16,15),然后调出每个初始值的值。看如下代码。classCalculator:
name = 'good calculator'
price = 18def__init__(self,name,price,height,width,weight):# 注意,这里的下划线是双下划线
self.name=name
self.price=price
self.h=height
self.wi=width
self.we=weight
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
17. 异常处理
try:
file=open('eeee.txt','r') #会报错的代码
except Exceptionas e: # 将报错存储在 e 中print(e)