002-pythn基础-循环、编码
1. 循环
while 条件:
代码块(循环体)
else:
当上面的条件为假. 才会执行
执行顺序:
判断条件是否为真. 如果真. 执行循环体. 然后再次判断条件....直到循环条件为假. 程序退出
2. break和continue
break: 停止当前本层循环
continue: 停止当前本次循环. 继续执行下一次循环
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 | # 死循环 count = 1 <br> while count < = 5 : print ( "喷死你.." ) count = count + 1 <br> # 数数 1-100 count = 1 while count < 101 : print (count) count = count + 2 # 让用户一直去输入内容, 并打印. 直到用户输入q的时候退出程序 while True : content = input ( "请输入一句话,(输入q退出程序):" ) if content = = 'q' : break # 打断. 终止当前本层循环 print (content) flag = True while flag: content = input ( "请输入一句话,(输入q退出程序):" ) if content = = 'q' : flag = False # 打断. 终止当前本层循环 print (content) else : print ( "123" ) while True : content = input ( "请输入一句话,(输入q退出程序):" ) if content = = 'q' : continue # 停止当前本次循环. 继续执行下一次循环 print (content) # break和continue的区别: break是彻底的停止掉当前层循环. continue停止当前本次循环,继续执行下一次循环 count = 1 while count < = 10 : if count = = 4 : count = count + 1 continue # 用来排除一些内容 print (count) count = count + 1 # 必须要写 <br>count = 1 while count < = 20 : if count = = 10 : break # 不会触发else的执行, while...else...是一个整体. break的时候彻底的停止这个整体 print (count) count = count + 1 else : # 当上面的条件不成立的时候执行这个else中的代码 print ( "数完了" ) |
3. 格式化输出
%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 24 25 | name = "alex" age = 38 hobby = "浪" location = "湖边" print (age + "岁的" + name + "在" + location + "喜欢" + hobby) # # 格式化 # %s 占位. 占位的是字符串, 全能的. 什么都能接 # %d 占位. 占位的是数字 print ( "%s岁的%s在%s喜欢%s" % (age, name, location, hobby)) name = input ( "请输入名字:" ) age = input ( "请输入年龄:" ) job = input ( "请输入你的工作:" ) hobby = input ( "请输入你的爱好:" ) <br>s = '''------------ info of %s ----------- Name : %s Age : %s job : %s Hobbie: %s ------------- end -----------------''' % (name, name, age, job, hobby) # print(s) name = 'sylar' # 如果你的字符串中出现了%s这样的格式化的内容. 后面的%都认为是格式化.如果想要使用%. 需要转义 %% print ( "我叫%s, 我已经学习了2%%的python了" % (name)) print ( "我叫周润发. 我已经活了50%了" ) |
4. 运算符
and: 并且, 两端同时为真. 结果才能是真
or: 或者, 有一个是真. 结果就是真
not: 非真既假, 非假既真
顺序: () => not => and => or
x or y:
如果x是零, 输出y
如果x是非零, 输出x
True: 非零
False: 零
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 | print ( 1 + 1 ) print ( 1 - 1 ) print ( 1 * 2 ) print ( 1 / 2 ) print ( 10 % 3 ) # 计算余数 10/3=3......1 n = 49 if n % 2 = = 1 : print ( "奇数" ) else : print ( "偶数" ) print ( 10 / / 3 ) # 整除. 地板除. 计算商 print ( 5 * * 3 ) # 5的2次幂 m**n m的n次幂 a = 10 b = 20 print (a = = b) # 等于 print (a ! = b) # 不等于 a = 1 b = 2 a + = b # a = 3 a+=b => a = a + b # a *= b => a = a * b print (a) print (b) |
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 | # 逻辑运算符 # 1. and 并且的含义. 左右两端同时为真. 结果才能是真. # 2. or 或者的含义. 左右两端有一个是真. 结果就是真. 所有的条件都是假. 结果才是假 # 3. not 取反 非真既假, 非假既真 # 顺序: () => not => and => or 相同的运算. 从左往右算 print ( 1 > 2 and 4 < 6 or 5 > 7 ) print ( 1 > 2 or 3 > 4 ) print ( 5 > 3 or 4 < 6 ) print ( 5 > 3 or 4 > 6 ) print ( 3 > 4 or 4 < 3 and 1 = = 1 ) # False print ( 1 < 2 and 3 < 4 or 1 > 2 ) # True print ( 2 > 1 and 3 < 4 or 4 > 5 and 2 < 1 ) # True print ( 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 ) # False print ( 1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ) # False print ( not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ) # False # x or y 如果x是0 返回y, 如果x是非零, 返回x print ( 1 or 2 ) # 1 print ( 1 or 0 ) # 1 print ( 0 or 1 ) # 1 print ( 0 or 2 ) # 2 print ( 0 or 1 or 2 or 3 ) print ( 3 or 0 or 1 or 0 or 2 ) # and和or相反. 不要去总结and. 记住or print ( 1 and 2 ) # 2 print ( 0 and 2 ) # 0 print ( 1 and 0 ) # 0 print ( 0 and 1 ) # 0 print ( 1 and 2 or 3 ) print ( 1 or 2 and 3 ) # False: 0, True: 1(非零) print ( 1 and 2 > 3 ) print ( 2 > 3 and 1 ) print ( 1 > 2 or 0 and 3 < 6 or 5 ) # 先算and 后算or print ( 2 * * 32 ) |
5. 编码
1. ascii. 最早的编码. 至今还在使用. 8位一个字节(字符)
2. GBK. 国标码. 16位2个字节.
3. unicode. 万国码. 32位4个字节
4. UTF-8. 可变长度的unicode.
英文: 8位. 1个字节
欧洲文字:16位. 2个字节
汉字. 24位. 3个字节
8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
2. bool. 类型转换的问题=>
3. str(重点).
作者: do康解U
出处: https://www.cnblogs.com/David-domain/>
关于作者:专注数据库、Python 开发、ROS、深度学习,请多多赐教!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(iamkuboy@163.com)咨询.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY