0% found this document useful (0 votes)
31 views

Python File

Uploaded by

gauravjoshi1711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Python File

Uploaded by

gauravjoshi1711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PYTHON

PROGRAM 1
OBJECTIVE: To write a Python Program to print HELLO INDIA.
CODE: print(“HELLO INDIA”)

PROGRAM 2
OBJECTIVE: To write a Python program to Find the Division of Student.
CODE: def calculate_division(marks):
total_marks = sum(marks)

average_marks = total_marks / len(marks)

if average_marks >= 90:

return "FIRST DIVISION"

elif average_marks >= 80:

return "SECOND DIVISION"

elif average_marks >= 65:

return "THIRD DIVISION"

elif average_marks >= 50:

return "FOURTH DIVISION"

elif average_marks >= 33:

return "FIFTH DIVISION"

else:

return "Fail"

marks = [78, 82, 88, 75, 90]

division = calculate_division(marks)

print("Division:", division)

1 SPARSH GOYAL
PYTHON

PROGRAM 3

OBJECTIVE: To Write a Program that Implements Fibonacci Series.


CODE:
def fibonacci(n):
# Check if n is less than or equal to 0
if n <= 0:
print("Invalid input. Please enter a positive integer.")
return
# Initialize the first two terms of the series
fib_series = [0, 1]
# Generate the Fibonacci series up to the nth term
while len(fib_series) < n:
next_term = fib_series[-1] + fib_series[-2]
fib_series.append(next_term)
return fib_series
n = int(input("Enter the number of terms for Fibonacci series: "))
result = fibonacci(n)
print("Fibonacci series up to", n, "terms:", result)

2 SPARSH GOYAL
PYTHON

PROGRAM 4

OBJECTIVE: Write a Python Program for Factorial.


CODE: def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
else:
# Recursive case: factorial of n is n times factorial of (n-1)
return n * factorial(n - 1)
# Example usage
num = int(input("Enter a number to calculate its factorial: "))
result = factorial(num)
print("Factorial of", num, "is", result)

3 SPARSH GOYAL
PYTHON

PROGRAM 5

OBJECTIVE: Write a Python Program to Use of Functions.


CODE: def greet(name):
print("Hello,", name, "! Welcome to the program.")

# Function to add two numbers

def add(a, b):

return a + b

# Function to subtract two numbers

def subtract(a, b):

return a - b

# Example usage of functions

name = input("Enter your name: ")

greet(name)

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

print("Sum:", add(num1, num2)

print("Difference:", subtract(num1, num2))

4 SPARSH GOYAL
PYTHON

PROGRAM 6
OBJECTIVE: To write a Python Program to Implement List.
CODE: # Define an empty list
my_list = []
# Append elements to the list
my_list.append(10)
my_list.append(20)
my_list.append(30)
my_list.append(40)
my_list.append(50)
# Print the list
print("Original List:", my_list)
# Access elements by index
print("Element at index 0:", my_list[0])
print("Element at index 3:", my_list[3])
# Modify an element
my_list[1] = 25
print("Modified List:", my_list)
# Remove an element
removed_element = my_list.pop(2)
print("List after removing element at index 2:", my_list)
print("Removed element:", removed_element)
# Insert an element at a specific index
my_list.insert(2, 35)
print("List after inserting 35 at index 2:", my_list)
# Check if an element exists in the list
if 30 in my_list:
print("30 is present in the list")
else:
print("30 is not present in the list")
# Print the length of the list
print("Length of the list:", len(my_list))
5 SPARSH GOYAL
PYTHON

# Iterate over the list


print("El ements of the list:")
for element in my_list:
print(element)

6 SPARSH GOYAL
PYTHON

PROGRAM 7

OBJECTIVE: To Write a Python Program to Implement Tuples.


CODE: class MyTuple:
def __init__(self, *args):
self.data = args
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
def __str__(self):
return str(self.data)
# Example usage:
my_tuple = MyTuple(1, 2, 3)
print(my_tuple)
print(my_tuple[0])
print(len(my_tuple))

7 SPARSH GOYAL
PYTHON

PROGRAM 8

OBJECTIVE: To Write a Python Program to Implement INSERTION SORT.


CODE: def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Example usage:
my_list = [12, 11, 13, 5, 6]
insertion_sort(my_list)
print("Sorted array is:", my_list)

8 SPARSH GOYAL
PYTHON

PROGRAM 9
OBJECTIVE: To Write a Python Program to Implement MERGE SORT.
CODE: def merge_sort(arr):
if len(arr) > 1:

mid = len(arr) // 2

left_half = arr[:mid]

right_half = arr[mid:]

merge_sort(left_half)

merge_sort(right_half)

i=j=k=0

while i < len(left_half) and j < len(right_half):

if left_half[i] < right_half[j]:

arr[k] = left_half[i]

i += 1

else:

arr[k] = right_half[j]

j += 1

k += 1

while i < len(left_half):

arr[k] = left_half[i]

i += 1

k += 1

while j < len(right_half):

arr[k] = right_half[j]

j += 1

k += 1

# Example usage:

my_list = [12, 11, 13, 5, 6, 7]

merge_sort(my_list)

print("Sorted array is:", my_list)

9 SPARSH GOYAL
PYTHON

PROGRAM 10

OBJECTIVE: Write a Python Program to Find First N Prime Numbers.


CODE: def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def first_n_primes(n):
primes = []
num = 2
while len(primes) < n:
if is_prime(num):
primes.append(num)
num += 1
return primes
# Test the function
n = int(input("Enter the value of N: "))
print("First", n, "prime numbers are:", first_n_primes(n))

10 SPARSH GOYAL

You might also like