Python中的简单图案打印程序

Pattern 1:

模式1:

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *

Code:

码:

for row in range (0,5):
for column in range (0, row+1):
print ("*", end="")
# ending row
print('\r')

Pattern 2:

模式2:

Now if we want to print numbers or alphabets in this pattern then we need to replace the * with the desired number you want to replace. Like if we want pattern like,

现在,如果要在此模式下打印数字或字母,则需要将*替换为要替换的所需数字。 就像我们想要图案一样

1
1  1
1  1  1
1  1  1  1
1  1  1  1  1

Code:

码:

#row operation
for row in range(0,5):
# column operation
for column in range(0,row+1):
print("1 ",end="")
# ending line
print('\r')

Pattern 3:

模式3:

If want increasing numbers in this pattern like,

如果要以这种模式增加数字,

1
1  2  
1  2  3  
1  2  3  4
1  2  3  4  5

Here we need to declare a starting number from which the patter will start. In the above case the number is starting from 1. So, here we have to create a variable and assigns its value to 1 then we need to print only the value of variable.

在这里,我们需要声明一个起始编号,从该起始编号开始。 在上述情况下,数字从1开始。因此,这里我们必须创建一个变量并将其值分配为1,然后只需要打印变量的值即可。

As its value is increasing every row by 1, but starting value is always 1.

由于其值每行增加1,但起始值始终为1。

So, for that we have to declare the value of the starting number before column operation (second for loop) and need to increase it by 1 after the column operation section after the printing value.

因此,为此,我们必须在列运算之前声明起始编号的值(循环的第二个),并且需要在打印值后的列运算部分之后将起始编号增加1。

Code:

码:

#row operation
for row in range (0, 5):
n = 1
# column operation
for column in range (0, row+1):
print(n, end=" ")
n = n+1
# ending line
print('\r')

Pattern 4:

模式4:

1
2 3
4 5 6
7 8 9 10
11 12 13 14

To get the above pattern only we have to declare the variable before the row operation. Follow the code below,

为了获得上述模式,我们只需要在行操作之前声明变量。 请遵循以下代码,

Code:

码:

n = 1
#row operation
for row in range (0, 5):
# column operation
for column in range (0, row+1):
print(n, end=" ")
n = n+1
# ending line
print('\r')

Pattern 5:

模式5:

A
A  B
A   B  C
A  B  C  D
A  B  C  D  E

The above pattern can also be another type.

上面的模式也可以是其他类型。

For that should have the knowledge of ASCII values of 'A'.

为此,应具有ASCII值 “ A”的知识。

Its ASCII value is 65.

其ASCII值为 65。

In column operation We have to convert the ASCII value to character using chr() function.

在列操作中,我们必须使用chr()函数将ASCII值转换为字符。

Code:

码:

#row operation
for row in range (0, 5):
n = 65
# column operation
for column in range (0, row+1):
c = chr(n)
print(c, end=" ")
n = n+1
# ending line
print('\r')

Practice more python experiences here: python programs

在这里练习更多python经验: python程序

翻译自: https://www.includehelp.com/python/simple-pattern-printing-programs.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544249.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring Boot 如何解决多个定时任务阻塞问题?

大家好,我是不才磊哥~最近长文撸多了,有点累,今天来点简单的。今天这篇文章介绍一下Spring Boot 中 如何开启多线程定时任务?为什么Spring Boot 定时任务是单线程的?想要解释为什么,一定要从源码入手&#…

mysql之explain

⊙ 使用EXPLAIN语法检查查询执行计划 ◎ 查看索引的使用情况 ◎ 查看行扫描情况⊙ 避免使用SELECT * ◎ 这会导致表的全扫描 ◎ 网络带宽会被浪费话说工欲善其事,必先利其器。今天就简单介绍下EXPLAIN。 内容导航 idselect_typetabletypepossible_keyskeyke…

SpringCloud OpenFeign + Nacos正确打开方式!

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)Nacos 支持两种 HTTP 服务请求,一个是 REST Template,另一个是 Feign Client。之前的文章咱们介绍过…

array.unshift_Ruby中带有示例的Array.unshift()方法

array.unshiftArray.unshift()方法 (Array.unshift() Method) In this article, we will study about Array.unshift() Method. You all must be thinking the method must be doing something which is related to unshifting of objects in the Array instance. It is not as…

为什么创建线程池一定要用ThreadPoolExecutor?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)在 Java 语言中,并发编程都是依靠线程池完成的,而线程池的创建方式又有很多,但从…

ruby array_Ruby中带有示例的Array.fill()方法(3)

ruby arrayArray.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() method. You all must be thinking the method must be doing something related to populate the Array instance. Well, we will figure this out in the rest of our …

Objects.equals有坑

前言最近review别人代码的时候,发现有个同事,在某个业务场景下,使用Objects.equals方法判断两个值相等时,返回了跟预期不一致的结果,引起了我的兴趣。原本以为判断结果会返回true的,但实际上返回了false。记…

strtoupper 小写_PHP strtoupper()函数与示例

strtoupper 小写PHP strtoupper()函数 (PHP strtoupper() function) strtoupper() function is a string function it accepts the string and returns an uppercase string. strtoupper()函数是一个字符串函数,它接受字符串并返回大写字符串。 Syntax: 句法&#…

Java 18 正式发布,默认 UTF-8,finalize 被弃用,别再乱用了!

JDK 18 正式发布JDK 17 刚发布半年,JDK 18 又如期而至,JDK 版本号这算是成年了?JDK 18 发布了,栈长继续为大家解读!JDK 18 延续了 JDK 17 开创的免费策略,但,JDK 18~20 不是长期支持…

Spring官方推荐的@Transactional还能导致生产事故?

在Spring中进行事务管理非常简单,只需要在方法上加上注解Transactional,Spring就可以自动帮我们进行事务的开启、提交、回滚操作。甚至很多人心里已经将Spring事务与Transactional划上了等号,只要有数据库相关操作就直接给方法加上Transactio…

python函数实例化_用Python实例化函数

python函数实例化In terms of Mathematics and Computer science, currying is the approach/technique by which we can break multiple-argument function to single argument function. 从数学和计算机科学的角度来看, 柯里化是一种方法/技术,通过它我…

京东二面:MySQL 主从延迟、读写分离 7 种解决方案!

我们都知道互联网数据有个特性,大部分场景都是 读多写少,比如:微博、微信、淘宝电商,按照 二八原则,读流量占比甚至能达到 90%结合这个特性,我们对底层的数据库架构也会做相应调整。采用 读写分离处理过程&…

array_keys_PHP array_keys()函数与示例

array_keysPHP array_keys()函数 (PHP array_keys() function) array_keys() function is used to get the keys of an array, it accepts an array as an argument and returns a new array containing keys. array_keys()函数用于获取数组的键,它接受一个数组作为…

再见Postman,这款API神器更好用!

代码未动,文档先行其实大家都知道 API 文档先行的重要性,但是在实践过程中往往会遇到很多困难。程序员最讨厌的两件事:1. 写文档,2. 别人不写文档。大多数开发人员不愿意写 API 文档的原因是写文档短期收益远低于付出的成本&#…

strtolower_PHP strtolower()函数与示例

strtolowerPHP strtolower()函数 (PHP strtolower() function) strtolower() function is a string function, it accepts the string and returns an lowercase string. strtolower()函数是一个字符串函数,它接受该字符串并返回小写字符串。 Syntax: 句法&#xf…

如何保证数据库和缓存双写一致性?

前言数据库和缓存(比如:redis)双写数据一致性问题,是一个跟开发语言无关的公共问题。尤其在高并发的场景下,这个问题变得更加严重。我很负责的告诉大家,该问题无论在面试,还是工作中遇到的概率非…

面试官:AtomicInteger是如何保证线程安全?

blog.csdn.net/nanhuaibeian/article/details/120936139一、为什么引入 AtomicInteger ?谈到线程安全,会首先想到了synchronized 和 Lock,但是这种方式又有一个名字,叫做互斥锁,一次只能有一个持有锁的线程进入,再加上…

机器学习 训练验证测试_测试前验证| 机器学习

机器学习 训练验证测试In my previous article, we have discussed about the need to train and test our model and we wrote a code to split the given data into training and test sets. 在上一篇文章中,我们讨论了训练和测试模型的必要性,并编写了…

如何判断线程池已经执行完所有任务了?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)很多场景下,我们需要等待线程池的所有任务都执行完,然后再进行下一步操作。对于线程 Thread …

IRCTC的完整形式是什么?

IRCTC:印度铁路餐饮和旅游公司 (IRCTC: Indian Railways Catering and Tourism Corporation) IRCTC is an abbreviation of Indian Railways Catering and Tourism Corporation. It is a subsidiary of the Indian Railway established by the Ministry of Railways…