0% found this document useful (0 votes)
74 views40 pages

Integration Formulas

The document contains code snippets and solutions for various Python programming problems including: 1. A Fibonacci series program that prints the Fibonacci sequence up to a given number. 2. Code to check if a string is a palindrome. 3. A program that finds and prints all perfect numbers up to a given limit. 4. Code that checks if a given year is a leap year. 5. Solutions and snippets for additional problems like printing tables, checking strings, prime numbers, middle elements of strings, and more.

Uploaded by

Zoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views40 pages

Integration Formulas

The document contains code snippets and solutions for various Python programming problems including: 1. A Fibonacci series program that prints the Fibonacci sequence up to a given number. 2. Code to check if a string is a palindrome. 3. A program that finds and prints all perfect numbers up to a given limit. 4. Code that checks if a given year is a leap year. 5. Solutions and snippets for additional problems like printing tables, checking strings, prime numbers, middle elements of strings, and more.

Uploaded by

Zoya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Mid 2

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"

perfect numbers upto the given number

x=input("enter no upto perfect numbers u want")


i=1
while i<x:
j=1
s=0
while j<i:
if i%j==0:
s=s+j

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]

Write a program to print the indexes of entered string. For example if


you have entered a string hello
then output should be....
Example-1 :: Hello Example-2 :: Hai
------------------ ----------------
H0|H0
e1|a1
l2|i2
l3|
o4|

s=raw_input("enter string")

for i in range(0,len(s),1):
print i,s[i]

sum of first 50,3 and 5 multiples

#sum of first 50,3 & 5 multiples


n=input('enter how many multiples sum u want')
i=1
c=0
s=0
while c<n:
if i%3==0:
if i%5==0:
print i
c=c+1
s=s+i
i=i+1
print s

sum of the numbers in the given number

n=input("enter number")
s=0
while n!=0:
r=n%10
s=s+r
n=n/10
print s

Factorial of the given number


a=input("enter number you want factorial\n")
f=1
while a>=1:
f=f*a

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")

if 13<=a<=19 and a!=15:


a=0
if 13<=b<=19 and b!=15:
b=0
if 13<=c<=19 and c!=15:
c=0

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

elif (13<=c<=19 and c!=15) and (13<=a<=19 and a!=15):


print b
elif (13<=a<=19 and a!=15):
print b+c
elif (13<=b<=19 and b!=15):
print a+c
elif (13<=c<=19 and c!=15):
print b+a
5. Given 3 int values, a b c, return their sum. However, if one of the values is the
same as another of the values, it does not count towards the sum.
(1, 2, 3)-->6
(3, 2, 3)-->2
(3, 3, 3)-->0

ans..
a=input("enter number")
b=input("enter second number")
c=input("enter third number")

if a!=b and a!=c:


print a+b+c
elif a==b and a==c:
print "0"

elif a==b and a!=c:


print c

elif a==c and a!=b:


print b

elif b==c and b!=a:


print a

7. write a program to check about curriculum of IIIT-N. We have generally 7 days


in these Saturday is for exam and Sunday is a holiday except all days are normal
days, it means classes will go on according to periods. If the entered day is
Saturday then print 'Today is for exam' if the session is morning and print
'Yahoo!!! it is time for movie' if the session is evening. Just print 'It is all
funnnnn!!!!' if the day is Sunday. For Friday then print "This session is for classes"
if the session is morning print "It is time for study hours” if the session is
afternoon. For remaining days just print "Today is for studies" for any time.
ans...
#time table
a=raw_input("enter the day ")

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]

Prime numbers to upto user defined

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

print the middle elements

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

2. Write a program to print all the elements in reverse order. L=[3,5,2,18,10,7]


ans...

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

print "multiples of elements in the list is:",m

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

9. Write a program to print the average of the elements in the list

ans...

l=[]

n=input("enter len of the list")


i=0
s=0
for i in range(n):
x=input("enter value of list")
l.append(x)
s=s+x

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"

20. Sorting the elements in the


l=[2,5,4,6,17,19,21,25,27]
l=[2,4,5,6,17,19,21,25,27]

l.sort

print l

21. Eliminate the duplicate elements from the list


L=[2,5,4,5,8,5,6,9,7] L=[2,5,4,8,6,9,7]
ans...

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

1.Write a program to print the even position elements in the list.

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]

2.Write a program to print the odd position elements in the list.

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

some of the methods to sort a list

1..in ascending order

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

2...in descending order

l=[1,9,2,5,8]

a=[]
for i in range(len(l)):
x=max(l)
a.append(x)
l.remove(x)

print a

3...Creating list and sorting

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

4....max value in the sorted list

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]

print "max vale is:",max

5....Sort bubble method


l=[2,6,4,9,7,8,5]

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

6...sorting using while condition

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=()

x=input("enter the length of the tuple")

for i in range(x):
a=input("enter the element of the tuple")
t=t+(a,)
print t

Deviation in the tuple



elements 2
∑ of the squares of the values− no. of values
**** Standard Deviation S=
no of values−1

ans...

import math
t=()

x=input("enter the length of the tuple")


for i in range(x):
a=input("enter the element of the tuple")

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)

Finding len of the tuple


t=(3,5,2,7)
c=0

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=()

x=input("enter the length of the tuple")


s=0
for i in range(x):
a=input("enter the element of the tuple")

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

Q....Write a program to print the consonants in the given string


ans....

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

creating a dictionary with the given half of the month names


d={1:'jan',2:'feb',3:'mar',4:'april',5:'may',6:'june',7:'july'}

x=input("enter the len of the dictionary")

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={}

x=input("enter the len of the dictionary")

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

If vowel 'I' in dictionary print true

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...

#frequency in the diction


l=[]

x=input("enter len of the list")

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

mode of the list


ans...

#mode of the list


l=[]

x=input("enter len of the list")

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

Q...If the given list has len 3 print middle element

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

Q..print true if the list has numbers 2 or 3

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

1)Write a program to sort the list l=[3,7,26,25,73,45,5,28,19]


solu)
l=[3,7,26,25,73,45,5,28,19]
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 k
OR
l=[3,7,26,25,73,45,5,28,19]
b=[]
import math
for i in range(len(l)):
c=min(l)
b.append(c)
k.remove(c)
print b
2)
a)Write any five in – built functions with examples
1)min(L)
2)max(L)
3)sorted(L)
4)L.pop()

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=[]

x=input("enter len of the list")

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


5) Dictionary
D={'n150001':{'name':'afrin','gender':'female','gp':10},'n150050':
{'name':'ramu','ender':'male','gp':7},'n150060':
{'name':'sofia','gender':'female','gp':10},'n150070':
{'name':'ravi','gender':'male','gp':8},'n150080':
{'name':'kabali','gender':'male','gp':10}}
a)
for i in D:
if D[i]['gp']==10:
print 'id no:',i
print 'name:',D[i]['name']
print 'Gender:',D[i]['gender']
print 'GP:',D[i]['gp']
b)
for i in D:
if i=='n150080':
print 'id no::',i
print 'Name:',D[i]['name']
print 'Gender:',D[i]['gender']
print 'GP:',D[i]['gp']
c)
for i in D:
if D[i]['gender']=='female':
print 'Name:',D[i]['name']

6) Match the following


List == mutable
DICTIONARY==unordered set
Tuple ==immutable
Keys ==unique
Values == mutable or immutable
functions
print a+b

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)

a=input("enter a first no")

b=input("enter second number")


calculator(a,b)
nine lines programme

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()

prime numbers upto user defined

print "'this programme is to print prime,even, odd numbers:'"


def prime(a):
for i in range(1,a):
j=0
c=0
for j in range(1,a):
if i%j==0:
c=c+1
if c==2:
print "prime number",i
def odd(a):
for i in range(a):
if i%2!=0:
print "odd numbers",i
odd(a)

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)

given no is even or not

#find out given number is even or odd

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)

2 Define a function max_of_three() that takes three numbers as arguments and


returns the largest of them.

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)

3. Define a function factorial() to calculate the factorial of a number (non-


negative integer). The function accept the number as a argument.

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)

5. Define a function revstring() that computes the reversal of a string. For


example, revstring("I am testing") should return the string "gnitset ma I".

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)

print given number is palindrome

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"

6. Define a function is_palindrome() that recognizes palindromes (i.e. words


that look the same written backwards). For example,
is_palindrome("radar") should return True.

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)

7. Define a function primenum() that takes a number as a parameter and check


the number is prime or not.

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)

8. Define a function perfect() that takes a number as a parameter and check


whether a number is perfect or not.

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)

9.Define a function even_num() print the even numbers from a given


list,function accept the list as a argument.
def even_num(l):
print l
for i in l:
if i%2==0:
print i

x=input("enter len of the list")


l=[]
for i in range(x):
a=input("enter values to the list")
l.append(a)
even_num(l)

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)

x=input("enter len of the list")


l=[]
for i in range(x):
a=input("enter values to the list")
l.append(a)
list(l)
11. Write a function longest_word() that takes a list of words and returns the
length of the longest one.

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]

x=input("enter len of the list")


l=[]
for i in range(x):
a=raw_input("enter values to the list")
l.append(a)
longest_word(l)

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"

print amstrong numbers upto given numbers

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

print palindrome upto given numbers

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

HCF of given two numbers

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)

You might also like