0% found this document useful (0 votes)
29 views

14 Iteration Pattern Printing 2 Code

The document discusses various patterns that can be printed using loops and conditionals in Python. It includes examples of printing staircases with and without spaces, hollow and filled diamond patterns, and exercises for readers to try variations.

Uploaded by

Rahul B. Waghale
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)
29 views

14 Iteration Pattern Printing 2 Code

The document discusses various patterns that can be printed using loops and conditionals in Python. It includes examples of printing staircases with and without spaces, hollow and filled diamond patterns, and exercises for readers to try variations.

Uploaded by

Rahul B. Waghale
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/ 9

Pattern_Printing_2.

md 7/19/2022

Pattern Printing 2
Warm up Problem
n = 5
d = 3

print(n*10 + d)

53

n = 0
d = 5
print(n*10 + d)

n = 1253

while n > 0:
dig = n % 10 # get the last digit
print(dig)
n = n // 10 # remove the last digit

3
5
2
1

Reverse
n = 1253
rev = 0

1/9
Pattern_Printing_2.md 7/19/2022

while n > 0:
dig = n % 10 # get the last digit
rev = rev*10 + dig
n = n // 10 # remove the last digit
print(rev)

3
35
352
3521

Pattern 1:
Staircase with Spaces on left
N=5

*
**
***
****
*****

N = int(input())

for i in range(1, N + 1):


# print (N - i) spaces
for _ in range(N - i):
print(' ', end= '')
# print i stars
for _ in range(i):
print('*', end='')
print()

10

*
**
***
****
*****
******

2/9
Pattern_Printing_2.md 7/19/2022

*******
********
*********
**********

Pattern 2:
Staircase with Spaces on left II
N=5

*****
****
***
**
*

N = int(input())

for i in range(1, N + 1):


# (i - 1) spaces
for j in range(i - 1):
print(' ', end='')

# (N - i + 1) stars
for j in range(N - i + 1):
print('*', end='')

# print the new line


print()

*****
****
***

3/9
Pattern_Printing_2.md 7/19/2022

**
*

N = int(input())

for i in range(1, N + 1):


# (N - i + 1) stars
for j in range(N - i + 1):
print('*', end='')
# (i - 1) spaces
for j in range(i - 1):
print(' ', end='')
# print the new line
print()

*****
****
***
**
*

Pattern 3:
Hollow Diamond Upper part
N=3

******
** **
* *

N = int(input())
for i in range(1, N + 1):
# print(N-i+1) stars
for j in range(N - i + 1):
print('*', end='')

# print 2*i - 2 spaces


for j in range(2*i - 2):
print(' ', end='')

4/9
Pattern_Printing_2.md 7/19/2022

# print(N-i+1) stars
for j in range(N - i + 1):
print('*', end='')

print()

**********
**** ****
*** ***
** **
* *

Pattern 4:
Hollow Diamond Lower part
N=3

* *
** **
******

N = int(input())
for i in range(1, N + 1):
# print(i) stars
for j in range(i):
print('*', end='')

# print 2*(N - i) spaces


for j in range(2*(N - i)):
print(' ', end='')

# print(i) stars
for j in range(i):
print('*', end='')

print()

5/9
Pattern_Printing_2.md 7/19/2022

* *
** **
*** ***
**** ****
**********

Full Hollow Diamond


N = int(input())
for i in range(1, N):
# print(N-i+1) stars
for j in range(N - i + 1):
print('*', end='')

# print 2*i - 2 spaces


for j in range(2*i - 2):
print(' ', end='')

# print(N-i+1) stars
for j in range(N - i + 1):
print('*', end='')

print()

for i in range(1, N + 1):


# print(i) stars
for j in range(i):
print('*', end='')

# print 2*(N - i) spaces


for j in range(2*(N - i)):
print(' ', end='')

# print(i) stars
for j in range(i):
print('*', end='')

print()

6/9
Pattern_Printing_2.md 7/19/2022

************
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
************

Filled Diamond
N = int(input())
for i in range(1, N):
# print(N-i+1) stars
for j in range(N - i + 1):
print(' ', end='')

# print 2*i - 2 spaces


for j in range(2*i - 2):
print('*', end='')

# print(N-i+1) stars
for j in range(N - i + 1):
print(' ', end='')

print()

for i in range(1, N + 1):


# print(i) stars
for j in range(i):
print(' ', end='')

# print 2*(N - i) spaces


for j in range(2*(N - i)):
print('*', end='')

# print(i) stars
for j in range(i):
print(' ', end='')
7/9
Pattern_Printing_2.md 7/19/2022

print()

# HW: Try to think of getting one extra star

**
****
******
********
******
****
**

Doubts
N = 15

total_sum_of_factors = 0
for i in range(1, N):
if N % i == 0:
# print(i)
total_sum_of_factors += i

print(total_sum_of_factors == N)

False

N = 3
M = 2
for i in range(N): # [0, 1, 2]
for j in range(M): # [0, 1]
print(i, j)

0 0
0 1
1 0
1 1

8/9
Pattern_Printing_2.md 7/19/2022

2 0
2 1

N = 3
M = 2
for i in range(N): # [0, 1, 2]
for i in range(M): # [0, 1]
print(i, j)

0 1
1 1
0 1
1 1
0 1
1 1

9/9

You might also like