Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

Q:-1Write a program to input a number and print its cube.

num=float(input('Enter a number:'))
num_cube=num*num*num
print('The cube of',num,'is',num_cube)

OUTPUT:-1
Q:-2Write a program to input a number and print its square
root.

num=float(input('Enter a number:'))
num_sqrt=num**0.5
print('The square root of',num,'is',num_sqrt)

OUTPUT:-2
Q:-3Write a program that inputs an integer in range 0-999 and
then prints if the integer entered is a 1/2/3.

num=int(input("Enter a number(0..999):"))
if num<0:
print("Invalid entry.Valid range is 0to999.")
elif num<10:
print("Single digit number is entered")
elif num<100:
print("Two digit number is entered")
elif num<999:
print("Three digit number is entered")
else:
print("Invalid entry.Valid range is 0to999.")

OUTPUT:-3
Q:-4Write a program that inputs an integer in range 0-999 and
then prints if the integer entered is a 1/2/3.Use nested if
statement..

num=int(input("Enter a number(0..999):"))
if num<0 or num>999:
print("Invalid entry.Valid range is 0to999.")
else:
if num<10:
print("Single digit number is entered")
else:
if num<100:
print("Two digit number is entered")
else:
print("Three digit number is entered")
OUTPUT:-4
Q:-5Write a program to print cubes of numbers in the range
15 to 18.

for i in range(15,18):
print("Cube of number",i,end=' ')
print("is",i**3)

OUTPUT:-5
Q:-6Write a program to print square root of every alternate
number in the range 1 to 18.

for i in range(1,18,2):
print("Sq. root of",i,":",(i**0.5))

OUTPUT:-6
Q:-7Write a program that multiples two integer numbers
without using the * operator, using repeated addition.

n1=int(input("Enter first number:"))


n2=int(input("Enter second number:"))
product=0
count=n1
while count>0:
count=count-1
product=product+n2
print("The product of",n1,"and",n2,"is",product)

OUTPUT:-7
Q:-8Program that reads a line and prints its statics like:
Number of uppercase letters:
Number of lowercase letters:
Number of alphabets:
Number of digits:

line=input("Enter a line:")
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
if a.isalpha():
alphacount+=1
print("Numbers of uppercase letters:",uppercount)
print("Numbers of lowercase letters:",lowercount)
print("Numbers of alphabets :",alphacount)
print("Numbers of digits:",digitcount)

OUTPUT:-8
Q:-9Write a program to create a dictionary containing names
of competition winner students as keys and number of their
wins as values.

n=int(input("How many students ?"))


CompWinners={}
for a in range(n):
key=input("Name of the student:")
value=int(input("Number of competitions won:"))
CompWinners[key]=value
print("The dictionary now is:")
print(CompWinners)
OUTPUT:-9
Q:-10Given three lists as list1= ['a', 'b', 'c'], list2= ['h', 'i','t'] and
list3 ['0','1','2'].Write a program that adds list2 and list3 to
list1 as single element each. The resultant list should be in the
order of list3,element of list1,list2.

list1=['a','b','c']
list2=['h','i','t']
list3=['0','1','2']
print("Orignally:")
print("List1=",list1)
print("List2=",list2)
print("List3=",list3)

#adding list2 as single element at the end of list1


list1.append(list2) #list2 gets added as one element at the
end of list1

#adding list3 as single element at the start of list1


list1.insert(0,list3) #list3 gets added as one element at the
begginning of list1
print("After adding two lists as individual elements,list now is:")
print(list1)
OUTPUT:-10
Q:-11Given three lists as list1=['a','b','c'],list2=['h','i','t']and
list3['0','1','2'].Write a program that adds individual elements
of lists 2 and 3 to list1. The resultant list should be in the order
of elements of list3, elements of list1, elements of list2.

list1=['a','b','c']
list2=['h','i','t']
list3=['0','1','2']
print("Orignally:")
print("List1=",list1)
print("List2=",list2)
print("List3=",list3)

#adding elements of list1 at the end of list3


list3.extend(list1)

#adding elements of list2 at the end of list3


list3.extend(list2)
print("After adding elements of two lists individually, list now
is:")
print(list3)
OUTPUT:-11
Q:-12Write a program that finds an element's index/position
in a tuple WITHOUT using index().

tuple1=('a','p','p','l','e')
char=input("Enter a single letter without quotes:")
if char in tuple1:
count=0
for a in tuple1:
if a!=char:
count+=1
else:
break
print(char,"is at index",count,"in",tuple1)
else:
print(char,"is NOT in",tuple1)
OUTPUT:-12
Q:-13Program to sort a list using Bubble sort.

aList=[15,6,13,22,3,52,2]
print("Original list is:",aList)
n=len(aList)
#Traverse through all list elements
for i in range(n):
#Last i elements are already in place
for j in range(0,n-i-1):
#traverse the list from 0 to n-i-1
#Swap if the element foundd is greater
#than the next element
if aList[j]>aList[j+1]:
aList[j],aList[j+1]=aList[j+1],aList[j]
print("List after sorting:",aList)
OUTPUT:-13
Q:-14Program to sort a sequence using insertion sort.

aList=[15,6,13,22,3,52,2]
print("Original list is:",aList)
for i in range(1,len(aList)):
key=aList[i]
j=i-1
while j>=0 and key <aList[j]:
aList[j+1]=aList[j] #shift elements to right to make room
for key
j=j-1
else:
aList[j+1]=key
print("List after sorting:",aList)
OUTPUT:-14
Q:-15Program to add two numbers.

#program add.py to add two numbers through a function


Def calcSum(x,y):
S=x+y #statement1
Return S #statement2

num1=float(input(“Enter first number:”))


num=2float(input(“Enter second number:”))
Sum=calcSum(num1,num2)
Print(“Sum of two given number is:”Sum)

OUTPUT:-15
Q:-16Program that receives two numbers in a function and
returns the results of all arithmetic operations (+,-,*,/,%) on
these numbers.

def arCalc(x,y):
return x+y,x-y,x*y,x/y,x%y

#_main_
num1=int(input("Enter a number 1:"))
num2=int(input("Enter a number 2:"))
add,sub,mult,div,mod=arCalc(num1,num2)
print("Sum of given numbers:",add)
print("Subtraction of given numbers:",sub)
print("Multiplication of given numbers:",mult)
print("Division of given numbers:",div)
print("Modulo of given numbers:",mod)
OUTPUT:-16
Q:-17Program to calculate simple interest using a function
interest() that can receive principal amount,time and rate and
returns calculated simple interest.Do specify default values
for rate and time as 10% and 2 years respectively.

def interest(principal,time=2,rate=0.10):
return principal*rate*time

#_main_
prin=float(input("Enter principal amount:"))
print("Simple interest with default ROI and time values is:")
si1=interest(prin)
print("Rs.",si1)
roi=float(input("Enter rate of interest(ROI):"))
time=int(input("Enter time in years:"))
print("Simple interest with your provided ROI and time values
is:")
si2=interest(prin,time,roi/100)
print("Rs.",si2)

OUTPUT:-17
Q:-18Insertion in sorted array using bisect module.
import bisect
myList=[10,20,30,40,50,60,70]
print("The list in sorted order is:")
print(myList)
ITEM=int(input("Enter new element to be inserted:"))
ind=bisect.bisect(myList,ITEM)
bisect.insort (myList,ITEM)
print(ITEM,"inserted at index",ind)
print("The list after inserting new element is")
print(myList)

OUTPUT:-18

Q:-19Traversing a linear list.


def traverse(AR):
size=len(AR)
for i in range(size):
print(AR[i],end=" ")
#_main_
size=int(input("Enter the size of linear list to be input:"))
AR=[None]*size # create an empty list of the given
size
print("Enter elements for the Linear list")
for i in range(size):
AR[i]=int(input("Element"+str(i)+":"))
print("Traversing the list:")
traverse(AR)

OUTPUT:-19
Q:-20Linear searching in an array(Linear list).
#linear search
def Lsearch(AR,ITEM):
i=0
while i<len(AR) and AR[i]!=ITEM:
i+=1
if i<len(AR):
return i
else:
return False
#----main----
N=int(input("Enter desired linear-list size(max.50)..."))
print("\nEnter elements for Linear List\n")
AR=[0]*N #intialize list of sizw N with zeroes
for i in range(N):
AR[i]=int(input("Element"+str(i)+":"))
ITEM=int(input("\nEnter Element to be searched ffor..."))
index=Lsearch(AR,ITEM)
if index:
print("\nElement found at index:",index,",Position:",
(index+1))
else:
print("\nSorry!!Given element could not be found.\n")

OUTPUT:-20

You might also like