๐Ÿ NIELIT O Level Practical โ€ข M3-R5.1

Python Programs

Explore complete Python source codes, practical programs, assignments, viva questions, and step-by-step video explanations from DEEPAKRAJTech live classes.

โœ” 100+ Programs ๐Ÿ’ป Source Code ๐Ÿ“˜ Practical Programs โœ” Viva Questions
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to check if a number is positive, negative or zero.
If input is: 10 then output should be: Positive
If input is: 0 then output should be: Zero
If input is: -9 then output should be: Negative

                    
  n = int(input("Enter a Number: "))

  if n == 0:
      print("Zero")
  elif n > 0:
      print("Positive")
  else:
      print("Negative")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement Python script to print factorial of a number.

                    
  n = int(input("Enter a Number: "))

  fact = 1

  while n > 0:
      fact = fact * n
      n = n - 1

  print("Factorial:", fact)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to demonstrate the working of max() and min().
If input is: 4, 12, 43.3, 19, 100 the output is Maximum: 100, Minmum: 4

                    
  # Method 1

  lst = []

  for i in range(1, 6):
      n = eval(input(f"Enter Number {i}: "))
      lst.append(n)
                      
  print("Maximum:", max(lst))
  print("Minimum:", min(lst))


  # Method 2

  numbers = eval(input("Enter Comma Separated Values: "))

  print("Maximum:", max(numbers))
  print("Minimum:", min(numbers))
                      
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement python script to check the given year is leap year or not.

                    
  import calendar

  year = int(input("Enter Year: "))

  if calendar.isleap(year):
      print(year, "is leap year")
  else:
      print(year, "is not leap year")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a program for checking the given number is even or odd.

                    
  n = int(input("Enter a Number: "))

  if n % 2 == 0:
      print("Even Number")
  else:
      print("Odd Number")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program that reads two integer inputs from keyboard and compute the quotient and remainder.
If input is -786 and 8, then output should be:
The Quotient is -99
The Remainder is 6.

                    
  x = int(input("Enter First Number: "))
  y = int(input("Enter Second Number: "))
  
  q = x // y
  r = x % y
  
  print("The Quotient is", q)
  print("The Remainder is", r)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to access the current year, month and date attributes.

                    
  from datetime import datetime

  dt = datetime.now()

  print("Year:", dt.year)
  print("Month:", dt.month)
  print("Date:", dt.day)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement Python Script to generate first N natural numbers.

                    
  n = int(input("Enter value of N: "))

  for i in range(1, n + 1):
      print(i, end=" ")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement Python Script to check given number is palindrome or not.

                    
  num = int(input("Enter a Number: "))
  temp = num
  rev = 0

  while temp > 0:
      digit = temp % 10
      rev = rev * 10 + digit
      temp = temp // 10

  if num == rev:
      print("Palindrome")
  else:
      print("Not Palindrome")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Given a NumPy array, write a Python program to reverse the NumPy array.
If input is [3, 6, 7, 2, 5, 1, 8]
then output should be [8 1 5 2 7 6 3]

                    
  import numpy as np

  arr = np.array([3, 6, 7, 2, 5, 1, 8])

  print("Original Array:", arr)
  print("Reversed Array:", arr[::-1])
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Using NumPy array, write a Python program to find the arrayโ€™s dimensions, the total number of elements and the data type of its elements.
If input is: [1, 2, 3, 4, 5] then output is: (5, ); 5; int32
If input is: ['a', 'b', 'c', 'd', 'e'] then output is: (5, ); 5; < U1

                    
  import numpy as np

  print("Array 1 Shape:", arr1.shape)
  print("Array 1 Size:", arr1.size)
  print("Array 1 Data Type:", arr1.dtype)

  print("================================")

  print("Array 2 Shape:", arr2.shape)
  print("Array 2 Size:", arr2.size)
  print("Array 2 Data Type:", arr2.dtype)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program that reads a positive integer from the user and computes the sum of its digits.
If input is: 999 then output should be: 27
If input is: 640 then output should be: 10

                    
  num = int(input("Enter a Number: "))

  sum = 0
                      
  while num > 0:
      digit = num % 10
      sum = sum + digit
      num = num // 10
                      
  print("Sum of Digits:", sum)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to find the length of a dictionary.
If input is: {โ€œEnglandโ€: โ€œLondonโ€, โ€œItalyโ€: โ€œRomeโ€} then output should be: 2
If input is: {10: โ€œtenโ€, 20: โ€œtwentyโ€, 30: โ€œthirtyโ€} then output should be: 3
If input is: {} then output should be: 0

                    
  dict1 = {"England": "London", "Italy": "Rome"}
  dict2 = {10: "ten", 20: "twenty", 30: "thirty"}
  dict3 = {}
  
  print("Length of dict1 is =", len(dict1))
  print("Length of dict2 is =", len(dict2))
  print("Length of dict3 is =", len(dict3))
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to iterate through a Tuple using for loop.
If input is: (โ€˜appleโ€™, โ€˜bananaโ€™, โ€˜orangeโ€™) then output should be:
apple
banana
orange

                    
  tp = ("apple", "banana", "orange")

  for item in tp:
      print(item)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program that:
(a) Accepts a sentence from the user as input.
(b) Displays the first 5 characters and the last 5 characters of the sentence using slicing.
(c) Concatenates the sentence with another predefined string.

                    
  txt = input("Enter a sentence: ")

  greeting = "Hello, "

  print("First 5 characters:", txt[:5])
  print("Last 5 characters:", txt[-5:])
  print(greeting + txt)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a python program that calculates the sum of squares of the first n natural numbers.
If input is 5, then output should be 55.
If input is 10, then output should be 385.
If input is 50, then output should be 42925.
If input is 100, then output should be 338250.

                    
  n = int(input("Enter the value of n: "))

  sum = 0

  for num in range(1, n + 1):
      sum = sum + num ** 2

  print("Sum of squares of the first", n, "natural numbers is =", sum)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to create a list of tuples from given list having number and its cube in each tuple.
If input is: [4, 1, 6, 2] then output should be: [(4, 64), (1, 1), (6, 216), (2, 8)]

                    
  lst = [4, 1, 6, 2]

  cubelist = []

  for item in lst:
      cubelist.append((item, item ** 3))

  print(cubelist)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to count the number of digits in an integer.
If input is: 123456 then output should be: 6
If input is: 153 then output should be: 3
If input is: 00003452 then output should be: 4

                    
  n = int(input("Enter a number: "))

  digits = len(str(n))

  print("Number of digits =", digits)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement Python Script to generate prime numbers series up to n.

                    
  x = int(input("Enter the value of n: "))

  for n in range(2, x + 1):
      for i in range(2, n):
          if n % i == 0:
              break
      else:
          print(n, end=" ")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Given an integer number n, write a python program to print the factorial of this number.
If input is: 4 then output should be: 24
If input is: 5 then output should be: 120
If input is: 6 then output should be: 720

                    
  n = int(input("Enter any n number: "))

  fact = 1

  while n != 0:
      fact = fact * n
      n = n - 1

  print("Factorial is = ", fact)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Create a Python program that removes even duplicate positive integer numbers (includes zero) from a list and prints the unique numbers in the order they first appeared.
If input is 4 3 2 1, then output should be 4 3 2 1.
If input is 0 0 0 0 1, then output should be 0 1.
If input is 1 2 1 3 2 3, then output should be 1 2 1 3 3.

                    
  lst = [1, 2, 3, 2, 4, 4]

  for i in range(len(lst)):
      if lst[i] % 2 == 0 and lst[i] >= 0:
          if lst[i] not in lst[:i]:
              print(lst[i], end=" ")
      else:
          print(lst[i], end=" ")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a program that takes 2 numbers as command line arguments and prints its sum.

                    
  # To run this program, use the CMD command line and provide two numbers as arguments for example: add.py 5 10

  import sys

  n1 = int(sys.argv[1])
  n2 = int(sys.argv[2])
  sum = n1 + n2

  print("Sum is ", sum)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to calculate the area of a triangle.
Area = 0.5 * base * height

                    
  base = float(input("Enter base: "))
  height = float(input("Enter height: "))

  area = 0.5 * base * height

  print("area of a triangle = ", area)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement Python Script to check given number is Armstrong or not.

                    
  n = int(input("Enter any number: "))

  power = len(str(n))

  temp = n
  sum = 0

  while temp != 0:
      digit = temp % 10
      sum = sum + digit ** power
      temp = temp // 10

  if n == sum:
      print(n, "Number is Armstrong")
  else:
      print(n, "Number is not Armstrong")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program which returns a string by converting all the characters to their opposite letter case (uppercase to lowercase and vice versa).
If input is: JoHn CeNa , then output should be: jOhN cEnA

                    
  st = input("Enter a string: ")

  viceversa = st.swapcase()

  print(viceversa)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement Python Script to check given string is palindrome or not.

                    
  st = input("Enter a string: ")

  rev = st[::-1]

  if st == rev:
      print("String is palindrome")
  else:
      print("String is not palindrome")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Python program to display the elements of a list in reverse order.

                    
  lst = [10, 20, 30, 40, 50]

  for item in reversed(lst):
      print(item, end=" ")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Implement python script to accept line of text and find the number of characters, number of vowels and number of blank spaces in it.

                    
  text = input("Enter a line of text: ")
  characters = len(text)
  vowels = 0
  spaces = 0
  
  for ch in text:
      if ch.lower() in "aeiou":
          vowels += 1
      elif ch == " ":
          spaces += 1
  
  print("Number of Characters =", characters)
  print("Number of Vowels =", vowels)
  print("Number of Blank Spaces =", spaces)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a program that takes as input a list having a mix of 10 positive numbers and a key value.
Apply linear search to find whether the key is present in the list or not.
If input is: [10, 50, 30, 70, 80, 20, 90, 40], key = 30
Then output should be: Yes
If input is: [10, 50, 30, 70, 80, 20, 90, 40], key = 15
Then output should be: No

                    
  lst = []

  print("Enter 10 positive numbers:")
  
  for i in range(10):
      num = int(input())
      lst.append(num)
  
  key = int(input("Enter key to search: "))
  
  for i in lst:
      if i == key:
          print("Yes")
          break
  else:
      print("No")
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jul 2026

Write a Function to Compute the Sum of n Terms of the Following Series.
Series: X + X^4 / 4! + X^6 / 6! + X^8 / 8! + ... for any positive integer value of X.

                    
  import math
                      
  def series(x, n):
      total = 0
                      
      for i in range(1, n + 1):
          if i == 1:
              total += x
          else:
              power = 2 * i
              total += (x ** power) / math.factorial(power)
                      
      return total
                      
  x = int(input("Enter value of X: "))
  n = int(input("Enter number of terms: "))
                      
  print("Sum of Series =", series(x, n))
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a Python function that takes two lists and returns True if they have at least one common item.

                    
  # Python function that takes two lists and returns True if they have at least one common item.

  def common_item(list1, list2):
      for item in list1:
          if item in list2:
              return True
      return False
                      
  # Example usage:
  list_a = [1, 2, 3, 4]
  list_b = [4, 5, 6]
  print(common_item(list_a, list_b))  
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a program which takes list of numbers as input and finds:
a) The largest number in the list
b) The smallest number in the list
c) Product of all the items in the list

                    
  # Finds the largest, smallest and product from a list 

  numbers = list(eval(input("Enter numbers separated by comma: ")))

  largest = max(numbers)

  smallest = min(numbers)

  product = 1
  for num in numbers:
      product *= num

  print("Original List:", numbers)
  print("Largest Number:", largest)
  print("Smallest Number:", smallest)
  print("Product of All Numbers:", product)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a Python program that takes list of numbers as input from the user and produces a cumulative list where each element in the list at any position n is sum of all elements at positions upto n-1.

                    
  # Python Program: Cumulative Sum List 

  numbers = list(eval(input("Enter numbers separated by comma: ")))
  s = 0
  cumulative = []

  for n in numbers:
      cumulative.append(s)
      s = s + n

  print("Original List:", numbers)
  print("Cumulative List:", cumulative)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a program that takes in a sentence as input and displays the number of words, number of capital letters, no. of small letters and number of special symbols.

                    
  # Program Count Words, Capital Letters, Small Letters, Digits, and Special Symbols

  sentence = input("Enter a sentence: ")

  word_count = 0
  capital_letters = 0
  small_letters = 0
  digits = 0
  special_symbols = 0

  word_count = len(sentence.split())

  # Loop through each character in the sentence
  for char in sentence:
      if char.isupper():
          capital_letters += 1
      elif char.islower():
          small_letters += 1
      elif char.isdigit():
          digits += 1
      elif not char.isspace() and not char.isalnum():
          special_symbols += 1

  # Display the results
  print("\n--- Result ---")
  print("Number of words        :", word_count)
  print("Number of capital letters:", capital_letters)
  print("Number of small letters  :", small_letters)
  print("Number of digits         :", digits)
  print("Number of special symbols:", special_symbols)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a function that takes a string as parameter and returns a string with every successive repetitive character replaced by? e.g. school may become scho?l.

                    
  # Replace every successive repetitive character with '?' in Python

  def replace_repetitions(text):
      result = ""
      for i in range(len(text)):
          if i > 0 and text[i] == text[i - 1]:
              result += "?"
          else:
              result += text[i]
      return result

  text = replace_repetitions('school')
  print(text)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a program to compute the wages of a daily laborer as per the following rules: -
Hours Worked Rate Applicable Upto first 8 hrs Rs100/-
(a) For next 4 hrs Rs30/- per hr extra
(b) For next 4 hrs Rs40/- per hr extra
(c) For next 4 hrs Rs50/- per hr extra
(d) For rest Rs60/- per hr extra

                    
  # Python Program to compute the wages of a daily laborer as per the following rules:

  name = input("Enter the name of the labourer: ")
  hours = int(input("Enter the number of hours worked: "))

  wage = 0

  # Calculate wages based on hours worked
  if hours <= 8:
      wage = hours * 100

  elif hours <= 12:
      wage = 8 * 100 + (hours - 8) * 130  # โ‚น100 base + โ‚น30 extra

  elif hours <= 16:
      wage = 8 * 100 + 4 * 130 + (hours - 12) * 140  # โ‚น100 base + โ‚น40 extra

  elif hours <= 20:
      wage = 8 * 100 + 4 * 130 + 4 * 140 + (hours - 16) * 150  # โ‚น100 base + โ‚น50 extra

  else:
      wage = 8 * 100 + 4 * 130 + 4 * 140 + 4 * 150 + (hours - 20) * 160  # โ‚น100 base + โ‚น60 extra

  # Display output
  print("Labourer Name:", name)
  print("Worked Hours:", hours)
  print("Total Wages: Rs", wage)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a program to multiply two numbers by repeated addition
Example 6*7 = 6+6+6+6+6+6+6

                    
  # Program to multiply two numbers using repeated addition

  # Input from user
  x = int(input("Enter the number to be added repeatedly: "))
  n = int(input("Enter how many times to add it: "))

  total = 0

  # Repeated addition
  for i in range(n):
      total = total + x

  # Showing repeated addition result
  print("Sum of two numbers using repeated addition is: ", total)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a function to obtain sum n terms of the following series for any positive integer value of X 1+x/1!+x2/2!+x3/3!+โ€ฆ

                    
  def exp_series(x, n):
      sum = 1.0  # Starting with the first term (i = 0)
      term = 1.0  # To keep track of x^i / i!

      for i in range(1, n + 1):
          term *= x / i  # Efficient way to calculate x^i / i!
          sum += term

      return sum

  # Example usage:
  x = 3
  n = 5
  print("Sum of series:", exp_series(x, n))
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a function to obtain sum n terms of the following series for any positive integer value of X +X3 /3! +X5 /5! ! +X7 /7! + โ€ฆ

                    
  import math

  # Input from the user
  x = int(input("Enter the value of X: "))
  n = int(input("Enter number of terms: "))

  sum = 0

  # Loop for n terms
  for i in range(n):
      power = 2 * i + 1        # Only odd powers: 1, 3, 5, ...
      fact = math.factorial(power)
      term = (x ** power) / fact
      sum += term              # Add each term to the sum

  # Final Output
  print("Sum of the series is", sum)
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Write a program to print all Armstrong numbers in a given range. Note: An Armstrong number is a number whose sum of cubes of digits is equal to the number itself. E.g. 370=33+73+03

                    
  # Program to print Armstrong numbers in a given range
  start = int(input("Enter starting number: "))
  end = int(input("Enter ending number: "))
  
  for n in range(start, end + 1):
      temp = n
      sum = 0
      digits = len(str(n))
  
      while temp != 0:
          rem = temp % 10
          sum += rem ** digits
          temp = temp // 10
  
      if n == sum:
          print(n, end=' ')
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Python Program to print Hello World!

                    
  print("Hello World!")
  print('Hello World!')
  print("""Hello World!""")
  print('''Hello World!''')
                    
                    

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Python Program to Add Two Numbers

  
  # This Python  program adds two numbers
  
  num1 = 14
  num2 = 25
  
  sum = num1 + num2
  
  print('The sum of ', num1, ' and ', num2, ' is ', sum)

  

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Python Program to Add Two Numbers From the User

    
  num1 = input('Enter first number: ')
  num2 = input('Enter second number: ')
  
  # Add two numbers
  sum = float(num1) + float(num2)
  
  # Display the sum
  print('The sum of ', num1, ' and ', num2, ' is ', sum)
      
    
  

โ–ถ๏ธ Watch Now on YouTube
M3-R5.1 O Level Practical ๐Ÿ“… Jan 2026

Automate any Chat-Messenger with Python

In order to access the Python library, you need to install it first :   pip install pyautogui

                
  ''' 
  Automate any Chat-Messenger with Python
  Author :  DEEPAK RAJ
  '''
                  
  # import the necessary module
  import pyautogui
  import time
                  
  time.sleep(5)
                  
  text = 'โ˜  You Are Hack โ˜ '
                  
  while True:
      pyautogui.typewrite(text)
      pyautogui.press('enter')
      time.sleep(1)            
                
              

โ–ถ๏ธ Watch Now on YouTube