0% found this document useful (0 votes)
4 views18 pages

Python Pattern11

The document outlines steps to print various patterns in Python, including number patterns, inverted pyramids, and diamond shapes, using loops and print statements. It also explains how to calculate sums of natural numbers, squares, and Fibonacci series using different methods. Key concepts include using nested loops for patterns and applying mathematical formulas for efficient calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Python Pattern11

The document outlines steps to print various patterns in Python, including number patterns, inverted pyramids, and diamond shapes, using loops and print statements. It also explains how to calculate sums of natural numbers, squares, and Fibonacci series using different methods. Key concepts include using nested loops for patterns and applying mathematical formulas for efficient calculations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Steps to Print Pattern in Python

To print any pattern, you must first understand its logic and
technique. Use the below steps to print any pattern in Python.
1. Decide the number of rows and columns
The number of rows and columns is crucial when printing a
pattern. To achieve this, we utilize two loops: outer loops
and nested loops. The outer loop is the number of rows, while the
inner loop tells us the column needed to print the pattern.

The input () function accepts the number of rows from a user to


decide the size of a pattern.
2. Iterate rows
Next, write an outer loop to Iterate the number of rows using a for
loop and range() function.
3. Iterate columns
Next, write the inner or nested loop to handle the number of
columns. The internal loop iteration depends on the values of the
outer loop.
4. Print star or number
Use the print() function in each iteration of nested for loop to
display the symbol or number of a pattern (like a star (asterisk *)
or number).
5. Add a new line after each iteration of the outer loop
Add a new line using the print() function after each iteration of the
outer loop so that the pattern displays appropriately
In this number pattern, we display a single digit on the first row,
two digits on the second row, and three digits on the third row.
This process will repeat until the number of rows is reached.
Note:
• The count of numbers on each row is equal to the current
row number.
• Also, each number is separated by space.
• We used a nested loop to print the pattern

Note: In each row, every next number is incremented by 1.


• This sets the variable rows to 5.
• It determines how many rows of numbers will be printed in
the triangle.
• This is a for loop that controls the number of rows.
• range(1, rows + 1) means it will start from 1 and go up
to 5 (since rows + 1 is 6, and range goes up to but doesn't
include the end value).
• So, i will take values 1, 2, 3, 4, and 5 — one for each row.
• This is a nested for loop, inside the outer loop.
• For each row i, this inner loop prints the numbers from 1 up to
i.
• For example, when i = 3, j will take values 1, 2, 3.
• • This prints the current value of j on the same line, followed
by a space.
• • The end=' ' part prevents the print() function from
moving to a new line after printing; instead, it adds a space.
• After finishing printing one row, this print() moves the
cursor to the next line.
• This ensures that each new row starts on a new line.

Inverted pyramid pattern of numbers

An inverted pyramid is a downward pattern where numbers get


reduced in each iteration, and on the last row, it shows only one
number. Use reverse for loop to print this pattern.
1. You set rows = 5 and start a counter b = 0.
2. The outer loop runs from 5 down to 1 (reverse).
3. In each row:
4. You increase b by 1.
5. Then print b multiple times (based on the row number,
decreasing each time).
6. Each row moves to a new line

Inverted Pyramid pattern with the same digit

1. Set number of rows to 5 (rows = 5).


2. Store the number to print in num (also 5).
3. Outer loop runs from 5 down to 1 (to create a reverse
triangle).
4. Inner loop prints the value num (5) multiple times —
decreasing by one each row.
5. After each row, move to the next line.

Alternate numbers pattern using a while loop

1. You’re using while loops to print a pattern.


2. Each row prints an odd number, calculated as (i * 2 -
1).
3. The number of times it prints equals the row number.
4. So, row 1 prints 1 once, row 2 prints 3 twice, row 3
prints 5 three times, etc.

Simple half pyramid pattern:

Right triangle pyramid of Stars


Diamond-shaped pattern of stars
1. The code prints a diamond-shaped pattern using stars (*).
2. First part builds the top half of the diamond (including the
middle row), centered with spaces.
3. Second part builds the bottom half, decreasing the number
of stars and increasing spaces.
4. k is used to control the spaces before each row of stars for
alignment.

Alphabets and letters pattern


In Python, there are ASCII values for each letter. To print the
patterns of letters and alphabets, we need to convert them to
their ASCII values.
• Decide the number of rows
• Start with ASCII number 65 ( ‘A’)
• Iterate a loop and in nested for loop use the char function to
convert ASCII number to its equivalent letter.
1. The code prints a pattern of characters starting from 'A'.
2. The outer loop runs for 7 rows.
3. The inner loop prints an increasing number of
characters per row, starting from 'A' and using the ASCII
value to get the next character.
4. After each row, it moves to the next line.

Pattern to display letter of the word


Sum of Natural Numbers (1 + 2 + 3 + ... + n)
1. Method 1: Uses a for loop to add up the numbers from
1 to n.
2. Method 2: Uses Python's built-in sum() function and
range() to sum the numbers from 1 to n.
3. Method 3: Uses the formula n * (n + 1) // 2 to directly
calculate the sum of the first n natural numbers, which
is the most efficient.

Sum of Squares (1^2 + 2^2 + 3^2 + ... + n^2)


def sum_of_squares(n):
total = 0
for i in range(1, n + 1):
total += i ** 2
return total

num_terms = 5
result = sum_of_squares(num_terms)
print(f"The sum of squares of the first {num_terms} natural
numbers is: {result}")

# Alternative using formula


def sum_of_squares_formula(n):
return n * (n + 1) * (2 * n + 1) // 6

num_terms = 5
result = sum_of_squares_formula(num_terms)
print(f"The sum of squares of the first {num_terms} natural
numbers is: {result}")

EXPLANATION
Method 1: Loops through numbers from 1 to n, squares each
number (i ** 2), and adds them up.
Method 2: Uses the formula n * (n + 1) * (2n + 1) // 6 to directly
calculate the sum of squares, which is faster.

Sum of a Series with a Pattern (e.g., 1 + 1/2 + 1/3 + ... + 1/n)

1. The function sum_of_series calculates the sum of the


harmonic series:

2. It loops through numbers from 1 to n, adds their reciprocals


(1/i) to the total.
3. For num_terms = 10, it computes the sum up to the 10th
term.
PYTHON PROGRAM TO FIND THE SUM OF FIBONACCI SERIES
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 …….

1. The code calculates the sum of the first n Fibonacci


numbers.
2. It starts with 0 and 1, then generates the rest using the
rule:
F(n) = F(n-1) + F(n-2)
3. Each new Fibonacci number is added to the total sum.
4. Finally, it prints the sum.

You might also like