How To Draw A Set Square
Howdy Techies,
The Square shape in python is the most frequently asked question in virtually interview processes. In this weblog, we will learn how to draw a square shape in Python.
Square shape in Python
#i. Square shape in Python using Star Pattern
To draw this pattern we are using loop and range function of Python. Basically in this program nosotros are specifically using 5 rows and 5 columns. Yous can modify it every bit per your requirement.
for row in range(5): for col in range(v): print("*", terminate=" ") impress()
Subsequently executing the above code, the following results will be obtained –
* * * * * * * * * * * * * * * * * * * * * * * * *
#2. Square shape in Python using Star Pattern with user input
Nosotros are using Python's loop and range office to describe this design. Basically in this plan, nosotros are taking input from the user to create a square shape.
input_number = int(input("Enter the side of square:")) for row in range(input_number): for col in range(5): print("*", stop=" ") print()
After executing the in a higher place code, the following results will be obtained –
Enter the side of square:five * * * * * * * * * * * * * * * * * * * * * * * * *
#iii. Python number square shape
We are using Python'south loop and range function to describe this pattern. Basically in this program, nosotros are writing the program for the number square.
for row in range(5): for col in range(5): print(row, end=" ") print()
After executing the above code, the following results will be obtained –
0 0 0 0 0 1 1 ane i i two two 2 two 2 three 3 3 iii 3 4 4 iv iv 4
#iv. The hollow foursquare pattern in Python
We are using Python'south loop and range function to draw this pattern. Basically in this plan, we are writing the program for hollow foursquare.
for i in range(v): for j in range(5): if(i == 0 or i == five - one or j == 0 or j == five - 1): print('*', stop = ' ') else: print(' ', terminate = ' ') print()
Later executing the above code, the following results volition be obtained –
* * * * * * * * * * * * * * * *
#five. Numbers in square shape in Python
Method ane: Using for loop
val = int(input("Enter the number of rows:")) n_list = [[0 for x in range(val)] for y in range(val)] n = 1 low = 0 high = val - 1 count = int((val + 1) / two) for i in range(count): for j in range(low, high + 1): n_list[i][j] = northward northward = north + 1 for j in range(low + one, high + ane): n_list[j][loftier] = n north = n + 1 for j in range(high - one, low - 1, -1): n_list[loftier][j] = n n = n + 1 for j in range(loftier - i, low, -1): n_list[j][low] = n n = n + ane low = low + 1 high = high - 1 for i in range(val): for j in range(val): print(n_list[i][j], end="\t") print()
Subsequently executing the in a higher place lawmaking, the following results will be obtained –
1 two three 4 5 16 17 18 19 6 15 24 25 twenty 7 14 23 22 21 eight 13 12 11 10 nine
#vi. Square in python using turtle library
In this program, nosotros are using the turtle library to draw the square shape.
import turtle t = turtle.Turtle() # drawing 1st side t.forward(100) t.left(90) # drawing second side t.forrad(100) t.left(ninety) # drawing third side t.forrard(100) t.left(90) # cartoon 4th side t.forrad(100) t.left(90) turtle.done()
Later on executing the above code, the following results volition be obtained –
See theirofficial site for more information about the Turtle Library.
I have covered a different pattern of squares in this blog. Annotate below if you want any other blazon of square.
How To Draw A Set Square,
Source: https://techpluslifestyle.com/technology/draw-square-shape-in-python/
Posted by: hodgestherrielf.blogspot.com
0 Response to "How To Draw A Set Square"
Post a Comment