花式打印0~100中3的倍数

75 篇文章 0 订阅

列表解析3的倍数负步长切片倒序,iter、zip函数配合实现分行格式打印。


  (本笔记适合熟悉python列表解析式的 coder 翻阅)


【学习的细节是欢悦的历程】


  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
            —— 华罗庚


等风来,不如追风去……


列表解析3的倍数负步长切片倒序
花式打印0~100中3的倍数
(iter、zip函数配合实现分行格式打印)


本文质量分:

96
本文地址: https://blog.csdn.net/m0_57158496/article/details/133346467

CSDN质量分查询入口:http://www.csdn.net/qc


目 录



◆ 花式打印0~100中3的倍数


1、题目描述


  • 题目描述截屏图片
    在这里插入图片描述

题目来源于 CSDN 问答社区提问“倒序打印0~100中3的倍数



回页目录


2、算法解析


  循环遍历0~100,用判定整除于3的条件表达式判定遴选3的倍数,按每行五个整数逆序打印输出。
  此题目相对简单,更谈不上“算法”但代码实现的途径却不只一个,还算有些趣味,鼓捣鼓捣,也算活络活络手指。记此笔记,以“编程基技”博您展颜一乐。😄😀😁😊😜🤗


2.1 条件表达式筛选


  • 代码运行效果截屏图片
    在这里插入图片描述

1、最“正”的算法表达:遍历0~100,用3的倍数条件表达式筛选

列表解析式代码


print('\n0~100能被3整除的数:\n', *[i for i in range(101) if i>0 and not i%3]) # 遍历筛选。


2、还可以从3的最小倍数3开始,以3为步长遍历0~100。

负步长遍历代码


print('\n0~100能被3整除的数:\n', *[i for i in range(3, 101, 3)]) # 直接步长遍历。



回页目录


2.2 倒序打印输出


  • 代码运行效果截屏图片
    在这里插入图片描述

1、直接负步长倒序遍历代码


print('\n0~100能被3整除的数:\n', *[i for i in range(101, 0, -1) if i>0 and not i%3])
print('\n0~100中3倍数有:\n', *[i for i in range(99, 0, -3)]) 


2、负步长拷贝[::-1]实现倒序代码


print('\n0~100能被3整除的数:\n', *[i for i in range(101) if i>0 and not i%3][::-1])
print('\n0~100中3倍数有:\n', *[i for i in range(3, 101, 3)][::-1])



回页目录


2.3 分行格式打印


1、索引下标分行打印

  • 代码运行效果截屏图片
    在这里插入图片描述

代码


print(f"\n{'':>4}0~100能被3整除的数:\n")
print(f"{'':>8}", end='') # 逢5打印换行。
nums_3 = [i for i in range(101) if i>0 and not i%3][::-1]
for i in range(len(nums_3)):
    print(f"{nums_3[i]} ", end='') # 打印不换行。
    
    if not (i+1)%5:
        print(f"\n{'':>8}", end='') # 逢5打印换行。
    
print() 


2、也可以用iter、zip函数配合实现分行格式打印。

  • 代码运行效果截屏图片
    在这里插入图片描述

代码


nums_3 = [i for i in range(3, 101, 3)][::-1]
nums_3 = iter(nums_3 + ['']*(5-len(nums_3)%5)) # 对应列表长度成五的倍数。
print(f"\n{'':>10}0~100中3倍数有:\n")
for i in zip(nums_3, nums_3, nums_3, nums_3, nums_3):
    print(' '*15, *i)



回页目录


3、完整源码

(源码较长,点此跳过源码)

#!/sur/bin/nve python
# coding: utf-8


print('\n0~100能被3整除的数:\n', *[i for i in range(101) if i>0 and not i%3]) # 遍历筛选。

print('\n0~100能被3整除的数:\n', *[i for i in range(3, 101, 3)]) # 直接步长遍历。

## 直接倒序遍历 ##
print('\n0~100能被3整除的数:\n', *[i for i in range(101, 0, -1) if i>0 and not i%3])
print('\n0~100中3倍数有:\n', *[i for i in range(99, 0, -3)]) 

## 负步长拷贝[::-1]逆序 ##
print('\n0~100能被3整除的数:\n', *[i for i in range(101) if i>0 and not i%3][::-1])
print('\n0~100中3倍数有:\n', *[i for i in range(3, 101, 3)][::-1])


## 索引下标分行格式打印 ##
print(f"\n{'':>4}0~100能被3整除的数:\n")
print(f"{'':>8}", end='') # 逢5打印换行。
nums_3 = [i for i in range(101) if i>0 and not i%3][::-1]
for i in range(len(nums_3)):
    print(f"{nums_3[i]} ", end='') # 打印不换行。
    
    if not (i+1)%5:
        print(f"\n{'':>8}", end='') # 逢5打印换行。
    
print() 

## 函数配合分行格式打印 ##
nums_3 = [i for i in range(3, 101, 3)][::-1]
nums_3 = iter(nums_3 + ['']*(5-len(nums_3)%5)) # 对应列表长度成五的倍数。
print(f"\n{'':>10}0~100中3倍数有:\n")
for i in zip(nums_3, nums_3, nums_3, nums_3, nums_3):
    print(' '*15, *i)



回页目录


4、我的相关博文笔记






回页首


上一篇:  彩色文本进度条(动态加色打印,显示进行到的百分比,实时更新总共用时)
下一篇: 

我的HOT博:

  本次共计收集 238 篇博文笔记信息,总阅读量 40.02w,平均阅读量 1681。已生成 27 篇阅读量不小于 3000 的博文笔记索引链接。数据采集于 2023-09-26 00:13:08 完成,用时 4 分 53.58 秒。


  1. ChatGPT国内镜像站初体验:聊天、Python代码生成等
    ( 58890 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/129035387
    点赞:125   踩 :0  收藏:796  打赏:0  评论:71
    本篇博文笔记于 2023-02-14 23:46:33 首发,最晚于 2023-07-03 05:50:55 修改。
  2. 让QQ群昵称色变的神奇代码
    ( 57966 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500
    点赞:24   踩 :0  收藏:83  打赏:0  评论:17
    本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。
  3. pandas 数据类型之 DataFrame
    ( 9123 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814
    点赞:6   踩 :0  收藏:31  打赏:0  评论:0
    本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。
  4. 个人信息提取(字符串)
    ( 7152 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618
    点赞:1   踩 :0  收藏:13  打赏:0  评论:0
    本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。
  5. 罗马数字转换器|罗马数字生成器
    ( 6993 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。
  6. Python字符串居中显示
    ( 6875 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023
    点赞:1   踩 :0  收藏:7  打赏:0  评论:1
    本篇博文笔记
  7. Python列表(list)反序(降序)的7种实现方式
    ( 6734 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700
    点赞:5   踩 :0  收藏:20  打赏:0  评论:8
    本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。
  8. 斐波那契数列的递归实现和for实现
    ( 5503 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295
    点赞:4   踩 :0  收藏:2  打赏:0  评论:8
    本篇博文笔记
  9. 练习:字符串统计(坑:f‘string‘报错)
    ( 5076 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096
    点赞:0   踩 :0  收藏:1  打赏:0  评论:0
    本篇博文笔记于 2021-12-04 22:54:29 发布。
  10. python清屏
    ( 5040 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101
    点赞:0   踩 :0  收藏:8  打赏:0  评论:0
    本篇博文笔记
  11. 回车符、换行符和回车换行符
    ( 5034 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488
    点赞:1   踩 :0  收藏:2  打赏:0  评论:0
    本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。
  12. 练习:尼姆游戏(聪明版/傻瓜式•人机对战)
    ( 4914 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399
    点赞:14   踩 :0  收藏:42  打赏:0  评论:0
    本篇博文笔记
  13. 密码强度检测器
    ( 4288 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694
    点赞:1   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。
  14. 练习:生成100个随机正整数
    ( 4226 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220
    点赞:1   踩 :0  收藏:6  打赏:0  评论:0
    本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。
  15. 罗马数字转换器(用罗马数字构造元素的值取模实现)
    ( 4125 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。
  16. 我的 Python.color() (Python 色彩打印控制)
    ( 4085 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259
    点赞:2   踩 :0  收藏:7  打赏:0  评论:0
    本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。
  17. 练习:班里有人和我同生日难吗?(概率probability、蒙特卡洛随机模拟法)
    ( 3863 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124424935
    点赞:1   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2022-04-26 12:46:25 首发,最晚于 2022-04-27 21:22:07 修改。
  18. 练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
    ( 3660 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/125415626
    点赞:3   踩 :0  收藏:6  打赏:0  评论:3
    本篇博文笔记于 2022-06-22 19:54:20 首发,最晚于 2022-06-23 22:41:33 修改。
  19. random.sample()将在python 3.9x后续版本中被弃用
    ( 3610 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/120657230
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记
  20. 聊天消息敏感词屏蔽系统(字符串替换 str.replace(str1, *) )
    ( 3438 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/124539589
    点赞:4   踩 :0  收藏:2  打赏:0  评论:3
    本篇博文笔记于 2022-05-02 13:02:39 首发,最晚于 2022-05-21 06:10:42 修改。
  21. Linux 脚本文件第一行的特殊注释符(井号和感叹号组合)的含义
    ( 3415 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123087606
    点赞:0   踩 :0  收藏:4  打赏:0  评论:3
    本篇博文笔记于 2022-02-23 13:08:07 首发,最晚于 2022-04-04 23:52:38 修改。
  22. 练习:小炼二维数组
    ( 3271 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/125175592
    点赞:9   踩 :0  收藏:5  打赏:0  评论:9
    本篇博文笔记于 2022-06-07 23:54:43 首发,最晚于 2022-06-08 00:31:49 修改。
  23. 练习:求列表(整数列表)平衡点
    ( 3219 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121737612
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记
  24. 练习:银行复利计算(用 for 循环解一道初中小题)
    ( 3142 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/123854548
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记于 2022-03-30 20:06:37 首发,最晚于 2022-04-06 18:15:16 修改。
  25. 练习:柱状图中最大矩形
    ( 3107 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122032365
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记
  26. 练习:电话拨号键盘的字母组合(一个缩进给我惹了麻烦)
    ( 3031 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/121887995
    点赞:0   踩 :0  收藏:0  打赏:0  评论:0
    本篇博文笔记
  27. Python 续行符(\\)“拯救”你的超长语句
    ( 3009 阅读)
    博文地址:https://blog.csdn.net/m0_57158496/article/details/122878447
    点赞:4   踩 :0  收藏:4  打赏:0  评论:0
    本篇博文笔记于 2022-02-11 13:24:16 首发,最晚于 2022-02-11 23:41:20 修改。
推荐条件 阅读量突破三千
(更多热博,请点击蓝色文字跳转翻阅)

回页首


老齐漫画头像

精品文章:

来源:老齐教室


回页首

Python 入门指南【Python 3.6.3】


好文力荐:


CSDN实用技巧博文:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻精灵_cq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值