Program.
1
Program for calculator
# Define function to add two numbers
def add(x, y):
return x + y
# Define function to subtract two numbers
def subtract(x, y):
return x - y
# Define function to multiply two numbers
def multiply(x, y):
return x * y
# Define function to divide two numbers
def divide(x, y):
return x / y
# Print instructions for the user
print("Select operation.")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
# Ask user to enter their choice
choice = input("Enter choice (1/2/3/4): ")
# Ask user to enter two numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Perform the operation based on the user's choice
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("Invalid input")
Program .2
'''num = 5
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
n3 = n1 + n2
n1 = n2
n2 = n3
print(n3, end=" ")
print()'''
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Program.3
Number1 = int(input("enter number"))
Number3 = int(input("enter percentage"))
Number2 = int(input("enter duration in month"))
interest = Number1 * Number3 * Number2 / 100
print (str(interest))
Program.4
lst=[]
n=int(input("enter the number of elements:"))
print("enter the elements:")
for i in range(0,n):
ele=int(input())
lst.append(ele)
print("the list is:",lst)
Program.5
name=input("enter your name:")
eng=float(input("enter your english marks:"))
sc=float(input("enter your science marks:"))
math=float(input("enter your maths marks:"))
cs=float(input("enter your computer science marks:"))
eco=float(input("enter your economices marks:"))
total=eng+sc+math+cs+eco
per=(total/500)*100
print(name," scored",total,"marks out of 500.")
print(name, "percentage is:", per)
Program.6
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# Iterate each element in list
# and add them in variable total
for ele in list1:
if ele % 2 == 0:
list1.remove(ele)
# printing modified list
print("New list after removing all even numbers: ", list1)
Program.7
string = "GeekforGeeks!"
vowels = "aeiouAEIOU"
count = sum(string.count(vowel) for vowel in vowels)
print(count)
Program.8
# Python program to print all
# prime number in an interval
def prime(x, y):
prime_list = []
for i in range(x, y):
if i == 0 or i == 1:
continue
else:
for j in range(2, int(i/2)+1):
if i % j == 0:
break
else:
prime_list.append(i)
return prime_list
# Driver program
starting_range = 2
ending_range = 7
lst = prime(starting_range, ending_range)
if len(lst) == 0:
print("There are no prime numbers in this range")
else:
print("The prime numbers in this range are: ", lst)
Program.9
# Python program to multiply all values in the
# list using traversal
def multiplyList(myList):
# Multiply elements one by one
result = 1
for x in myList:
result = result * x
return result
# Driver code
list1 = [1, 2, 3]
list2 = [3, 2, 4]
list3 = [5,8,7]
print(multiplyList(list1))
print(multiplyList(list2))
print(multiplyList(list3))
Program.10
# Python program to remove empty tuples from a
# list of tuples function to remove empty tuples
# using list comprehension
def Remove(tuples):
tuples = [t for t in tuples if t]
return tuples
# Driver Code
tuples = [(), ('ram','15','8'), (), ('laxman', 'sita'),
('krishna', 'akbar', '45'), ('',''),()]
print(Remove(tuples))
Program.11
# Python 3 program for recursive binary search.
# Modifications needed for the older Python 2 are found in comments.
# Returns index of x in arr if present, else -1
def binary_search(arr, low, high, x):
# Check base case
if high >= low:
mid = (high + low) // 2
# If element is present at the middle itself
if arr[mid] == x:
return mid
# If element is smaller than mid, then it can only
# be present in left subarray
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
# Else the element can only be present in right subarray
else:
return binary_search(arr, mid + 1, high, x)
else:
# Element is not present in the array
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
Program.12
# python 3 code to print inverted star
# pattern
# n is the number of rows in which
# star is going to be printed.
n=11
# i is going to be enabled to
# range between n-i t 0 with a
# decrement of 1 with each iteration.
# and in print function, for each iteration,
# ” ” is multiplied with n-i and ‘*’ is
# multiplied with i to create correct
# space before of the stars.
for i in range (n, 0, -1):
print((n-i) * ' ' + i * '*')
Program.13
# Function to find all close matches of
# input string in given list of possible strings
from difflib import get_close_matches
def closeMatches(patterns, word):
print(get_close_matches(word, patterns))
# Driver program
if __name__ == "__main__":
word = 'appel'
patterns = ['ape', 'apple', 'peach', 'puppy']
closeMatches(patterns, word)
Program.14
# Python program to find minimum
# sum of product of number
# To find minimum sum of
# product of number
def find_min_sum(num):
min_sum = num
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
factor = num // i
min_sum = min(min_sum, i + factor)
return min_sum
# driver code
number = 16
# Call the function and print the result
result = find_min_sum(number)
print("The minimum sum of factors for", number, "is", result)
# This code is contributed by AYUSH MILAN
Program.15
# Python program to find X-th triangular
# matchstick number
def numberOfSticks(x):
return (3 * x * (x + 1)) / 2
# main()
print(int(numberOfSticks(7)))