Python pattern programs are a common way for beginners to practice and understand fundamental programming concepts, particularly loop control structures like for and while loops, and how to manipulate output using the print() function.


Program code :
n = 5
for i in range(1, n+1):
print("*" * i)
Output :
*
**
***
****
*****
Program code :
n = 5
for i in range(n, 0, -1):
print("*" * i)
Output :
*****
****
***
**
*
Program code :
n = 5
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end=" ")
print()
Output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Program code :
n = 5
for i in range(1, n+1):
print((str(i) + " ") * i)
Output :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Program code :
n = 5
for i in range(n):
for j in range(i+1):
print(chr(65 + j), end=" ")
print()
Output :
A
A B
A B C
A B C D
A B C D E
Program code :
n = 5
for i in range(n):
print(" " * (n - i - 1) + "* " * (i+1))
Output :
*
* *
* * *
* * * *
* * * * *
Program code :
n = 5
num = 1
for i in range(1, n+1):
for j in range(i):
print(num, end=" ")
num += 1
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Program code :
n = 5
for i in range(1, n+1):
print(" " * (n - i) + "*" * i)
Output :
*
**
***
****
*****
Program code :
n = 5
for i in range(1, n+1):
for j in range(1, i+1):
print((i+j)%2, end=" ")
print()
Output :
0
1 0
0 1 0
1 0 1 0
0 1 0 1 0
Program code :
n = 5
for i in range(n, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()
Output :
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Program code :
n = 5
for i in range(1, n+1):
for j in range(1, i+1):
if j == 1 or j == i or i == n:
print("*", end=" ")
Else:
print(" ", end=" ")
print()
Output :
*
* *
* *
* *
* * * * *
Program code :
n = 5
for i in range(1, n+1):
print(" " * (n - i) + "* " * i)
Output :
*
* *
* * *
* * * *
* * * * *
Program code :
n = 5
# Top half
for i in range(1, n+1):
print(" " * (n - i) + "* " * i)
# Bottom half
for i in range(n-1, 0, -1):
print(" " * (n - i) + "* " * i)
Output :
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Program code :
From math import factorial
n = 5
for i in range(n):
for j in range(n - i + 1):
print(" ", end=" ")
for k in range(i + 1):
print(factorial(i) // (factorial(k) * factorial(i - k)), end=" ")
print()
Output :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Program code :
n = 5
for i in range(1, n+1):
for j in range(n - i):
print(" ", end="")
for j in range(2 * i - 1):
if j == 0 or j == 2*i - 2 or i == n:
print("*", end="")
Else:
print(" ", end="")
print()
Output :
*
* *
* *
* *
*********
Program code :
n = 5
for i in range(n):
print(" " * (n-i-1), end="")
for j in range(i+1):
print(chr(65 + j), end=" ")
print()
Output :
A
A B
A B C
A B C D
A B C D E
Program code :
n = 5
num = 1
for i in range(1, n+1):
for j in range(i):
print(num, end=" ")
num += 1
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Program code :
n = 5
for i in range(1, n+1):
print(" " * (n - i), end="")
for j in range(1, i+1):
print(j, end="")
print()
Output :
1
12
123
1234
12345
Program code :
n = 5
for i in range(n, 0, -1):
print(" " * (n - i) + "* " * i)
for i in range(2, n+1):
print(" " * (n - i) + "* " * i)
Output :
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Program code :
def show_menu():
print("\n===== Expense Tracker Menu =====")
print("1. Add Expense")
print("2. View All Expenses")
print("3. Total Spent")
print("4. Exit")
def add_expense(expenses):
category = input("Enter expense category (e.g., Food, Travel): ")
Try:
amount = float(input("Enter amount spent: ₹"))
expenses.append((category, amount))
print(f"Added ₹{amount} under '{category}' category.")
Except ValueError:
print("Invalid amount. Please enter a number.")
def view_expenses(expenses):
If not expenses:
print("No expenses recorded.")
return
print("\n--- Expense List ---")
For idx, (category, amount) in enumerate(expenses, 1):
print(f"{idx}. {category} - ₹{amount:.2f}")
def total_spent(expenses):
total = sum(amount for _, amount in expenses)
print(f"\nTotal Spent: ₹{total:.2f}")
def main():
expenses = []
While True:
show_menu()
choice = input("Enter your choice (1-4): ")
if choice == "1":
add_expense(expenses)
elif choice == "2":
view_expenses(expenses)
elif choice == "3":
total_spent(expenses)
elif choice == "4":
print("Exiting Expense Tracker. Goodbye!")
break
Else:
print("Invalid choice. Please select a valid option.")
main()
Sample Output :
===== Expense Tracker Menu =====
1. Add Expense
2. View All Expenses
3. Total Spent
4. Exit
Enter your choice (1-4): 1
Enter expense category (e.g., Food, Travel): Food
Enter amount spent: ₹250
Added ₹250.0 under 'Food' category.
===== Expense Tracker Menu =====
1. Add Expense
2. View All Expenses
3. Total Spent
4. Exit
Enter your choice (1-4): 1
Enter expense category (e.g., Food, Travel): Travel
Enter amount spent: ₹100
Added ₹100.0 under 'Travel' category.
===== Expense Tracker Menu =====
1. Add Expense
2. View All Expenses
3. Total Spent
4. Exit
Enter your choice (1-4): 2
--- Expense List ---
1. Food - ₹250.00
2. Travel - ₹100.00
===== Expense Tracker Menu =====
1. Add Expense
2. View All Expenses
3. Total Spent
4. Exit
Enter your choice (1-4): 3
Total Spent: ₹350.00
===== Expense Tracker Menu =====
1. Add Expense
2. View All Expenses
3. Total Spent
4. Exit
Enter your choice (1-4): 4
Exiting Expense Tracker. Goodbye!
Program code :
def show_menu():
print("\n=== Library Menu ===")
print("1. Display Available Books")
print("2. Borrow Book")
print("3. Return Book")
print("4. Exit")
def display_books(books):
If not books:
print("No books available.")
Else:
print("\nAvailable Books:")
for idx, book in enumerate(books, 1):
print(f"{idx}. {book}")
def borrow_book(books, borrowed):
display_books(books)
name = input("Enter the name of the book you want to borrow: ")
If the name is in books:
books.remove(name)
borrowed.append(name)
print(f"You borrowed '{name}'. Please return it on time.")
Else:
print("Book not available or already borrowed.")
def return_book(books, borrowed):
name = input("Enter the name of the book you want to return: ")
If the name is borrowed:
borrowed.remove(name)
books.append(name)
print(f"Thank you for returning '{name}'.")
Else:
print("This book was not borrowed.")
def main():
books = ["1984", "Python for Beginners", "Data Science 101", "Harry Potter"]
borrowed = []
while True:
show_menu()
choice = input("Enter your choice: ")
if choice == "1":
display_books(books)
elif choice == "2":
borrow_book(books, borrowed)
elif choice == "3":
return_book(books, borrowed)
elif choice == "4":
print("Thank you for using the library system!")
break
Else:
print("Invalid input. Try again.")
main()
Sample Output :
=== Library Menu ===
1. Display Available Books
2. Borrow a Book
3. Return Book
4. Exit
Enter your choice: 1
Available Books:
1. 1984
2. Python for Beginners
3. Data Science 101
4. Harry Potter
=== Library Menu ===
1. Display Available Books
2. Borrow a Book
3. Return Book
4. Exit
Enter your choice: 2
Available Books:
1. 1984
2. Python for Beginners
3. Data Science 101
4. Harry Potter
Enter the name of the book you want to borrow: 1984
You borrowed '1984'. Please return it on time.
=== Library Menu ===
1. Display Available Books
2. Borrow a Book
3. Return Book
4. Exit
Enter your choice: 1
Available Books:
1. Python for Beginners
2. Data Science 101
3. Harry Potter
=== Library Menu ===
1. Display Available Books
2. Borrow a Book
3. Return Book
4. Exit
Enter your choice: 3
Enter the name of the book you want to return: 1984
Thank you for returning '1984'.
=== Library Menu ===
1. Display Available Books
2. Borrow a Book
3. Return Book
4. Exit
Enter your choice: 1
Available Books:
1. Python for Beginners
2. Data Science 101
3. Harry Potter
4. 1984
=== Library Menu ===
1. Display Available Books
2. Borrow a Book
3. Return Book
4. Exit
Enter your choice: 4
Thank you for using the library system!
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR