# Learn Python With Dhawal -6- Chapter

CHAPTER 6

Before we begin our journey with the Functions and learn how to use them, there's one more thing you need to know about loops and using them. Just like the if condition you can also nest the functions together to form a larger loop. You can nest as many functions as you like we will see how they're nested in the example below.

Write A Program to print a pattern

1
12
123
1234

Input:

1. for i in range(1,5):
2.     for j in range (1,i+1):
3.         print (j, end=" ")
4.     print()

Output:

1. 1 2
2. 1 2 3
3. 1 2 3 4
Now, as we can see, we have to print a pattern. To print this particular pattern we need to simplify it into rows and columns. As you can see as the number of rows grow, the number of columns grow as well. So in row 1 we just print 1, in row 2 we print 1,2 and in row 3 we print 1,2,3 and for the final tow aka row 4 we print 1,2,3,4. So if you can notice the direct correlation between the number of rows and the numbers to be printed, here the tasks becomes easy for you.

We'd start creating a loop that can print the numbers 1 to 4 in first attempt. For that you'd print every row number and have range (1,5). I am using range (1,5) instead of (0,4) since we actually need the number 1 here.

Once you are satisfactorily printing the row numbers we can move on to the next step that is printing the numbers before that row number. So now create a loop inside that for loop with the range limited at the row number. So for the first row the range would be (1,2), for the second row the range would be (1,3), for the third row the range would be (1,4) and for the last one it'd be (1,5). Now you won't manually apply the ranges here as the row number 'i' already states the value that we need but since in row number 1 the value of i will be 1 and we need 2, we would write range as (1,1+i) for the second for loop.

Now, try mentally running the loop or try running the loop and getting output mentally with a paper and pen before you actually push that run button to execute the code.

Note: The best way to try a code before you actually execute it on the computer is using a paper and pen to run the code and try getting output in a way that an actual computer would do. This way you'd grasp your code in much better manner as well as you'd realise the faults that you'd actually make in your code much before the compiler just dumps your code for some error.

Since we want to print the column numbers which we are using the variable j for, we would print j on each loop run. Now you'd wonder that there's 'end' keyword with a blank space value, in python after a print statement by default it takes you to a new line but if you want to replace it with a space and not a new line then you have to use the 'end' keyword.

Note: You can always search for more features or get help using the 'help()' command. Type the help() command and write the name of command or feature you are trying to search help for inside the parenthesis to get more information about it.

Now you can try the code for yourself and see if it works for you, else you can always check it here and troubleshoot. This code is mostly for building your logic.

Write A Program to print a pattern

*
**
***
****

Input:

1. for i in range(1,5):
2.     for j in range (1,i+1):
3.         print ('*', end=" ")
4.     print()

Output:

1. * *
2. * * *
3. * * * *
A similar code just like above but here we are printing stars instead of the column numbers. Simply replacing the j variable from above code with the * here gets the work done.

Write A Program to print the pattern
*
* *
* * *
* * * *

Input:

1. for i in range(1,5):
2.     for j in range(1,5-i):
3.         print (end=' ')
4.     for j in range (1,i+1):
5.         print ('*', end=" ")
6.     print()

Output:

1.    *
2.   * *
3.  * * *
4. * * * *
Now here's a huge trick, you need it to form a * pyramid. You know how to print the * equal to the row numbers but to print the pyramid you need to know how to print the amount of spaces you'd need for that. Here there's space between two *s so make sure you also note that.

Here, as you can see in the code there are two 'for' loops. One for loop is just like in the example above to print the *s but the one before that * printing loop there's one loop to print plain spaces. It will print the spaces inverse to the amount of the stars you want minus one. since you need 3 spaces in the middle it'd just take range (1,4) which would translate to (5-i) as the row number increases.

You print the spaces as shown in the code above and then you head to the * printing loop. So in this manner you'd just continue to print spaces and *s in a regular fashion.

Write a Program to print the pattern :
*
* *
* * *
* * * *
* * *
* *
*
Input:

1. for i in range(1,5):
2.     for j in range(1,5-i):
3.         print (end=' ')
4.     for j in range (1,i+1):
5.         print ('*', end=" ")
6.     print()
7. for i in range(1,5):
8.     for j in range(1,1+i):
9.         print (end=' ')
10.     for j in range (1,5-i):
11.         print ('*', end=" ")
12.     print()

Output:

1.    *
2.   * *
3.  * * *
4. * * * *
5.  * * *
6.   * *
7.    *

Here now the task gets much more complicated as now not only we have to print a * diamond but also the spaces. Just like from the code above first we start printing spaces in decreasing order and the *s in the increasing order until we stop and print no spaces. Then we print the spaces in increasing order and * in decreasing order until there's 1 star in last.

I feel you can analyse this code on your own as it is very much self explanatory if you have fairly understood the previous examples. You'd need more and more practice to build this logic and programming is mostly all about logic.

Write A Program to print this pattern taking the number of max stars from the user
*
* *
* * *
* *
*

Input:

1. num=int(input("Enter the number from 1-10: "))
2. num=num+1
3. for i in range(1,num):
4.     for j in range(1,num-i):
5.         print (end=' ')
6.     for j in range (1,i+1):
7.         print ('*', end=" ")
8.     print()
9. for i in range(1,num):
10.     for j in range(1,1+i):
11.         print (end=' ')
12.     for j in range (1,num-i):
13.         print ('*', end=" ")
14.     print()

Output:

1. Enter the number from 1-10: 3
2.   *
3.  * *
4. * * *
5.  * *
6.   *
Here we are taking the number of Max stars from the user to print the same diamond. There's a simple input taking process after which we increase the input given by user by adding one to satisfy out range conditions. Then we just replace the 5 from the previous code with 'num' and make it flexible. It can print as many star diamond as you want but for the simplicity purpose the number asked is for 1-10, although there's no checking condition to check if the user entered a number more than 10 or less than 1, You can add that checking condition yourself and try out how it works.

In Next chapter we will learn about functions and how to use them in python.

~*~*~*~

This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.

~*~*~*~

Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.