Sign in to confirm you’re not a bot
This helps protect our community. Learn more
Square and Triangle Patterns in Python || Lesson 15 || Python || Learning Monkey ||
19Likes
833Views
2020Aug 30
#python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse Square and Triangle Patterns in Python In this class, we do practice on square and triangle patterns in python. Square Pattern Before practice, you should have some basic knowledge of nested loops. Click here. Here we will concentrate on how to do coding for a given problem. These simple programs help you in solving the complex programs we discuss in subsequent classes. Q1) Write a program to display the square pattern shown below.
  • * * * *
  • * * * *
  • * * * *
  • * * * *
  • * * * *
The program has to take input about the side of the square. It should be an integer input. For example, if the side of the square value is 5. we have to display a 5X5 square shown above. Suppose the input value is six. Display a 6X6 square. Logic: Suppose the input is given as 5. In the first line, we have to display 5-star symbols with space between them. In the second line, display five stars. In the same way, we have to display five lines. Previously we discussed when to uses loops. Something to do repeatedly we use loops. To display five stars, we use a loop. To repeat the above loop five times. We need a nested loop. The program to display the square pattern is shown below. size = int(input(“enter the size of square”) for I in range(size): for j in range(size): print(“*”,end =” “) print() From the above program, the loop statement with i variable executes five times. Inside the loop, we have written another loop that executes five times. The inner loop is displaying one line with five stars. To display stars in the same line, use print(“*”,end=” “) For each outer loop, the inner loop will display a line of five stars. Triangle Pattern Q2) Write a program to display the triangle pattern given below. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Take the input from the user. If the input is given 5. Display the above triangle pattern. First-line display 1. The second line display 1 2. Similarly, in each line, increase the numbers to be displayed by 1. We can analyze that this program needs a nested loop. The program is given below. lines=int(input(“enter no of lines”) for I in range(1,lines+1): for j in range(i): print(j+1,end=” “) print() The outer for loop will execute five times from the above program if the line variable value is 5. Each time it went into the loop, the inner loop executes for i times. i is a variable from the outer loop. The first iteration of the outer loop i value is 1. The inner loop executes one time and displays 1. The second iteration of the outer loop i value is 2. The inner loop executes two times and displays 1 2. The outer loop executes five times. Related links: Link for playlists:    / @learningmonkey   Link for our website: https://learningmonkey.in Follow us on Facebook @   / learningmonkey   Follow us on Instagram @   / learningmonkey1   Follow us on Twitter @   / _learningmonkey   Mail us @ learningmonkey01@gmail.com

Follow along using the transcript.

Learning Monkey

62.6K subscribers