PROGRAM 1
1. Python program to find the factorial of a number provided by the user.
# To take input from the user
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
OUTPUT:
Enter a number: 5
The factorial of 5 is 120
PROGRAM 2
2. Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print(num, " is even ")
else:
print(num, " is odd")
OUTPUT:
Enter a number: 45
45 is odd
Enter a number: 22
22 is even
PROGRAM 3
3. Write a Python program to take a number as input from the user and check whether the
number is Prime or not.
num = int(input("Enter a number: "))
if num == 0 or num == 1:
print(num, "is not a prime number")
elif num > 1: # check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number") # if input number is less than or equal to 1, it is not prime
else:
print(num,"is not a prime number")
OUTPUT:
Enter a number: 45
45 is not a prime number
3 times 15 is 45
Enter a number: 13
13 is a prime number
PROGRAM 4
4. Python program to print numbers until the user enters 0
number = int(input('Enter a number: '))
# iterate until the user enters 0
while number != 0:
print(f'You entered : ', number)
number = int(input('Enter a number: '))
print('The end.')
OUTPUT:
Enter a number: 44
You entered 44.
Enter a number: 23
You entered 23.
Enter a number: 12
You entered 12.
Enter a number: 0
The end.
=== Code Execution Successful ===
PROGRAM 5
5. Python program to enter an operator by the user and perform the mathematical
operations according to the operator entered
operator = input("Enter an operator: ")
x=5
y = 10
match operator:
case '+':
result = x + y
case '-':
result = x - y
case '*':
result = x * y
case '/':
result = x / y
print(result)
OTPUT:
Enter an operator: +
15
Enter an operator: -
-5
Enter an operator: *
50
Enter an operator: /
0.5
PROGRAM 6
6. A menu based Python program to enter a subject and marks by the user and print
according to the following cases:
Score Print
Physics or chemistry or biology>= 80 Excellent in Science!
English or Grammar >=80 Excellent in English!
Mathematics>= 80 Excellent in Maths!
Default case Needs Improvement!
subject = input("Enter a subject: ")
score = int(input("Enter a score: "))
match subject:
# if score is 80 or higher in Physics or Chemistry
case 'Physics' | 'Chemistry' if score >= 80:
print("Excellent in Science!")
# if score is 80 or higher in English or Grammar
case 'English' | 'Grammar' if score >= 80:
print("Excellent in English!")
# if score is 80 or higher in Maths
case 'Maths' if score >= 80:
print("Excellent in Maths!")
case _:
print(f"Needs improvement in {subject}!")
OUTPUT:
Enter a subject: Grammar
Enter a score: 45
Needs improvement in Grammar!
Enter a subject: Maths
Enter a score: 89
Excellent in Maths!
PROGRAM 7
7. Python program to print a star pattern
*
**
***
****
*****
row=5
for i in range(1,row+1):
for j in range(1,i+1):
print("*",end='')
print()
OUTPUT:
**
***
****
*****
=== Code Execution Successful ===
PROGRAM 8
8. Python program to print a number pattern
1
22
333
4444
55555
Program:
row=6
for i in range(row):
for j in range(i):
print(i,end='')
print('')
OUTPUT
22
333
4444
55555
=== Code Execution Successful ===
PROGRAM 9
9. Python program to print a number pattern
1
12
123
1234
12345
PROGRAM:
row=5
for i in range(1,row+1):
for j in range(1,i+1):
print(j,end='')
print()
OUTPUT:
12
123
1234
12345
=== Code Execution Successful ===
PROGRAM 10
10. Python program to print a reverse number pattern
111111
22222
3333
444
55
6
row=6
b=0
for i in range(row,0,-1):
b+=1
for j in range(1,i+1):
print(b,end='')
print('')
OUTPUT
111111
22222
3333
444
55
=== Code Execution Successful ===
PROGRAM 11
11. Python program to print a reverse number pattern
55555
5555
555
55
5
PROGRAM:
row=5
num=row
for i in range(row,0,-1):
for j in range(0,i):
print(num,end='')
print(' ')
OUTPUT
55555
5555
555
55
=== Code Execution Successful ===
PROGRAM 12
12. Write a Program to create a straight line using Matpolib
import matplotlib.pyplot as plt
import numpy as np
x = [0,1,2,3,4]
y = [0,1,2,3,4]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Straight line')
plt.show()
OUTPUT
PROGRAM 13
13. Write a program to create a list of all students in your class and sort them in alphabetical
order.
students=['John','Asit','Shobhit','Eva','Dinesh','Garima']
sorted_students= sorted(students)
print("Sorted list of students")
for students in sorted_students:
print(students)
OUTPUT
Sorted list of students
Asit
Dinesh
Eva
Garima
John
Shobhit
PROGRAM 14
14. Program to create a one dimensional NumPy array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
OUTPUT:
[1 2 3 4 5]
<class 'numpy.ndarray'>
PROGRAM 15
15. Create a 3X4 D array with random integer values less than 10
import numpy as np
a= np.random.randint(10,size=(3,4))
print(a)
OUTPUT:
[[5 9 4 3]
[6 0 6 3]
[7 2 9 5]]