python 基础
if 判断
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 | [root@es-3 ~] # cat test.py #-*-encoding:utf-8 -*- '' ' if 1 < 2 : print( "hajjj" ) if False : print(3456) print(787) if 1 < 3 : print(123456) else : print(56789) num = input( '请输入您猜的数字' ) if num == '1' : print( "看《活着》" ) elif num == '2' : print( "看《摆渡人》" ) elif num == '3' : print( "看《岛上书店》" ) else : print( "学python" ) score = int(input( "输入分数: " )) if score > 100: print( "厉害,牛" ) elif score >= 90: print( "A" ) elif score >= 70: print( "B" ) elif score >= 60: print( "C" ) else : print( "笨了" ) '' ' name = input( '请输入名字:' ) age = input( '请输入年龄' ) if name == '小二' : if age == '18' : print( '666' ) else :print(333) 测试 [root@es-3 ~] # python3 test.py 请输入名字:小san 请输入年龄18 [root@es-3 ~] # python3 test.py 请输入名字:小二 请输入年龄18 666 [root@es-3 ~] # cat test.py #-*-encoding:utf-8 -*- '' ' if 1 < 2 : print( "hajjj" ) if False : print(3456) print(787) if 1 < 3 : print(123456) else : print(56789) num = input( '请输入您猜的数字' ) if num == '1' : print( "看《活着》" ) elif num == '2' : print( "看《摆渡人》" ) elif num == '3' : print( "看《岛上书店》" ) else : print( "学python" ) score = int(input( "输入分数: " )) if score > 100: print( "厉害,牛" ) elif score >= 90: print( "A" ) elif score >= 70: print( "B" ) elif score >= 60: print( "C" ) else : print( "笨了" ) '' ' name = input( '请输入名字:' ) age = input( '请输入年龄' ) if name == '小二' : name等于小二是就进入下一个判断;否则直接退出 if age == '18' : 判断age是否等于18,如果等于18就打印666 print( '666' ) else :print(333) 否则打印333 测试 [root@es-3 ~] # python3 test.py 请输入名字:小san 请输入年龄18 [root@es-3 ~] # python3 test.py 请输入名字:小二 请输入年龄18 666 [root@es-3 ~] # python3 test.py 请输入名字:小二 请输入年龄12 333 |
循环
无限死循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [root@es-3 ~] # vim test.py print( '9090' ) while True: print( 'chenxi' ) print( '流浪' ) print( '痒' ) print( '1010' ) [root@es-3 ~] # python3 test.py 9090 chenxi 流浪 痒 chenxi 流浪 痒 chenxi 流浪 痒 chenxi 流浪 ^Z [1]+ 已停止 python3 test .py |
循环语句终止,打印1到10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [root@es-3 ~] # cat test.py count = 1 flag = True while flag: print(count) count = count + 1 if count > 10 : flag = False [root@es-3 ~] # python3 test.py 1 2 3 4 5 6 7 8 9 10 |
循环语句终止,另一种写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@es-3 ~] # vim test.py count = 1 while count <= 10: print(count) count = count + 1 [root@es-3 ~] # python3 test.py 1 2 3 4 5 6 7 8 9 10 |
计算从1加到100
1 2 3 4 5 6 7 8 9 | [root@es-3 ~] # vim test.py count = 1 sum = 0 while count <= 100: sum = sum + count count = count + 1 print( sum ) [root@es-3 ~] # python3 test.py 5050 |
循环语句终止之二break;跳本次循环
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@es-3 ~] # cat test.py print( '11' ) while True: print( '222' ) print( '333' ) break print( '444' ) print( 'abc' ) [root@es-3 ~] # python3 test.py 跳出循环没有444 11 222 333 abc |
- continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [root@es-3 ~] # cat test.py count = 0 while count <= 100: count += 1 if count > 5 and count < 95: continue 本次循环结束 print( "loop" ,count) print( "----out----" ) [root@es-3 ~] # python3 test.py loop 1 loop 2 loop 3 loop 4 loop 5 loop 95 loop 96 loop 97 loop 98 loop 99 loop 100 loop 101 ----out---- |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏