python-day3-条件判断与循环

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# ### 代码块 : 用冒号作为开始,用缩进来划分相同的作用域
"""作用域: 作用范围"""
 
# 基本使用
"""代码块是一个整体,在条件满足时,会一并执行,否则一并不执行"""
 
if 10 == 10:
    print(1)
    print(2)
print(3)
 
# 注意点
"""在使用代码时,要么全部使用缩进,要么全部使用4个空格,不能混合使用"""
'''
if 10 == 10:
    print(5)
    print(6)
    print(7) # IndentationError
'''
 
'''
# 扩展: (php js java ...)
if (10 == 10){
print(0)
print(1)
print(2)
}
'''
 
# ### 流程控制
"""
流程 : 代码执行的顺序
控制 : 对代码执行顺序的把控
 
三大结构:
(1) 顺序结构:
    代码从上到下依次执行
(2) 分支结构:
    四大小类
(3) 循环结构:
    1.for循环
    2.while循环
 
分支结构:
    (1) 单项分支
    (2) 双项分支
    (3) 多项分支
    (4) 巢状分支
"""
 
# 单项分支
"""
if 条件表达式:
    code1
    code2
     
当条件表达式成立的时候,执行代码块其中的内容,否则不执行
"""
xk = "帅哥"
if xk == "帅哥":
    print("哇,好帅呀")
 
 
# 双项分支 (二选一)
"""
if 条件表达式:
    code1
    code2
else:
    code3
    code4
当条件表达式成立的时候,执行if这个分支的代码块
当条件表达式不成立的时候,执行else这个分支的代码块
if分支代码块也叫作真区间
else分支代码块也叫作假区间
"""
xk = "靓女"
if xk == "靓仔":
    print("买包包给他")
else:
    print("买西装给他")
 
# ### 登录  账号/密码
"""账户: admin  密码:111 => 登录成功 , 否则提示登录失败 """
# input => 获取用户的输入内容,类型是字符串
# name = input("请输入您的姓名:")
# print(name,type(name))
"""
username = input("请输入您的用户名")
password = input("请输入您的密码")
if username == "admin" and password == "123123":
    print("登录成功")
else:
    print("登录失败")
 
"""
 
# ### 多项分支 (多选一)
"""
语法结构:
if 条件表达式1:
    code1
    code2
elif 条件表达式2:
    code3
    code4
elif 条件表达式3:
    code5
    code6
else:
    code ...
 
如果条件表达式1 成立 , 执行对应代码块 , 如果不成立,继续向下判断
看一下条件表达式2 是否成立 ,成立的话执行对应代码块, 如果不成立,继续向下判断
看一下条件表达式3 是否成立 ,成立的话执行对应代码块, 如果不成立,继续向下判断
当所有的条件都不满足的时候,直接执行else这个分支
elif 可以出现0次或者多次
else 可以出现0次或者1次
"""
youqian = False
youfang = False
youche = False
yanzhi = True
renpin = False
if youqian == True:
    print("选择嫁给他")
elif youche == True:
    print("选择嫁给他")
elif yanzhi == True:
    print("选择嫁给他")
elif renpin == True:
    print("选择嫁给他")
else:
    print("你是个好人")
 
print("<=====>")
# ### 巢状分支
"""单项分支,双项分支,多项分支的互相嵌套的写法"""
youqian = False
youfang = False
youche = False
yanzhi = False
renpin = False
 
if youqian == True:
    if youfang == True:
        if youche == True:
            if yanzhi == True:
                if renpin == True:
                    print("嫁给他,接盘侠")
                else:
                    print("你不适合我")
            else:
                print("去韩国整容再来吧")
else:
    print("好好赚钱")
 
'''
#出题 height
#女生找对象
    # 男生在1米~1.5米之间 小强 你在哪里?
    # 男生在1.5~1.7米之间 没有安全感~
    # 男生 1.7~ 1.8米之间 帅哥 留个电话
    # 男生 1.8~2米之间 帅哥 你建议多一个女朋友吗
 
height = float(input("请输入您的身高:"))
print(height,type(height))
 
# python特有
"""
if 1 <= height < 1.5:
    print("小强 你在哪里")
elif 1.5 <= height < 1.7:
    print("没有安全感")
elif 1.7 <= height < 1.8:
    print("帅哥 留个电话")
elif 1.8 <= height < 2:
    print("帅哥 你建议多一个女朋友吗")
else:
    print("没有合适的选项")
"""
 
#通用写法
if height >= 1 and height < 1.5:
    print("小强 你在哪里")
elif height >= 1.5 and height < 1.7:
    print("没有安全感")
elif height >= 1.7 and height < 1.8:
    print("帅哥 留个电话")
elif height >= 1.8 and height < 2:
    print("帅哥 你建议多一个女朋友吗")
else:
    print("没有合适的选项")
 
'''
print("<=====>")
 
# ### 循环结构
"""特点:减少冗余的代码,提升开发的效率,便于后期的维护"""
"""
(1) 初始化一个值
(2) 写上循环条件
(3) 写上自增自减的值
while 条件表达式:
   code
"""
# 打印1 ~ 100的数字
 
# (1) 初始化一个值
i = 1
# (2) 写上循环的条件
while i <= 100:
    # (4) 写上循环的逻辑
    print(i)
    # (3) 写上自增自减的值
    i+=1  # i = i+1
 
"""
# 代码解析:
# 第一次循环
i = 1 , i <= 100 =>True,执行循环  print(i) => 1
i += 1 => 1 + 1 => 2
 
# 第二次循环
i = 2 , i <= 100 =>True,执行循环  print(i) => 2
i += 1 => 2 + 1 => 3
 
# 第二次循环
i = 3 , i <= 100 =>True,执行循环  print(i) => 3
i += 1 => 3 + 1 => 4
 
依次类推...
当i = 101 时  i <= 100 =>False , 终止循环 , 程序结束.
 
"""
 
#  1 ~ 100 计算累加和
# 方法一
i = 1
total = 0
while i <= 100:
    total += # total = total + i
    i+=1
print(total,"方法1")   # 5050
 
"""
第一次循环
i = 1 i <= 100 成立 , 执行循环  total += i => total = total + i => 0 + 1
i += 1 => 2
 
第二次循环
i = 2 i <= 100 成立 , 执行循环  total += i => total = total + i => 0 + 1 + 2
i += 1 => 3
 
第三次循环
i = 3 i <= 100 成立 , 执行循环  total += i => total = total + i => 0 + 1 + 2 + 3
i += 1 => 4
 
什么时候结束?
i = 101 i <= 100 不成立 , 不执行循环,循环终止
total => 0 + 1 + 2 + 3 + ... + 100 = 5050
"""
 
 
# 死循环
"""
while True:
    print(123)
"""
 
# 死循环的写法完成1~100的累加和
# 方法二
 
i = 1
total = 0
sign = True
while sign:
    total += i   # 1 + 2 + ... +100
 
    i+=1
 
    if i == 101:
        # 手动设置False ,跳出循环
        sign = False
 
print(total,"方法2")
 
print("<=====>")
 
# ### 单向循环练习
# (1)打印一行十个小星星
"""
help(print)
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
print("*",end="")
"""
# print默认是打印一行,结尾加换行。end=''意思是末尾不换行。
i = 1
while i <= 10:
    print("*",end="")
    i+=1
 
 
print()
 
 
# (2)用变量拼接字符串的形式,打印一行十个小星星
# + 号可以用来拼接字符串
'''strvar = "可爱的" + "小宝贝"
strvar = "可爱的"
strvar += "小宝宝" # strvar = strvar + "小宝宝"  => 可爱的小宝宝
print(strvar)
'''
i = 0
strvar = ""
 
while i < 10:
    strvar += "*"
    i+=1
print(strvar)
 
"""
代码解析
strvar = "*"
strvar = "*" + "*" ="**"
strvar = "**" + "*" ="***"
strvar = "***" + "*" ="****"
...
strvar = "*********" + "*" = "**********"
"""
 
# (3)打印一行十个小星星 奇数个打印★ 偶数个打印☆
# ★☆★☆★☆★☆★☆
i = 0
while i < 10:
    if i % 2 == 0:
        print("★",end="")
    else:
        print("☆", end="")
    i+=1
 
"""
### 规律: 任意数和n取余,余数范围是多少? 0~ (n-1)
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
 
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
"""
 
print()
 
# (4)用 一个循环 打印十行十列小星星
i = 0
while i < 100:
    # 打印星星
    print("★",end="")
 
    # 什么时候换行 9,19,29... 99 是换行点
    if i % 10 == 9:
        # 添加换行
        print()
    i+=1
 
"""
0123456789 
**********
10111213141516171819
* * * * * * * * * *
20 ~ 29
* * * * * * * * * *
30 ~ 39
* * * * * * * * * *
...
90 ~ 99
* * * * * * * * * *
9,19,29... 99 是换行点
i % 10 == 9
"""
print()
<br>
# (5)一个循环 打印十行十列隔列变色小星星(一个循环)
"""
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
★☆★☆★☆★☆★☆
"""
 
i = 0
while i < 100:
    # 打印黑白相间的小星星
    if i % 2 == 0:
        print("★",end="")
    else:
        print("☆", end="")
    # 打印换行
    if i % 10 == 9:
        # 添加换行
        print()
    i+=1
 
# (6)一个循环 打印十行十列隔行变色小星星(一个循环)
"""
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
★★★★★★★★★★
☆☆☆☆☆☆☆☆☆☆
"""
 
"""
###规律:任意数和n进行地板除,会出现n个相同的数字!
10个相同的0
0 // 10 => 0
1 // 10 => 0
2 // 10 => 0
3 // 10 => 0
4 // 10 => 0
...
9 // 10 => 0
 
10个相同的1
10 // 10 => 1
11 // 10 => 1
12 // 10 => 1
...
19 // 10 => 1
 
0 // 2 => 0
1 // 2 => 0
2 // 2 => 1
3 // 2 => 1
 
0 ~ 9   // 10  => 10个相同的0
10 ~ 19 // 10  => 10个相同的1
20 ~ 29 // 10  => 10个相同的2
...
90 ~ 99 // 10  => 10个相同的9
 
相同数字的范围是0 ~ 9
"""
 
i = 0
while i < 100:
    # 打印黑白相间的小星星
    if i // 10 % 2 == 0:
        print("★",end="")
    else:
        print("☆", end="")
    # 打印换行
    if i % 10 == 9:
        # 添加换行
        print()
    i+=1
 
 
# ### 双向循环练习
# (1)打印十行十列小星星 (用两个循环)
 
j = 0
while j < 10:
    # 打印一行十个星星,控制列
    i = 0
    while i < 10:
        print("*",end="")
        i+=1
    # 控制换行
    print()
 
    j+=1
 
# (2)打印十行十列隔列换色小星星
j = 0
while j < 10:
    i= 0
    while i < 10:
        if i % 2 == 0:
            print("★", end="")
        else:
            print("☆", end="")
        i+=1
    print()
 
    j+=1
 
# (3)打印十行十列隔行换色小星
j = 0
while j < 10:
 
    # 控制打印一行十个黑白相间的星星
    i = 0
    while i < 10:
        if j % 2 == 0:
            print("★", end="")
        else:
            print("☆", end="")
 
        i += 1
    # 打印换行
    print()
 
    j += 1
 
print()
 
# (4)99乘法表
# 方向一
i = 1
while i <= 9:
    j = 1
    while j <= i:
        print("%d*%d=%2d " %(i,j,i*j),end="")
        j+=1
    print()
 
    i+=1
 
print()
# 方向二
i = 9
while i >= 1:
    # 打印表达式
    j = 1
    while j <= i:
        print("%d*%d=%2d " %(i,j,i*j),end="")
        j+=1
    # 打印换行
    print()
    i-=1
 
# 方向三
i = 1
while i <= 9:
    k = 9 - i
    while k > 0:
        print("       ",end="")
        k-=1
 
    j = 1
    while j <= i:
        print("%d*%d=%2d " %(i,j,i*j),end="" )
        j+=1
 
    print()
 
    i+=1
print()
 
# 方向四
i = 9
while i >= 1:
 
    # 打印空格
    k = 9 - i
    while k > 0:
        print("       ", end="")
        k -= 1
 
    # 打印表达式
    j = 1
    while j <= i:
        print("%d*%d=%2d " % (i, j, i * j), end="")
        j += 1
 
    # 打印换行
    print()
 
    i -= 1
 
# (5)100 ~ 999 找吉利数字 111 222 123 321 888 ...
"""
678
 
// 可以取到一个数的高位
%  可以取到一个数的低位
 
# 个位
678 % 10 => 8
 
# 十位
678 // 10 % 10 = 7
 
# 百位
678 // 100 => 6
"""
 
i = 100
while i <= 999:
    # 个位
    gewei = i % 10
    # 十位
    shiwei = i // 10 % 10
    # 百位
    baiwei = i // 100
    # 111 222 333
    if shiwei == gewei and shiwei == baiwei:
        print(i)
    # 123 456
    elif shiwei == gewei - 1 and shiwei == baiwei + 1:
        print(i)
    # 321 654
    elif shiwei == gewei + 1 and shiwei == baiwei - 1:
        print(i)
 
    i+=1
 
# 方法二
print("<====>")
i = 100
while i < 1000:
    # 先变成字符串
    strvar = str(i)
    # 用字符串取下标的形式得到个十百,强转成整型
    gewei = int(strvar[-1])
    shiwei = int(strvar[1])
    baiwei = int(strvar[0])
    # print(baiwei,shiwei,gewei)
 
    # 111 222 333
    if shiwei == gewei and shiwei == baiwei:
        print(i)
 
    # 123  456
    elif shiwei == gewei - 1 and shiwei == baiwei + 1:
        print(i)
    # 321
    elif shiwei == gewei + 1 and shiwei == baiwei - 1:
        print(i)
 
    i += 1
 
# ### for循环
# 遍历 循环 迭代 把里面的元素一个一个拿出来
lst = [1,2,3,4]
# lst[0]
# lst[1]
# lst[2]
# lst[3]
i = 0
while i < len(lst):
    print(lst[i])
    i+=1
 
# while 循环在遍历数据时,有局限性,for循环应用而生
"""
setvar = {"a","b","c",1,2,3}
while i < len(setvar):
    print(setvar[i]) error
    i+=1
"""
 
# for 循环语法
"""
for 变量 in 可迭代性数据:
    code ....
可迭代性对象(数据):
1.容器类型(str,list,tuple,set,dict)
2.range对象
3.迭代器
"""
 
# 遍历字符串
container = "你的酒窝不醉人,但是我却神魂颠倒"
for i in container:
    print(i)
 
# 遍历列表
container = ["小李子","小邓子","小卓子","小钉子"]
for i in container:
    print(i)
 
# 遍历元组
container = ("小李子","小邓子","小卓子","小钉子")
for i in container:
    print(i)
 
# 遍历集合
container = {"小李子","小邓子","小卓子","小钉子"}
for i in container:
    print(i)
 
# 遍历字典 (遍历字典的时候,默认只遍历键,没有值)
container = {"lz":"李子","dz":"邓子","zz":"卓子"}
for i in container:
    print(i)
 
# 可以遍历不等长的二级容器
container = [("小李子","小邓子"),{"小卓子"},["小钉子"]]
for i in container:
    for j in i:
        print(j)
 
# 变量的解包
a,b = 1,2
a,b = [3,4]
a,b = {7,8}
# 把字典中的键依次解包,赋值给对应的变量
a,b,c = {"a":10,"b":11,"c":12}
print(a,b,c)
 
# 遍历等长的二级容器
container = [("王思聪","王健林","王美丽"), ("马云",'马化腾',"马丽"),("王宝强","马蓉","宋小宝")]
for a,b,c in container:
    print(a,b,c)
 
 
# ### range 用法
"""
# range("开始值","结束值","步长")
结束值最大取不到,取到结束值之前的那个数据
for 一般配合 range使用
"""
 
# 只有一个值的情况
for i in range(10):
    print(i)
 
# 两个值的情况
for i in range(1,10):
    print(i)  # 1 ~ 9
 
# 三个值的情况
for i in range (1,10,3): #(正序)
    print(i) # 1 4 7
 
print("<====>")
 
for i in  range(10,0,-2):# 倒序
    print(i)  #10  8  6  4  2
 
 
 
# ### pass  break continue
 
# (1) pass 过
"""
if 10 == 10:
    pass
 
while True:
    pass
"""
<br>
# break 终止当前循环(只能用在循环中)
# 1~10 遇到5 终止
i = 1
while i <= 10:
    if i == 5:
        break
    print(i)
    i+=1
 
for i in range(1,10):
    if i == 5:
        break
    print(i)
 
i = 1
while i <= 3:
    j = 1
    while j <=3 :
        if j == 2:
            break
        print(i,j)
        j+=1
    i+=1
 
# (3)continue 跳过当前循环(只能用在循环当中)
# 1~10 遇到5跳过
# while 循环
i = 1
while i <= 10:
    if i == 5:
        # 需要手动加一, 否则死循环
        i += 1
        continue
    print(i)
    i+=1
 
# for循环
for i in range(1,11):
    if i == 5:
        continue
    print(i)

出处:https://www.cnblogs.com/liucx/

  

posted @   Mr'liu  阅读(182)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示