Computer Science, asked by Anonymous, 11 months ago

Write python script to print the following patterns :


a.


1

1 2

1 2 3

1 2 3 4

1 2 3 4 5


b.


* * * * *

* * * *

* * *

* *

*

Answers

Answered by AbhijithPrakash
16

We have to make a python script for these patterns;

We'll have to make loops for making these patterns.

For pattern (a):

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Here we have 5 rows and columns, so we'll use 2 nested "for" loops;

To begin our code we'll start with the for loop with i as the variable;

So, the code will look like ;

  • for i in range(1,6):

This will be the outer loop.

So now let's try to create the inner loop;

We will be using the j as the variable.

The code will be;

  • for j in range(1, i+1):

The "i+1" present inside the parentheses is to change values according to the outer loop.

Let's combine the 2 codes.

We'll get

\textrm{for i in range(1,6):}\\\textrm{	for j in range(1, i+1):}

Now let's start printing the values;

We will use the j variable to print as it will increase numbers at each column.

The code will be,

  • print(j, end=" ")

We have to use the end parameter so that all the required values come in rows.

And this print function will be inside the inner loop.

Combine the code;

\textrm{for i in range(1,6):}\\\textrm{\quad for j in range(1, i+1):}\\\textrm{\quad \quad print(j, end=" ")}

But if we run the code all the number will be printed in one line, and that's not we want, so we will use "print("\r")" or "print()" bellow the outer loop.

So, the entire code will be;

\textrm{for i in range(1,6):}\\\textrm{\quad for j in range(1, i+1):}\\\textrm{\quad \quad print(j, end=" ")}\\\textrm{\quad print()}

for i in range(1,6):

 for j in range(1, i+1):

   print(j, end=" ")

 print()

Now let's start (b)

* * * * *

* * * *

* * *

* *

*

Well, the code will be same but the number's in the parentheses will be different and instead of j we will print *;

Here is the code;

\textrm{for i in range(5,0,-1):}\\\textrm{\quad for j in range(i,0,-1):}\\\textrm{\quad \quad print("*", end=" ")}\\\mathrm{\quad print()}

for i in range (5,0,-1):

   for j in range (i,0,-1):

       print("*", end="")

   print()


Anonymous: Awesome and Great
AbhijithPrakash: ^^"
Answered by Anonymous
4

\huge{\star}{\underline{\boxed{\red{\sf{Answer :}}}}}{\star}

1) For (a)

_____________[Code]______________

print("1")

print("12")

print("123")

print("1234")

print('12345")

______________[End]______________

Output

1

12

123

1234

12345

=================================================================

2) for(b)

________________[Code]___________

print("*****")

print("****")

print("***")

print("**")

print("*")

_______________[End]______________

Output

*****

****

***

**

*

=======================================

Krishna Bro (Bending Reality)

Hope you understand

Similar questions