Cara menggunakan python pattern programs pdf

Table of Contents

  • Printing Hollow Square Star Pattern​
  • Python Program:
  • Description
  • Explanation
  • Description
  • Explanation
  • How do you make a hollow square in Python?
  • How do I print a square box pattern in Python?
  • How do you print a hollow square pattern?
  • How do you print a hollow pyramid pattern in Python?

Printing Hollow Square Star Pattern​

In this Python Program, we will be discussing about how to write a program to print Hollow Square Star Pattern. In this pattern, there are n*n rows and columns are present. We can see in this pattern, all stars are present at the boundary of the pattern. And filled with spaces inside the boundary. “For Loop” and “If-Else” Condition will help to print this Hollow Square Star Pattern.

Working:

Step 1. Start

Step 2. Take input from the user and store it in any variable [‘num’ in this case].

Step 3. Run a loop ‘i’ number of times to iterate through all the rows. From i=0 to num. 

Step 4. Run a nested loop inside the main loop for printing stars . From j=0 to num.

Step 5. Inside the above loop print stars only if i==0 or i==num-1 or j==0 or j==num-1 in all other cases print a blank space.

Step 6. Move to the next line by printing a new line by using print[] function.

Stop 7. Stop

Python Program:

num = int[input["Enter number:"]]

for i in range[0, num]:
    for j in range[0, num]:
        if i == 0 or j == 0 or j == num-1 or i == num - 1:
            print["*", end=""]
        else:
            print[" ", end=""]
    print[]

# This code is contributed by Shubhanshu Arya [Prepinsta Placement Cell Student] 

Overview

In this shot, we will discuss how to use stars to generate a hollow square pattern in Python.

There are tons of patterns that can be printed with Python once you have a strong grip on concepts involving loops.

Here, we will use simple for loops to generate a hollow square pattern with stars.

Description

A square is a plane figure that consists of four sides that are identical in terms of magnitude. A square has four angles, all of which are 90 degrees.

To execute this shape with Python programming, we will use two for loops:

  • An outer loop to loop through the number of rows.

  • An inner loop to print the patterns along with the columns.

Code

Let’s look at the code snippet below.

# No of rows

n = 5

# Loop through rows

for i in range[1,n+1]:

# Loop to print pattern

for j in range[1,n+1]:

# Printing Pattern

if [i==1 or i==n or j==1 or j==n]:

print["*", end = " "]

else :

print[" ", end = " "]

print[]

Explanation

  • In line 2, we take the input for the number of rows [i.e. the length of the square].

  • In line 5, we create a for loop to iterate through the number of rows.

  • In line 9, we create an inner nested for loop that runs along the columns.

  • From lines 12 to 15, conditional statements have been provided that print the given pattern. The end statement helps to stay on the same line.

  • In line 16, the print[] statement is used to move to the next line.

In this way, we can use stars to generate a hollow square pattern in Python.

CONTRIBUTOR

Vinisha Maheshwari

In this shot, we will discuss how to generate a hollow square pattern using numbers in Python.

Once we have a firm grip over loops’ concepts, tons of patterns can be printed using python. Here, we will use simple for loops to generate a hollow square pattern using numbers.

Description

A square is a plane figure consisting of four identical sides in terms of magnitude. It has four angles, all of which are 90 degrees. To execute this using Python programming, we will be using two for loops:

  • An Outer Loop: To loop through the number of rows.
  • An Inner Loop: To print the patterns along with the columns.

Code

Let us look at the code snippet below:

# No of rows

n = 5

# Loop through rows

for i in range[1, n+1]:

# Loop to print pattern

for j in range[1, n+1]:

# Printing Pattern

if [i == 1 or i == n or j == 1 or j == n]:

print[i, end = " "]

else :

print[" ", end = " "]

print[]

Explanation

  • In Line 2, we take the input for the number of rows [i.e., the length of the square].

  • In Line 5, we create a for loop to iterate through the number of rows.

  • In Line 9, we create an inner nested for loop that runs along the columns

  • From lines 12 to 15, conditional statements have been provided that print the given pattern. The end statement helps to stay on the same line.

  • In Line 16, the print[] statement is used to move to the next line.

CONTRIBUTOR

Vinisha Maheshwari

How do you make a hollow square in Python?

To make hollow square marks, we can use marker "ks" and markerfacecolor="none", markersize="15" and markeredge color="red".

How do I print a square box pattern in Python?

Pattern - 9: Square Pattern using with number.

rows = int[input["Enter the number of rows: "]].

for i in range[1, rows + 1]:.

for j in range[1, rows + 1]:.

# Check condition if value of j is smaller or equal than..

# the j then print i otherwise print j..

if j

Bài mới nhất

Chủ Đề