Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Program:

x=int(input('Enter a integer value:'))

print(x)

print(type(x))

x=float(input('Enter a float value:'))

print(x)

print(type(x))

x=complex(input('Enter a complex value:'))

print(x)

print(type(x))

x=input('Enter a string value:')

print(x)

print(type(x))

x=[]

n=int(input('Enter no.of values for list:'))

for i in range(0,n):

ele=input('Enter a value for list:')

x.append(ele)

print(x)

print(type(x))

x=('one','two','three')

print(x)

print(type(x))

x={}

n=int(input('Enter no.of values for dictionary:'))

for i in range(n):

key=input('Enter a key for dictionary:')

val=input('Enter a value for key:')

x[key]=val

print(x)

print(type(x))
x=bool(input('Enter a boolean value True or False:'))

print(x)

print(type(x))
Program:
def add(n1,n2):

print("Addition: ",n1+n2)

def sub(n1,n2):

print("Subtraction: ", n1 - n2)

def mul(n1,n2):

print("Multiplication: ", n1 * n2)

def div(n1,n2):

print("Division: ", n1 / n2)

def fdiv(n1,n2):

print("Floor Division: ", n1 // n2)

def power(n1,n2):

print("Power: ", n1 ** n2)

def mod(n1,n2):

print("modulos: ", n1 % n2)

num1 = int(input('Enter First number: '))

num2 = int(input('Enter Second number '))

add(num1,num2)

sub(num1,num2)

mul(num1,num2)

div(num1,num2)

fdiv(num1,num2)

power(num1,num2)

mod(num1,num2)
Program:
def concatenate_strings(str1, str2):

return str1 + str2

def access_substring(full_string, start_index, end_index):

return full_string[start_index:end_index]

def main():

first_string =input("Enter first string:")

second_string = input("Enter second string:")

concatenated_string = first_string+second_string

start_index = 7

end_index = 12

substring = access_substring(concatenated_string, start_index, end_index)

print("Concatenated string:", concatenated_string)

print("Substring: ", second_string)

main()
Program:
class check:

def _init_(self):

self.n=[]

def add(self,n):

self.n.append(a)

def remove(self,b):

self.n.remove(b)

def dis(self):

return (self.n)

obj=check()

choice=1

while choice!=0:

print("0. Exit")

print("1. Add")

print("2. Delete")

print("3. Display")

choice=int(input("Enter choice: "))

if choice==1:

n=int(input("Enter number to append: "))

obj.add(n)

print("List: ",obj.dis())

elif choice==2:

n=int(input("Enter number to remove: "))

obj.remove(n)

print("List: ",obj.dis())

elif choice==3:
print("List: ",obj.dis())

elif choice==0:

print("Exiting!")

else:

print("Invalid choice!!")

print()
Program:
T = ("apple", "banana", "cherry","mango","grape","orange")

print("\n Created tuple is :",T)

print("\n Second fruit is :",T[1])

print("\n From 3-6 fruits are :",T[3:6])

print("\n List of all items in Tuple :")

for x in T:

print(x)

if "apple" in T:

print("\n Yes, 'apple' is in the fruits tuple")

print("\n Length of Tuple is :",len(T))


Program:
student_scores = {'John': 85, 'Alice': 92, 'Bob': 78, 'Emma': 95}

alice_score = student_scores['Alice']

print(f"Alice's score: {alice_score}")

student_scores['Bob'] = 80

print(f"Bob's updated score: {student_scores['Bob']}")

student_scores['Charlie'] = 88

print("\nStudent Scores:")

for name, score in student_scores.items():

print(f"{name}: {score}")

if 'Emma' in student_scores:

print("\nEmma's score is recorded.")

else:

print("\nEmma's score is not found.")

del student_scores['John']

print("\nUpdated Student Scores:")

for name, score in student_scores.items():

print(f"{name}: {score}")
Program:
def findLargest(numbers):

if not numbers:

return None

largest = numbers[0]

for num in numbers:

if num > largest:

largest = num

return largest

n = int(input("Enter the number of elements: "))

elements = []

for i in range(n):

element = float(input(f"Enter element {i + 1}: "))

elements.append(element)

result = findLargest(elements)

print("The largest number is:", result)


Program:
def recur_factorial(n):

if n == 1:

return n

else:

return n*recur_factorial(n-1)

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

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

print("The factorial of", num, "is", recur_factorial(num))


Module Program:
def fib(n):

n1, n2 = 0, 1

count = 0

if n <= 0:

print("Please enter a positive integer")

elif n == 1:

print("Fibonacci sequence upto",n,":")

print(n1)

else:

print("Fibonacci sequence:")

while count < n:

print(n1)

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

Main Program:
import ex9mf

x=int(input("How Many Terms: "))

print(ex9mf.fib(x))
Module Program:
def rep(name,regno,dep,s1,s2,s3,s4,s5):

print("Name:",name)

print("Register No:",regno)

print("Department:",dep)

tot=s1+s2+s3+s4+s5

print("Total Mark:",tot)

ave=(s1+s2+s3+s4+s5)/5

print("Average:",ave)

per=(s1+s2+s3+s4+s5)/500*100

print("Percentage:",per)

Main Program:
import ex10mf

name=input("Enter a Student Name:")

regno=input("Enter a Register No:")

dep=input("Enter a Department:")

s1=int(input("Enter mark of subject 1:"))

s2=int(input("Enter mark of subject 2:"))

s3=int(input("Enter mark of subject 3:"))

s4=int(input("Enter mark of subject 4:"))

s5=int(input("Enter mark of subject 5:"))

ex10mf.rep(name,regno,dep,s1,s2,s3,s4,s5)
Program:

input_file_name = input("Enter the name of the input file: ")

output_file_name = input("Enter the name of the output file: ")

try:

with open(input_file_name, 'r') as input_file:

content = input_file.read()

except FileNotFoundError:

print(f"Error: {input_file_name} not found.")

quit()

with open(output_file_name, 'w') as output_file:

output_file.write(content)

print(f"Content from {input_file_name} has been copied to {output_file_name}.")


Program:
fname = input("Enter file name: ")

fh = open(fname)

lst = list()

words=[];

for line in fh:

words += line.split()

words.sort()

print("The unique words in alphabetical order are:")

for word in words:

if word in lst:

continue

else:

lst.append(word)

print(word)
Program:
class py_solution:

def int_to_Roman(self, num):

val = [

1000, 900, 500, 400,

100, 90, 50, 40,

10, 9, 5, 4,

syb = [

"M", "CM", "D", "CD",

"C", "XC", "L", "XL",

"X", "IX", "V", "IV",

"I"

roman_num = ''

i=0

while num > 0:

for _ in range(num // val[i]):

roman_num += syb[i]

num -= val[i]

i += 1

return roman_num

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

print(py_solution().int_to_Roman(num))
Program:
class py_solution:

def pow(self, x, n):

if x==0 or x==1 or n==1:

return x

if x==-1:

if n%2 ==0:

return 1

else:

return -1

if n==0:

return 1

if n<0:

return 1/self.pow(x,-n)

val = self.pow(x,n//2)

if n%2 ==0:

return val*val

return val*val*x

x=int(input("Enter a number 1:"))

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

print(py_solution().pow(x,n));
Program:
def reverseWord(s, start, end):

while start < end:

s[start], s[end] = s[end], s[start]

start = start + 1

end = end - 1

s = input("Enter a String:")

s = list(s)

start = 0

while True:

try:

end = s.index(' ', start)

reverseWord(s, start, end - 1)

start = end + 1

except ValueError:

reverseWord(s, start, len(s) - 1)

break

s.reverse()

s = "".join(s)

print(s)

You might also like