Div Python
Div Python
def add(num1,num2):
return num1+num2
def sub(num1,num2):
if num2>num1:
return num2-num1
else:
return num1-num2
def multi(num1,num2):
return num1*num2
def div(num1,num2):
if num2>num1:
return num2/num1
else:
return num1/num2
def mod(num1,num2):
if num2>num1:
return num2%num1
else:
return num1%num2
while True:
num1=int(input("Enter fist number="))
num2=int(input("Enter second number="))
print("Press 1 for addition\nPress 2 for subtraction\nPress
3 for multiplication\nPress 4 for division\nPress 5 for
reminder")
num=int(input("Enter a number to do task\n"))
if num==1:
print("Addition of",num1,"and",num2,"is",add(num1,num2))
elif num==2:
print("Subtraction of",num1,"and",num2,"is",sub(num1,num2))
elif num==3:
print("Multiplication of",num1,"and",num2,"is",multi(num1,num2))
elif num==4:
print("Division of",num1,"and",num2,"is",div(num1,num2))
elif num==5:
print("Reminder=",mod(num1,num2))
else:
print("Only press 1 to 5")
print("Do you want to continue?\nPress 1 to continue\nPress any key to break")
ch=int(input("Enter your choice:-"))
if ch==1:
continue
else:
break
3. Program to check whether even
or odd if less then 100
num=int(input("Enter a number to check whether
a number is even or odd\nThe number should
be less then 100\nEnter a number="))
if num<=100:
if num%2==0:
print("The given number",num,"is even")
else:
print("The given number",num,"is odd")
else:
print("The given number is out of range\nPlease
enter number between 1 to 100 only")
4. Program to check whether upper,
lower, digit or special character
print("Press any key to check whether it is
lowercase,uppercase,digit or special character")
ch=input("Enter the character:")
if ch.islower():
print("The given character",ch,"is a lowercase")
elif ch.isupper():
print("The given character",ch,"is a uppercase")
elif ch.isdigit():
print("The given character",ch,"is a digit")
else:
print("The given character",ch,"is a special character")
5. WAP to multiply without using * operator
lst1=[]
lst2=[]
elements=int(input("Enter number of elements you want in
list="))
for i in range(elements):
num=int(input("Enter element of list="))
lst1.append(num)
print("Original list is given below:\n",lst1)
for j in lst1:
lst2.append(j*2)
print("The list which contain twice of each element in list is:\
n",lst2)
8. Program to print table of 5 and if
value>10 then break the program
num=int(input("Enter element to
print table="))
for i in range(1,11):
if i*num>10:
break
print(num,"X",i,"=",num*i)
9. Program to find prime number
within a range
lowernum=int(input("Enter number to start:-"))
uppernum=int(input("Enter number to stop:-"))
print("Prime numbers
between",lowernum,"and",uppernum,"are:")
for num in range(lowernum,uppernum+1):
#all prime numbers are greater then 1
if num>1:
for i in range(2,num):
if num%i==0:
break
else:
print(num)
10. Program to print fibbonacci
series
nterms=int(input("How many terms?\n"))
#First two numbers
n1,n2=0,1
count=0
#check if the number of term is valid
if nterms<=0:
print("Please enter positive integer")
#If there is only one term,return n1
elif nterms==1:
print("Fibonacci sequence upto",nterms,":",n1)
#Generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth=n1+n2
#Update values
n1=n2
n2=nth
count+=1
11. Program to find smallest and largest
number in list
lst=[]
elements=int(input("Enter element of list="))
for i in range(elements):
element=input("Enter list value=")
lst.append(element)
print("Largest value of list is:",max(lst))
print("Smallest value of list is:",min(lst))
12.Program to check whether
palindrome or not
st=input("Enter a string:\n")
i,j=0,len(st)-1
is_palindrome=True #assume palindrome
while i<j:
if st[i]!=st[j]: #mismatch
is_palindrome=False
break
i+=1
j-=1
if is_palindrome:
print("Yes")
else:
print("No")
13. Program to find second highest number
in list
lst=[]
need=int(input("Enter the element you want in
list:-"))
for i in range(need):
elements=int(input("Enter the element of
list="))
lst.append(elements)
lst.sort()
print("Second largest number is:",lst[need-2])
14. Program to reverse a list
lst=[]
need=int(input("Enter the element you want in
list:-"))
for i in range(need):
elements=int(input("Enter the element of list="))
lst.append(elements)
print("Original list:\n",lst)
lst.reverse()
print("Reverse list:\n",lst)
15. Program to convert tuple into list
and vica versa
lst=[] #Empty list
tup=eval(input("Enter tuple:"))
need=int(input("Enter the element you want in
list:-")) #Value needed in list
for i in range(need):
elements=int(input("Enter the element of
list="))
lst.append(elements) #Append element in list
print("Original list:\n",lst)
print("Original tuple:\n",tup)
lst2=tuple(lst)
tup2=list(tup)
print("After covertion list in tuple and tuple in
list they are:")
print("List converted into tuple:",lst2)
print("Tuple converted into list:",tup2)
16. Program to concatinate 2 tuple
num1=int(input("Enter a
number to find factorial="))
fact=1
if num1<0:
print("Sorry! Factorial does
not exist for negative
number")
elif num1==0:
print("Factorial of 0 is 1")
else:
for i in range(1,num1+1):
fact=fact*i
print("The factorial
of",num1,"is",fact)
20.Program to print even number
between 2 to n