Python 基础-3
使用while打印1 2 3 4 5 6 8 9 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | count = 0 #while count < 10: while count < 10: count += 1 if count == 7: print( '' ) else : print(count) 测试 1 2 3 4 5 6 8 9 10 |
使用while打印1 2 3 4 5 6 8 9 10;当count=7时不打印空格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | while count < 10: count += 1 if count == 7: continue 返回重新开始 else : print(count) 执行 1 2 3 4 5 6 8 9 10 |
使用while打印1 2 3 4 5 6 8 9 10;当count=7时不打印空格,用pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | count = 0 #while count < 10: while count < 10: count += 1 if count == 7: pass 跳过 else : print(count) 执行测试 1 2 3 4 5 6 8 9 10 |
使用while打印100以内的奇数
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 | count = 1 while count < 101: print(count) count += 2 测试 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 |
打印 1-2+3-4+5....99打印结果
1 2 3 4 5 6 7 8 9 10 11 12 | [root@es-3 ~] # cat test.py sun = 0 count = 1 while count < 100: if count % 2 == 0: sun = sun - count else : sun = sun + count count += 1 print(sun) [root@es-3 ~] # python3 test.py 50 |
格式化输出
1 2 3 4 5 6 7 8 9 10 | name = input( '请输入名字' ) age = input( '请输入年龄' ) heigeht = input( '请入身高' ) msg = "我叫%s,今年%s,身高%s" %(name,age,heigeht) 占位符按顺序替换 print(msg) [root@es-3 ~] # python3 test.py 请输入名字 cd 请输入年龄98 请入身高90 我叫 cd ,今年98,身高90 |
格式化输出二 只有%s 和%d
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | name = input( '请输入名字' ) age = input( '请输入年龄' ) job = input( '请输入工作' ) hobbie = input( '你的爱好' ) msg = '' '------------cx------------- Name : %s Age : %d 数字类型 job : %s Hobbie : %s ---------------end---------------- '' ' %(name,int(age),job,hobbie) print(msg) [root@es-3 ~] # python3 test.py 请输入名字 cd 请输入年龄13 请输入工作erty 你的爱好yujnk ------------cx------------- Name : cd Age : 13 job : erty Hobbie : yujnk ---------------end---------------- |
格式化输出%
1 2 3 4 5 6 7 8 9 10 11 | name = input( '请输入姓名:' ) age = input( '请输入年龄:' ) height = input( '请输入身高:' ) msg = "我叫%s,今年%d,身高%s,学习进度为3%%" %(name,int(age),height) print(msg) 测试 [root@es-3 ~] # python3 test.py 请输入姓名:cx 请输入年龄:19 请输入身高:190 我叫cx,今年19,身高190,学习进度为3% |
pass 与break在while循环中的区别;当while循环被break打断,就不会执行else的语句了
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 | count = 0 while count < 10: count += 1 if count == 3: break 使用 break 直接中断后续操作 print (count) else : print (循环完成) 测试 C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe G: /python/v/rt .py 1 2 count = 0 while count < 10: count += 1 if count == 3:pass 结束本次判断 print (count) else : print ( "循环完成" ) C:\Users\zrd\AppData\Local\Programs\Python\Python37\python.exe G: /python/v/test-2 .py 1 2 3 4 5 6 7 8 9 10 循环完成 |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个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的强缓存和协商缓存
· 一文读懂知识蒸馏