76
76
# grade = '优秀'
77
77
# print("分数是{0},等级是{1}".format(score,grade))
78
78
79
- x = int (input ('输入x轴坐标:' ))
80
- y = int (input ('输入y轴坐标:' ))
81
-
82
- if (x == 0 and y == 0 ):
83
- print ("坐标在原点" )
84
- elif (x == 0 ):
85
- print ("坐标在y轴上" )
86
- elif (y == 0 ):
87
- print ("坐标在x轴上" )
88
- elif (x > 0 and y > 0 ):
89
- print ('坐标在第一象限' )
90
- elif (x < 0 and y > 0 ):
91
- print ('坐标在第二象限' )
92
- elif (x < 0 and y < 0 ):
93
- print ('坐标在第三象限' )
94
- else :
95
- print ('坐标在第四象限' )
79
+ # x = int(input('输入x轴坐标:'))
80
+ # y = int(input('输入y轴坐标:'))
81
+ #
82
+ # if(x==0 and y==0):
83
+ # print("坐标在原点")
84
+ # elif(x==0):
85
+ # print("坐标在y轴上")
86
+ # elif(y==0):
87
+ # print("坐标在x轴上")
88
+ # elif(x>0 and y>0):
89
+ # print('坐标在第一象限')
90
+ # elif(x<0 and y>0):
91
+ # print('坐标在第二象限')
92
+ # elif(x<0 and y<0):
93
+ # print('坐标在第三象限')
94
+ # else:
95
+ # print('坐标在第四象限')
96
+
97
+
98
+ #选择结构的嵌套
99
+ # score = int(input('输入一个在0-100之间的数字:'))
100
+ # grade = ""
101
+ # if score>100 or score<0:
102
+ # score = int(input('输入错误!重新输入一个在0-100之间的数字:'))
103
+ # else:
104
+ # if score>=90:
105
+ # grade = '优秀'
106
+ # elif score>=80:
107
+ # grade = '良好'
108
+ # elif score>=70:
109
+ # grade = '中等'
110
+ # elif score>=60:
111
+ # grade = '及格'
112
+ # else:
113
+ # grade = '不及格'
114
+ #
115
+ # print("分数是{0},等级是{1}".format(score,grade))
116
+
117
+ # score = int(input('输入一个在0-100之间的数字:'))
118
+ # grade = "ABCDE"
119
+ # if score>100 or score<0:
120
+ # score = int(input('输入错误!重新输入一个在0-100之间的数字:'))
121
+ # else:
122
+ # asz = score//10 #
123
+ # if asz<6:asz = 5
124
+ # grade = grade[9-asz]
125
+ # print("分数是{0},等级是{1}".format(score,grade))
126
+
127
+
128
+ #while循环结构_死循环处理
129
+ # num=0
130
+ # while num<=10:
131
+ # print(num,' while循环')
132
+ # num+=1
133
+ # print(num,' while循环结束')
134
+
135
+ num = 1
136
+ sum = 0
137
+ while 0 <= num <= 100 :
138
+ sum += num
139
+ num += 1
140
+
141
+ print (sum ,' while循环结束' )
142
+
143
+ #for循环结构_遍历各种可迭代对象_range对象
144
+ # for x in (20,30,40):
145
+ # print(x*10,end=' ')
146
+ #
147
+ # for y in 'hello':
148
+ # print(y,end=' ')
149
+ # d = {'name':'张','age':20,'address':'北京'}
150
+ # for k in d:
151
+ # print(k,d[k]) #遍历字典的键值对
152
+ #
153
+ # for k in d.keys(): #遍历字典的键
154
+ # print(k)
155
+ #
156
+ # for k in d.values(): #遍历字典的值
157
+ # print(k)
158
+ #
159
+ # for k in d.items(): #遍历字典的键值对
160
+ # print(k)
161
+
162
+ # for x in range(10):
163
+ # print(x,end=' ')
164
+ #
165
+ # for x in range(3,11):
166
+ # print(" ",x)
167
+ #
168
+ # for x in range(3,11,3):
169
+ # print(" ",x)
170
+
171
+
172
+ # sum_nums = 0
173
+ # a_sums = 0
174
+ # b_sums = 0
175
+ #
176
+ # for nums in range(101):
177
+ # sum_nums+=nums
178
+ # print(sum_nums)
179
+ #
180
+ # for nums in range(101):
181
+ # if nums%2==0:
182
+ # a_sums+=nums
183
+ # else:
184
+ # b_sums+=nums
185
+ # print(a_sums,b_sums)
186
+
187
+ #嵌套循环
188
+ for x in range (5 ):
189
+ for y in range (5 ):
190
+ print (x ,end = '\t ' )
191
+ print () #换行
192
+
193
+ #嵌套循环练习_九九乘法表_打印表格数据
194
+
195
+ #先打印一行的 乘法表
196
+ # for n in (1,6):
197
+ # print("{0}*{1}".format(5,n),end='\t')
198
+ # print()
199
+
200
+ #然后 再外层套大循环
201
+ # for m in range(1,10):
202
+ # for n in range(1,m+1):
203
+ # print("{0}*{1}".format(m,n),end='\t')
204
+ # print()
205
+
206
+ #
207
+ # r1 = dict(name='高小一',age=18,salary=30000,city='北京')
208
+ # r2 = dict(name='高小二',age=19,salary=20000,city='上海')
209
+ # r3 = dict(name='高小三',age=20,salary=10000,city='深圳')
210
+ # tb = [r1,r2,r3]
211
+ # for i in tb:
212
+ # if i.get('salary')>15000:
213
+ # print(i)
214
+ # print()
215
+
216
+ #break 语句 ---退出循环
217
+ # while True:
218
+ # x = input('输入一个字符(exit 表示结束):')
219
+ # if x=='exit':
220
+ # break
221
+ # print(x)
222
+ # else:
223
+ # print(x)
224
+
225
+
226
+ #continue 语句 ---跳过当前循环,继续下一次循环
227
+ # empNum = 0
228
+ # salarys = []
229
+ # salarySum = 0
230
+ # while True:
231
+ # s = input('请输入员工的薪资(按Q或q结束):')
232
+ # if s.upper()=='Q':
233
+ # print('录入完成,退出!')
234
+ # break
235
+ # if float(s)<0:
236
+ # print('录入错误,薪资不能为负数!请重新录入')
237
+ # continue
238
+ # print('录入成功!')
239
+ # empNum+=1
240
+ # salarys.append(float(s))
241
+ # salarySum+=float(s)
242
+ # print('员工人数为:{0},薪资总和为:{1},平均薪资为:{2}'.format(empNum,salarySum,salarySum/empNum))
243
+
244
+
245
+ #循环中的else语句(while,for)
246
+ # salarySum = 0
247
+ # salarys = []
248
+ # for i in range(4):
249
+ # s = input('请输入4名员工的薪资(按Q或q结束):')
250
+ # if s.upper()=='Q':
251
+ # print('录入完成,退出!')
252
+ # break
253
+ # if float(s)<0:
254
+ # print('录入错误,薪资不能为负数!请重新录入')
255
+ # continue
256
+ # print('录入成功!')
257
+ # salarySum+=float(s)
258
+ # salarys.append(float(s))
259
+ # else:
260
+ # print('所有员工的薪资录入成功!')
261
+ #
262
+ # print('录入的薪资为:',salarys)
263
+ # print('录入的薪资总和为:',salarySum)
264
+ # print('录入的薪资平均值为:',salarySum/4)
265
+
266
+ #循环代码优化技巧
267
+ import time
268
+ start = time .time ()
269
+ for i in range (1000 ):
270
+ result = []
271
+ for m in range (10000 ):
272
+ c = i * 1000
273
+ # result = result + [m*100] #不要使用+号,会生成新的列表对象
274
+ result .append (c + m * 100 ) #使用append方法,只是在原列表对象上添加元素
275
+ end = time .time ()
276
+ print ('耗时:' ,end - start )
277
+
278
+ print ('优化后的' )
279
+
280
+ start = time .time ()
281
+ for i in range (1000 ):
282
+ result = []
283
+ c = i * 1000
284
+ for m in range (10000 ):
285
+ # result = result + [m*100] #不要使用+号,会生成新的列表对象
286
+ result .append (c + m * 100 ) #使用append方法,只是在原列表对象上添加元素
287
+ end = time .time ()
288
+ print ('耗时:' ,end - start )
289
+
290
+ #zip()并行迭代多个序列
291
+ # names = ['张三','李四','王五','赵六']
292
+ # ages = [20,30,40,50]
293
+ # jobs = ['工程师','医生','律师']
294
+ #
295
+ # for name,age,job in zip(names,ages,jobs):
296
+ # print('{0}--{1}--{2}'.format(name,age,job))
297
+
298
+ #非zip实现方式
299
+ names = ('张三' ,'李四' ,'王五' ,'赵六' )
300
+ ages = (20 ,30 ,40 ,50 )
301
+ jobs = ('工程师' ,'医生' ,'律师' )
302
+
303
+ for i in range (min (len (ages ),len (ages ),len (jobs ))):
304
+ print ('{0}--{1}--{2}' .format (names [i ],ages [i ],jobs [i ]))
305
+
306
+ #推导式创建序列_列表推导式_字典推导式_集合推导式_生成器推导式
307
+ #列表推导式
308
+ ls = [x * 6 for x in range (10 ) if x % 2 == 0 ]
309
+ print (ls ,'推导式' )
310
+
311
+ lsb = []
312
+ for x in range (1 ,10 ):
313
+ if x % 2 == 0 :
314
+ lsb .append (x * 6 )
315
+ print (lsb ,'非推导式' )
316
+
317
+ #字典推导式
318
+ values = ['北京' ,'上海' ,'深圳' ,'广州' ]
319
+ cities = {id :city for id ,city in zip (range (1 ,5 ),values )}
320
+ print (cities ,'字典推导式' )
321
+
322
+ my_text = 'I love python programming I love javascript programming I love java programming'
323
+ char_count = {s :my_text .count (s ) for s in my_text }
324
+ print (char_count ,'字典推导式' )
325
+
326
+ #集合推导式
327
+ os_a = {x for x in range (10 ) if x % 2 != 0 }
328
+ print (os_a ,'集合推导式' )
329
+
330
+ #生成器推导式
331
+ g = (x for x in range (1 ,100 ) if x % 9 == 0 ) #生成器对象
332
+ print (g ,'生成器推导式' )
333
+
334
+ for x in g :
335
+ print (x ,end = ' ' )
336
+
337
+ for x in g :
338
+ print (x ,end = ' ' ) #生成器对象只能迭代一次
96
339
340
+ # 综合练习_绘制不同颜色的同心圆_绘制棋盘
341
+ import turtle
342
+ p = turtle .Pen () #创建画笔对象
343
+ # p.circle(10) #绘制半径为100的圆
344
+ # p.penup() #抬起画笔
345
+ # p.goto(0,-10) #移动到坐标(0,-10) #绘制同心圆
346
+ # p.pendown() #放下画笔
347
+ # p.circle(20)
348
+ radius = [x * 10 for x in range (1 ,11 )]
349
+ colors = ['red' ,'green' ,'yellow' ,'black' ]
350
+ p .width (4 ) #设置画笔宽度
351
+ for i ,color_j in zip (radius ,range (len (radius ))):
352
+ p .penup () #抬起画笔
353
+ p .goto (0 ,- i ) #移动到坐标(0,-i)
354
+ p .pendown () #放下画笔
355
+ # p.pencolor(colors.pop()) #设置画笔颜色
356
+ p .color (colors [color_j % len (colors )]) #设置画笔颜色
357
+ p .circle (i ) #绘制圆
97
358
359
+ turtle .done ()
0 commit comments