Sample Programs in Python
Here are some common Python programs, including pattern programs, that you can try out:
1. Hello World Program:
This is the most basic Python program.
print("Hello, World!")
2. Sum of Natural Numbers:
num = int(input("Enter a number: "))
sum_of_numbers = sum(range(1, num+1))
print(f"The sum of first {num} natural numbers is {sum_of_numbers}")
3. Fibonacci Sequence:
This program prints the Fibonacci sequence up to a given number.
num_terms = int(input("Enter the number of terms: "))
a, b = 0, 1
print("Fibonacci Sequence:")
for _ in range(num_terms):
print(a, end=" ")
a, b = b, a + b
4. Factorial of a Number:
Calculating the factorial of a given number.
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num+1):
factorial *= i
print(f"The factorial of {num} is {factorial}")
5. Prime Number Check:
This program checks if a number is prime.
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(f"{num} is not a prime number.")
break
else:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
Pattern Programs in Python
Pattern programs are a great way to improve logic and understanding of loops in Python. Below are some common patterns you can try:
1. Right-Angle Triangle Star Pattern:
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print("*" * i)
Output for n = 5:
*
**
***
****
*****
2. Inverted Right-Angle Triangle Star Pattern:
n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
print("*" * i)
Output for n = 5:
*****
****
***
**
*
3. Pyramid Star Pattern:
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print(" " * (n - i) + "*" * (2*i - 1))
Output for n = 5:
*
***
*****
*******
*********
4. Diamond Star Pattern:
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print(" " * (n - i) + "*" * (2*i - 1))
for i in range(n-1, 0, -1):
print(" " * (n - i) + "*" * (2*i - 1))
Output for n = 5:
*
***
*****
*******
*********
*******
*****
***
*
5. Number Pyramid Pattern:
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print(j, end="")
print()
Output for n = 5:
1
12
123
1234
12345
6. Hollow Square Pattern:
n = int(input("Enter the size of the square: "))
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("*", end="")
else:
print(" ", end="")
print()
Output for n = 5:
*****
* *
* *
* *
*****
7. Number Pyramid with Spaces:
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
print(" " * (n - i), end="")
for j in range(1, i + 1):
print(j, end=" ")
print()
Output for n = 5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Conclusion:
These sample programs will help you get familiar with Python’s basic syntax and control flow mechanisms such as loops and conditions. The pattern programs also help you practice how to use nested loops effectively to create different patterns.
Let me know if you'd like to explore more programs or need further assistance! 😊
0 Comments