Integration Formulas
Integration Formulas
Fibinocci series
sir
x=input("enter number")
i=1
a=0
b=1
print a
print b
while i<x:
c=a+b
print c
a=b
b=c
i=i+1
Palindrome
s=raw_input("enter string")
a=len(s)
if s==s[::-1]:
print "given string is a palindrome"
else:
print "given string is not a palindrome"
“or”
s=raw_input("enter string")
s1=""
a=len(s)-1
while a>=0:
s1=s1+s[a]
a=a-1
if s==s1:
print "palindrome"
else:
print "not palindrome"
j=j+1
if i==s:
print 'perfect no',s
i=i+1
Leap year
a=input("enter number")
if a%400==0:
print "it is a leap year"
elif a%4==0 and a%100!=0:
print "it is a leap year"
else:
print "it is not leap year"
Print table
x=input("enter 1st number")
y=input("enter number upto multiples you want")
z=input("enter which multiple u want")
while x<=y:
a=x*z
x=x+1
print a
Ask the user to enter any 2 strings and print starting letters of 1st and
2nd string
(Hai,Hello)-- HH
(IIIT,RGUKT)---IR
(hELLO,)--h
(,Jai)--J
s1=raw_input("enter string")
s2=raw_input("enter second string")
print s1[0]+s2[0]
s=raw_input("enter string")
for i in range(0,len(s),1):
print i,s[i]
n=input("enter number")
s=0
while n!=0:
r=n%10
s=s+r
n=n/10
print s
a=a-1
print "factorial of the given number is:", f
4. Take 3 integers from the keyboard or console and print their sum If any of the
number is teen (example 13, 14, 16, 17, 18, 19) then that value counts as 0.
Example
(1, 2, 3)-->6
(2, 14, 11)-->13
(7, 15, 5)-->27
(5, 3, 19)-->8
a=input("enter number")
b=input("enter second number")
c=input("enter third number")
print a+b+c
or
a=input("enter number")
b=input("enter second number")
c=input("enter third number")
if (13<=a<=19 and a!=15) and (13<=b<=19 and b!=15) and (13<=c<=19 and c!=15):
print "0"
elif (13<=a<=19 and a!=15) and (13<=b<=19 and b!=15):
print c
elif (13<=b<=19 and b!=15) and (13<=c<=19 and c!=15):
print a
ans..
a=input("enter number")
b=input("enter second number")
c=input("enter third number")
if a=="sunday":
print "It is all funnnnnnn!!!!!"
elif a=="saturday":
b=raw_input("is it morning or evening ")
if b=="morning":
print "Today is for exam"
elif b=="evening":
print "Yahoo!!! it is time for movie"
else:
print "classes will go on according to periods"
8. Generally IIIT-N students will go for outing in Sundays. If outing is NO then
print “Today no outing try next time!!!!!" If outing is YES then check the gander
(girl or boy). If boy then print "You can go for outing" if it girl then check for
parents. If parents are there with girl then print "Yahoo!! I can go for outing". If
parents are not there then print "You are not allowed to go for outing without
parents".
ans..
a=raw_input("are you want outing say 'yes' or 'no'\n")
if a=="yes":
b=raw_input ("are u a boy or girl\n")
if b=="boy":
print "you can go for outing\n"
else:
c=raw_input("do you have parents along with you say yes or no.\n")
if c=="yes":
print "Yahoo!I can go for outing\n"
else:
print "You are not allowed for outing without parents"
else:
print "Today is no outing try next time"
10. Write a program to check the entered year is leap year or not. Generally we say
leap year, if the year is divisible by 4 for normal years like 1976, 1788, 2012 etc. If
the years are like centuries 300,400,1300,1500,600,1800 etc then it should be
divisible by 400 then only we call the year as leap year.
ans...
a=input("enter integer")
if a%400==0:
print "it is a leap year"
elif a%4==0 and a%100!=0:
print "it is a leap year"
else:
print "it is not a leap year"
or
x=input(“enter a number”)
if x%4==0:
if x%100==0:
if x%400==0:
print “it is a leap year”
else:
print “it is not a leap year”
else:
print “it is a leap year”
else:
print “it is not a leap year”
Given a string of even length, return the first half. So the string
"WooHoo" yields "Woo".
first_half('WooHoo') ? 'Woo'
first_half('HelloThere') ? 'Hello'
first_half('abcdef') ? 'abc'
ans...
s=raw_input("enter string")
a=len(s)
if a%2==0:
print s[ :a/2]
else:m
print "string is odd length"
Given a string, return a version without the first and last char, so "Hello"
yields "ell". The string lengthwill be at least 2.
without_end('Hello') ? 'ell'
without_end('java') ? 'av'
without_end('coding') ? 'odin'
ans..
s=raw_input("enter string")
print s[1:-1]
ans...
x=input("enter no upto prime numbers u want")
i=1
c=0
while i<=x:
j=1
s=0
while j<=i:
if i%j==0:
s=s+1
j=j+1
if s==2:
print 'p no',i
i=i+1
s=raw_input("enter string")
a=len(s)
print (s[(a/2)-1])+s[a/2]+(s[(a/2)+1])
Strong number---145
ans...
import math
n=input("enter number")
a=n
s=0
while n!=0:
r=n%10
s=s+math.factorial(r)
n=n/10
if a==s:
print "it is a strong number "
else:
print "it is not a strong number"
1.. Write a program to print all the elements in the list L=[3,5,2,18,10,7]
ans..
l=[1,5,3,6,7]
i=0
while i<len(l):
print l[i]
i=i+1
l=[]
n=input("enter length of the list\n")
for i in range(n):
x=input("enter values of list:")
l.append(x)
print l
l.reverse()
print l
3. Write a program to print sums and multiplies (respectively) all the numbers in a list of numbers.
For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.
ans...
l=[]
n=input("enter length of the list\n")
s=0
m=1
for i in range(n):
x=input("enter values of list:")
l.append(x)
s=s+x
m=m*x
print "sum of elements in the list is:",s
4. Write a program to print all the element in the list that are greater than 10
ans....
l=[4,9,12,45,76,10]
i=0
while i<len(l):
m=l[i]
if m>10:
print m
i=i+1
5. Write a program to print all the element in the list that are even (HINT: if numList[x]%2 ==
0)
ans...
l=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
i=i+1
print l
i=0
while i<n:
if l[i]%2==0:
print l[i]
i=i+1
6. Write a program to print sum of the numbers in the list that are odd (HINT: if numList[x]%2
!= 0)
ans..
l=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
i=i+1
print l
i=0
s=0
while i<n:
if l[i]%2!=0:
print "odd no in the list is:",l[i]
s=s+l[i]
i=i+1
print s
7. Write a program to find the Maximum element in the list (don’t use max())
ans....
l=[1,4,2,5,7,45]
m=l[0]
for i in range(1,len(l)):
if m<l[i]:
m=l[i]
print "max value is:",m
8. Write a program to find the Minimum element in the list (don’t use min())
ans....
l=[1,4,2,5,7,6]
m=l[0]
for i in range(1,len(l)):
if m>l[i]:
m=l[i]
print "max value is:",m
ans...
l=[]
print s/n
10. Write a program that computes the length of a given list (without using len() function)
ans....
l=[3,5,2,7)]
c=0
for i in l:
c=c+1
print "len of the string:",l
11. Write a program that takes a value (i.e. a number, string, etc) x and a list of values a, and
returns True if x is a member of a, False otherwise.
ans....
l=[2,4,3,6,7]
x=input(“enter number”)
if x in l:
print “True”
else:
print “False”
12. Write a program that takes two lists and returns True if they have at least one member in
common, False otherwise
ans..
l=[3,5,2,7]
l1=[1,4,6,7]
for i in range(len(l)):
for j in range(len(l1)):
if l[i]==l1[j]:
print "True"
else:
print "false"
or
l=[3,5,2,7]
l1=[1,4,6,7]
for i in range(len(l)):
if l[i] in l1:
print “True”
13. Write a program that takes a list of integers and prints a histogram to the screen. For example,
given list is [4, 9, 7]) should print the following:
****
*********
*******
ans...
l=[2,5,4]
for i in l:
print "*"*i
14. Take a list of words and create another list of integers representing the lengths of the
correponding words.
ans.....
l=["abc","saikum","kumar kollati","python","program"]
b=[]
for i in l:
b.append(len(i))
print b
15. Write a program that takes a list of words and returns the length of the longest one.
ans....
a=["abc","saik","sai kumar","python","program"]
b=[]
for i in a:
b.append(len(i))
print b
m=b[0]
for j in range(len(b)):
if m<b[j]:
m=b[j]
x=j
print m
print a[x]
16. Write a function that takes a list of words and an integer n and returns the list of words that are
longer than n.
ans....
l=["abc","saikum","kumar kollati","python","program"]
x=input("enter number")
b=[]
for i in l:
if x<len(i):
b.append(i)
print b
17. Given an list length 1 or more of ints, return the difference between the largest and smallest
values in the array(Don’t use the built-in min(v1, v2) and max(v1, v2) functions).
[10, 3, 5, 6] → 7
[7, 2, 10, 9] → 8
[2, 10, 7, 2] → 8
ans...
l=[1,6,4,3,23,9]
m=l[0]
for i in range(1,len(l)):
if m<l[i]:
m=l[i]
print "max value is:",m
c=l[0]
for i in range(1,len(l)):
if c>l[i]:
c=l[i]
print "max value is:",c
print "max-min:",m-c
18. Return the sum of the numbers in the array, returning 0 for an empty array. Except the number
13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do
not count.
[1, 2, 2, 1] → 6
[1, 1] → 2
[1, 2, 2, 1, 13] → 6
ans....
x=[5,3,13,5,6,7,14,13]
s=0
for in in range(len(x)-1):
if x[i]==13:
x[i]=0
x[i+1]=0
s=s+x[i]
print s
19. Given an array of ints, return True if the array contains a 2 next to a 2 somewhere.
[1, 2, 2] → True
[1, 2, 1, 2] → False
[2, 1, 2] → False
ans...
x=[5,3,2,2,2,7,14,13]
k=0
for i in range(len(x)):
if x[i]==2 and x[i+1]==2:
k=1
if k==1:
print "True"
else:
print "False"
l.sort
print l
l=[2,4,5,5,3,9,2]
x=[]
for i in range(len(l)):
if l[i] not in x:
x.append(l[i])
print x
l=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
i=i+1
print "the list is:",l
for i in range(0,len(l),2):
print l[i]
l=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
i=i+1
print "the list is:",l
for i in range(1,len(l),2):
print l[i]
Sorting of list
l=[]
l1=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
i=i+1
print l
i=0
while i<len(l):
l1.append(min(l))
l.remove(min(l))
print l1
l=[1,9,2,5,8]
a=[]
for i in range(len(l)):
x=max(l)
a.append(x)
l.remove(x)
print a
l=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
l.sort()
i=i+1
#print "the list is:",l
print "the sorted list is:",l
ii..
l=[]
n=input("enter length of the list\n")
for x in range(n):
x=input("enter values of list:")
l.append(x)
l.sort()
print l
l=[]
n=input("enter length of the list\n")
for x in range(n):
x=input("enter values of list:")
l.append(x)
l.sort()
print l
a=l
max=a[0]
for i in range(1,len(a)):
if max <a[i]:
max=a[i]
for i in range(len(l)):
for j in range(len(l)-1):
if (l[j] > l[j+1]):
t=l[j]
l[j]=l[j+1]
l[j+1]=t
print l
l=[11,3,44,6,8]
i=0
a=[]
while i<len(l):
a.append(min(l))
l.remove(min(l))
print a
Creating a tuple
#creating a tuple
t=()
for i in range(x):
a=input("enter the element of the tuple")
t=t+(a,)
print t
ans...
import math
t=()
t=t+(a,)
print t
s=0
k=0
for i in t:
s=s+i
k=k+i**2
m=(k-((s**2)/x))/(x-1)
print math.sqrt(m)
for i in t:
c=c+1
print "len of the tuple:",c
3..Creating a tuple of strings and then sort them in a tuple and find the
following.
Ans...
t=('ab','cd','de','ba','aj')
l=list(t)
l=sorted(l)
t=tuple(l)
print t
4..ask n numbers from the user and store them in a tuple and find the following
1.sum of the tuple.
2.Maximum number in the tuple.
3.Minimum number in the tuple.
4.Average of the tuple.
note:Give the menu as 1 for sum, 2 for Maximum , 3 for Minimum , 4 for Average.
Ans......
t=()
t=t+(a,)
print t
c=input("enter no if 1=sum,2=max,3=min,4=avg")
m=t[0]
k=0
l=t[0]
for i in range(len(t)):
if c==1:
s=s+t[i]
k=1
if c==2:
if m<t[i]:
m=t[i]
k=2
if c==3:
if l>t[i]:
l=t[i]
k=3
if c==4:
s=s+t[i]
k=4
if k==1:
print s
elif k==2:
print m
elif k==3:
print l
elif k==4:
print "avg",s/x
Q..Write a programme to print all the vowels in the given string
x=raw_input("enter string")
i=0
c=0
while i<len(x):
if x[i]=='a' or x[i]=='e' or x[i]=='i' or x[i]=='o' or x[i]=='u':
c=c+1
print i
i=i+1
print c
x=raw_input("enter string")
i=0
c=0
while i<len(x):
if x[i]!='a' and x[i]!='e' and x[i]!='i' and x[i]!='o' and x[i]!='u':
c=c+1
i=i+1
print c
Dictionary
has_key
d={1:'sunday',2:'monday',3:'tuesday',4:'wednesday',5:'thursday',6:'friday',7:'saturday'}
print d.has_key(4)
x=input("enter the key value")
if d.has_key(x):
print d[x]
update a dictionary
d={1:'sai',2:'bh',3:'c'}
d1={4:'d',5:'g',6:'y'}
d.update(d1)
print d
print d
for i in range(x):
k=input("enter key value to the dict")
v=raw_input("enter the value")
d[k]=v
print d
Creating a dictionary
d={}
for i in range(x):
k=input("enter key value to the dict")
v=raw_input("enter the value")
d[k]=v
print d
Get(key,value)
d={1:'sunday',2:'monday',3:'tuesday',4:'wednesday',5:'thursday',6:'friday',7:'saturday'}
print d.get(5)
pop in dictionary
d={1:'sunday',2:'monday',3:'tuesday',4:'wednesday',5:'thursday',6:'friday',7:'saturday'}
d.popitem()
print d
d={1:'a',2:'e',3:'i',4:'o',5:'u'}
k=0
for i in d:
if d[i]=='i' or d.get(i)=='i':
k=1
if k==1:
print "true"
else:
print "False"
or
d={1:'a',2:'e',3:'i',4:'o',5:'u'}
k=0
for i in range(len(d)+1):
if d.get(i)=='u':
k=1
if k==1:
print "true"
else:
print "False"
frequency in dictionary
ans...
for i in range(x):
a=input("enter the value to the list")
l.append(a)
print l
d={}
for i in l:
if i not in d:
d[i]=1
else:
d[i]=d[i]+1
print d
for i in range(x):
a=input("enter the value to the list")
l.append(a)
print l
d={}
for i in l:
if i not in d:
d[i]=1
else:
d[i]=d[i]+1
print d
key=d.keys()
values=d.values()
print "keys\t",key
print "values\t",values
m=values[0]
ls=[]
for j in d:
if m<d[j]:
m=d[j]
for j in d:
if m==d[j]:
ls.append(j)
print "mode of the list",ls
a=[1,2,3]
b=[4,7,6]
c=[]
if len(a)==3 and len(b)==3:
c.append(a[len(a)/2])
c.append(b[len(b)/2])
print c
ans,,,...
l=[]
n=input("enter how many no in list\n")
i=0
while i<n:
x=input("enter value to the list")
l.append(x)
i=i+1
print "the list is:",l
if 2 in l or 3 in l:
print "True"
else:
print "False"
mid 3
5)L.insert(i,x)
b) Write a program to find the max element in the list .(With out
using max())
solu)
k=[1,2,34,5,6,67,23]
m=k[0]
for i in range(len(k)):
if m<k[i]:
m=k[i]
print m
3)Write a program to find the following from the tuple
a=(3,9,3,7,25,18) and b=(2,25,3,16,18,7,9)
solu)
#A U B
a=(3,9,3,7,25,18)
b=(2,25,3,16,18,7,9)
c=a+b
d=()
for i in c:
if i not in d:
d=d+(i,)
print d
# A intersection B
a=(3,9,3,7,25,18)
b=(2,25,3,16,18,7,9)
m=()
for j in a:
if j in b:
if j not in m:
m=m+(j,)
print m
4) Write a Program to find the mode of given list
solu)
#mode of the list
l=[]
for i in range(x):
a=input("enter the value to the list")
l.append(a)
print l
d={}
for i in l:
if i not in d:
d[i]=1
else:
d[i]=d[i]+1
print d
key=d.keys()
values=d.values()
print "keys\t",key
print "values\t",values
m=values[0]
ls=[]
for j in d:
if m<d[j]:
m=d[j]
for j in d:
if m==d[j]:
ls.append(j)
def add():
a=input("enter a no")
b=input("enter a no")
c=a+b
print c
add()
calculator operations
def calculator(a,b):
def add(l,m):
n=l+m
print "addition",n
add(a,b)
def sub(l,m):
n=l-m
print "subtraction",n
sub(a,b)
def mul(l,m):
n=l*m
print "multiplication",n
mul(a,b)
def nine_line():
def three_lines():
def new_line():
print "new line"
for i in range(1,4):
new_line()
for i in range(1,4):
three_lines()
nine_line()
another
def nine_line():
def three_lines():
print "new line"
for i in range(1,4):
three_lines()
for i in range(1,4):
nine_line()
def even(a):
for i in range(a):
if i%2==0:
print "even numbers",i
even(a)
a=input("enter a no")
prime(a)
def even(n):
if n%2==0:
print True
else:
print False
a=input("enter a number")
even(a)
name and no
def name(s):
print s
s=raw_input("enter ur name")
name(s)
x=input("enter a no")
name(x)
sum of numbers upto given no
def sum(n):
s=0
for i in range(n+1):
s=s+i
return s
a=input("enter a number")
print sum(a)
1. Define a function too_max() that takes two numbers as arguments and returns
the largest of them.
def too_max(x,y):
if x>y:
return "x is greatest",x
else:
return "y is greatest",y
x=input("enter a no")
y=input("enter a second no")
print too_max(x,y)
def greater(x,y,z):
if x>y and x>z:
print x
if y>x and y>z:
print y
if z>x and z>y:
print z
else:
print "all are equal"
x=input("enter a number")
y=input("enter second number")
z=input("enter third number")
greater(x,y,z)
def fac(a):
f=1
while a!=0:
f=f*a
a=a-1
return f
a=input("enter a number")
print fac(a)
4. Define a function vowel() that takes a character (i.e. a string of length 1) and
returns True if it is a vowel, False otherwise.
def vowel(s):
if s in ['a','e','i','o','u']:
print True
else:
print False
s=raw_input("enter a string")
vowel(s)
def rev(s):
s1=""
a=len(s)-1
while a>=0:
s1=s1+s[a]
a=a-1
return "reverse string is:",s1
s=raw_input("enter string")
print rev(s)
n=input("enter number")
a=n
s=0
while n!==0:
r=n%10
s=s*10+r
n=n/10
if a==s:
print "it is a palindrome"
else:
print "it is not a palindrome"
def is_palindrome(s):
s1=""
a=len(s)-1
while a>=0:
s1=s1+s[a]
a=a-1
if s1==s:
return True
else:
return False
s=raw_input("enter string")
print is_palindrome(s)
def primenum(a):
c=0
for i in range(1,a+1):
if a%i==0:
c=c+1
if c==2:
print "given number is prime number"
else:
print "not a prime"
a=input("enter number")
primenum(a)
def perfectnum(a):
c=0
for i in range(1,a):
if a%i==0:
c=c+i
if c==a:
print "given number is perfect number"
else:
print "not a perfect"
a=input("enter number")
perfectnum(a)
10. Define a function add() and a function multiply() that sums and multiplies
(respectively) all the numbers in a list of numbers. For example, add([1, 2, 3,
4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.
def list(l):
print l
def add(l):
s=0
for i in l:
s=s+i
print s
add(l)
def mul(l):
m=1
for i in l:
m=m*i
print m
mul(l)
def longest_word(l):
b=[]
for i in l:
b.append(len(i))
m=b[0]
x=0
for j in range(1,len(b)):
if m<b[j]:
m=b[j]
x=j
print l[x]
strong number-145
import math
n=input("enter number")
a=n
s=0
while n!=0:
r=n%10
s=s+math.factorial(r)
n=n/10
if a==s:
print "it is a strong number "
else:
print "it is not a strong number"
amstrong number-153
n=input("enter number")
a=n
s=0
while n!=0:
r=n%10
s=s+r**3
n=n/10
if a==s:
print "it is amstrong number"
else:
print "it is not a amstrong no"
n=input("enter number")
for j in range(1,n+1):
a=j
s=0
while a!=0:
r=a%10
s=s+r**3
a=a/10
if j==s:
print s
n=input("enter number")
for j in range(1,n+1):
a=j
s=0
while a!=0:
r=a%10
s=s*10 +r
a=a/10
if j==s:
print s
multiples of 3,5,7
x=input("enter number")
i=1
while i<x:
if i%3==0:
if i%5==0:
if i%7==0:
print "multiple of 3,5,7",i
elif i%5==0:
if i%7==0:
if i%3==0:
print "multiple of 3,5,7",i
elif i%7==0:
if i%3==0:
if i%5==0:
print "multiple of 3,5,7",i
i=i+1
LC M of given numbers
a=input("enter number")
b=input("enter second number")
for i in range(1,a*b,1):
if i%a==0 and i%b==0:
print i
break
a=input("enter number")
b=input("enter second number")
c=[]
for i in range(1,a*b+1):
if a%i==0 and b%i==0:
c.append(i)
print max(c)