This section covers various examples in Python programming Language. These Programs examples cover a wide range of programming areas in Computer Science. Every example program includes the problem description, problem solution, source code, program explanation and run time test cases. These examples range from simple Python programs to Mathematical functions, lists, strings, sets, dictionary, recursions, no-recursions, file handling, classes and objects, linked list, stacks and queues, searching and sorting, trees, heap, graphs, games, greedy algoritms and dynamic programming.
In this section we are showing some code related to patterns that are based on logic and how we can use for loop to create these pattern.
Pattern Programs in PythonQ1. Write a program in Python to print the following pattern a) 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5Ans.
for i in range(1, 6):
for j in range(1, i+1):
print(j, end = " ")
print()Q2. Write a program in Python to print the following pattern 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5Ans.
for i in range(5,0,-1):
for j in range(5, 5-i,-1):
print(j, end = " ")
print()
3. Write a program in Python to print the following pattern 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Ans.
for i in range(5, 0, -1):
for j in range(i):
print(i, end = " ")
print()
Q4. Write a program in Python to print the following pattern
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Ans.
for i in range(6,0,-1):
for j in range(1, i):
print(j, end = " ")
print()
Q5. Write a program in Python to print the following pattern
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Ans.
for i in range(1, 6):
for j in range(1, i+1):
print(i, end = " ")
print()
Q6. Write a program in Python to print the following pattern
5 4 4 3 3 3 2 2 2 2 1 1 1 1 1Ans.
for i in range(5, 0,-1):
for j in range(6,i,-1):
print(i, end = " ")
print()
Q7. Write a program in Python to print the following pattern
5 5 4 5 4 3 5 4 3 2 5 4 3 2 1
Ans.
for i in range(5, 0,-1):
for j in range(5,i-1,-1):
print(j, end = " ")
print()
Q8. Write a program in Python to print the following pattern
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
for i in range(5):
for j in range(5-i-1):
print(" ",end=" ")
for j in range(i+1):
print(j+1, end=" ")
print()
Q9. Write a program in Python to print the following pattern
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Ans.
for i in range(5):
for j in range(5-i-1):
print(" ",end=" ")
for j in range(i+1):
print(i+1, end=" ")
print()
Q10. Write a program in Python to print the following pattern
1 2 1 3 2 1 4 3 2 1 5 4 3 2 1
Ans.
for i in range(5):
for j in range(5-i-1):
print(" ", end=" ")
for j in range(i+1, 0, -1):
print(j, end=" ")
print()
Q11. Write a program in Python to print the following pattern
* * * * * * * * * * * * * * *
Ans.
for i in range(1,6):
for j in range(i):
print(" * ", end = "")
print( )
Q12. Write a program in Python to print the following pattern
* * * * * * * * * * * * * * *
Ans.
for i in range(5,0,-1):
for j in range(i):
print(" * ", end = "")
print( )
0 Comments