Patterns

We can use Python to create patterns. Let us see what the following code produces.

screenshot-20.png

screenshot-21.png

Let us go through the code step by step.

First we define the function called patterncreate which accepts a number n as input. n is the length of the pattern. Here n is the formal parameter of the function.

We initialize a for loop that iterates from 0 to n. i stands for the rows. Inside the i loop we initialize another j loop. In every row we observe that the number of stars present is equal to the index of the row or row number. That means in the first row, there is 1 star, in the second row there are 2 stars and so on.

The j loop iterates from 0 to i+1. Inside the j loop we print the “*” and end = ” ” ends the print by the character = ” “(blank space).  The j loop handles the columns. So if i is 4, j varies from 0 to 3 .So * is printed 4 times. After a particular row, we move on to the next row by going to a new line by using “\r”.

The user is asked to enter his desired pattern length and accordingly function patterncreate is called by passing the parameter input. Here “input” is the actual parameter.

Try creating the following pattern.

screenshot-22.png

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: