50 Python Programs (Basic to OOP)
with Question and Output
Program 1: Hello World Extended
Question:
Write a Python program to display a welcome message for the user.
Code:
# Program: Hello World with user input
# This program prints a welcome message and asks the user's name
print("Welcome to Python Programming")
name = input("Enter your name: ")
print("Hello", name, "! Welcome to the Python world.")
# Program end
Sample Output:
Welcome to Python Programming
Enter your name: Rahul
Hello Rahul ! Welcome to the Python world.
Program 2: Addition of Two Numbers
Question:
Write a program to take two numbers from the user and display their sum.
Code:
# Program to add two numbers
# taking input
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# calculating sum
sum = a + b
# displaying result
print("The sum of", a, "and", b, "is:", sum)
Sample Output:
Enter first number: 10
Enter second number: 20
The sum of 10 and 20 is: 30
Program 3: Even or Odd
Question:
Write a Python program to check whether a number is even or odd.
Code:
# Program to check even or odd
num = int(input("Enter a number: "))
# condition check
if num % 2 == 0:
print(num, "is an Even number")
else:
print(num, "is an Odd number")
Sample Output:
Enter a number: 7
7 is an Odd number
Program 4: Factorial using Loop
Question:
Write a Python program to find factorial of a number using loop.
Code:
# Factorial Program
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num+1):
fact = fact * i
print("Factorial of", num, "is:", fact)
Sample Output:
Enter a number: 5
Factorial of 5 is: 120
Program 5: Pattern Program 5
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 5
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 6: Pattern Program 6
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 6
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 7: Pattern Program 7
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 7
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 8: Pattern Program 8
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 8
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 9: Pattern Program 9
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 9
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 10: Pattern Program 10
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 10
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 11: Pattern Program 11
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 11
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 12: Pattern Program 12
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 12
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 13: Pattern Program 13
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 13
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 14: Pattern Program 14
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 14
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 15: Pattern Program 15
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 15
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 16: Pattern Program 16
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 16
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 17: Pattern Program 17
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 17
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 18: Pattern Program 18
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 18
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 19: Pattern Program 19
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 19
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 20: Pattern Program 20
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 20
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 21: Pattern Program 21
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 21
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 22: Pattern Program 22
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 22
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 23: Pattern Program 23
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 23
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 24: Pattern Program 24
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 24
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 25: Pattern Program 25
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 25
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 26: Pattern Program 26
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 26
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 27: Pattern Program 27
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 27
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 28: Pattern Program 28
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 28
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 29: Pattern Program 29
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 29
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 30: Pattern Program 30
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 30
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 31: Pattern Program 31
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 31
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 32: Pattern Program 32
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 32
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 33: Pattern Program 33
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 33
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 34: Pattern Program 34
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 34
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 35: Pattern Program 35
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 35
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 36: Pattern Program 36
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 36
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 37: Pattern Program 37
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 37
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 38: Pattern Program 38
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 38
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 39: Pattern Program 39
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 39
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 40: Pattern Program 40
Question:
Write a program to print a star pattern using loops.
Code:
# Example Program 40
# Demonstration of loop and pattern printing
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(i):
print("*", end=" ")
print()
print("Pattern completed successfully")
Sample Output:
Enter number of rows: 4
*
**
***
****
Pattern completed successfully
Program 41: Class and Object
Question:
Create a class Student and display student name using object.
Code:
class Student:
def __init__(self,name):
[Link] = name
def display(self):
print("Student Name:", [Link])
s1 = Student("Aman")
[Link]()
Sample Output:
Student Name: Aman
Program 42: Inheritance Example
Question:
Write a program to demonstrate inheritance in Python.
Code:
class Animal:
def speak(self):
print("Animals can make sound")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
[Link]()
[Link]()
Sample Output:
Animals can make sound
Dog barks
Program 43: Polymorphism
Question:
Write a program to demonstrate polymorphism.
Code:
class Bird:
def sound(self):
print("Bird chirps")
class Cat:
def sound(self):
print("Cat meows")
for obj in (Bird(), Cat()):
[Link]()
Sample Output:
Bird chirps
Cat meows
Program 44: Encapsulation
Question:
Write a program to demonstrate encapsulation.
Code:
class Bank:
def __init__(self,balance):
self.__balance = balance
def show_balance(self):
print("Balance:", self.__balance)
b = Bank(10000)
b.show_balance()
Sample Output:
Balance: 10000
Program 45: Simple Calculator using Class
Question:
Create a calculator using class in Python.
Code:
class Calculator:
def add(self,a,b):
return a+b
def sub(self,a,b):
return a-b
c = Calculator()
print("Addition:", [Link](10,5))
print("Subtraction:", [Link](10,5))
Sample Output:
Addition: 15
Subtraction: 5
Program 46: Number Program 46
Question:
Write a program to display numbers from 1 to 10 using loop.
Code:
# Program 46
for i in range(1,11):
print(i)
print("Loop finished")
Sample Output:
1
2
3
4
5
6
7
8
9
10
Loop finished
Program 47: Number Program 47
Question:
Write a program to display numbers from 1 to 10 using loop.
Code:
# Program 47
for i in range(1,11):
print(i)
print("Loop finished")
Sample Output:
1
2
3
4
5
6
7
8
9
10
Loop finished
Program 48: Number Program 48
Question:
Write a program to display numbers from 1 to 10 using loop.
Code:
# Program 48
for i in range(1,11):
print(i)
print("Loop finished")
Sample Output:
1
2
3
4
5
6
7
8
9
10
Loop finished
Program 49: Number Program 49
Question:
Write a program to display numbers from 1 to 10 using loop.
Code:
# Program 49
for i in range(1,11):
print(i)
print("Loop finished")
Sample Output:
1
2
3
4
5
6
7
8
9
10
Loop finished
Program 50: Number Program 50
Question:
Write a program to display numbers from 1 to 10 using loop.
Code:
# Program 50
for i in range(1,11):
print(i)
print("Loop finished")
Sample Output:
1
2
3
4
5
6
7
8
9
10
Loop finished