0% found this document useful (0 votes)
45 views9 pages

Python Programs

python programs

Uploaded by

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

Python Programs

python programs

Uploaded by

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

1.

Python Program to Calculate the Average of Numbers in a Given List

n=int(input("enter n:"))

a=[]

for i in range(n):

a.append(int(input("enter element:")))

print("Average =",round(sum(a)/n,2))

2. Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
a,b=map(int,input("enter 2 nos:").split(" "))
print("before swap",a,b)
a,b=b,a
print("after swap",a,b)

3. Python Program to Read a Mumber n and Compute n+nn+nnn

n=input("enter a number:")

nn=int(n+n)

nnn=int(n+n+n)

print(int(n)+nn+nnn)

4. Python Program to Reverse a Given Number

n=int(input("enter a number:"))

r=0

while(n>0):

d=n%10

r=r*10+d

n=n//10

print(r)

#n=input("enter a number:")

#print(n[::-1])
5. Python Program to Check Whether a Number is Positive or Negative

n=int(input("enter n:"))

if(n>0):

print("pos")

else:

print("neg")

6. Python Program to Take in the Marks of 5 Subjects and Display the Grade

a=[]

for i in range(5):

a.append(int(input("enter subject marks:")))

avg=sum(a)/5

if(avg>=80):

print("a")

elif(avg>=70):

print("b")

elif(avg>=60):

print("c")

elif(avg>=50):

print("d")

else:

print("f")

7. Python Program to Print all Numbers in a Range Divisible by a Given Number

l,u=map(int,input("Enter lower and upper range limit:").split(" "))

n=int(input("Enter the number to be divided by:"))

for i in range(l,u+1):

if(i%n==0):

print(i)

8. Python Program to Read Two Numbers and Print Their Quotient and Remainder
a,b=map(int,input("Enter any 2 nos:").split(" "))
print("Quotient=",a//b)
print("Remainder=",a%b)

9. Python Program to Accept Three Digits and Print all Possible Combinations from the Digits

a=[]

for i in range(3):

a.append(int(input("enter elements:")))

for i in range(3):

for j in range(3):

for k in range(3):

if(i!=j&j!=k&k!=i):

print(a[i],a[j],a[k])

10. Python Program to Print Odd Numbers Within a Given Range

lower=int(input("Enter the lower limit for the range:"))

upper=int(input("Enter the upper limit for the range:"))

for i in range(lower,upper+1):

if(i%2!=0):

print(i)

11. Python Program to Find the Sum of Digits in a Number

n=int(input("enter a number:"))

s=0

while(n>0):

d=n%10

s=s+d

n=n//10

print(s)
12. Python Program to Find the Smallest Divisor of an Integer

n=int(input("Enter an integer:"))

a=[]

for i in range(2,n+1):

if(n%i==0):

a.append(i)

a.sort()

print("Smallest divisor is:",a[0])

13. Python Program to Count the Number of Digits in a Number

n=int(input("enter a number:"))

c=0

while(n>0):

c=c+1

n=n//10

print("no of digits=",c)

14. Python Program to Check if a Number is a Palindrome

n=int(input("enter a number:"))

p=n

r=0

while(n>0):

d=n%10

r=r*10+d

n=n//10

if(p==r):

print("palindrome")

else:

print("not a palindrome")
15. Python Program to Print all Integers that Aren't Divisible by Either 2 or 3 and Lie between 1 and
50.

for i in range(1,51):

if((i%2!=0)&(i%3!=0)):

print(i)

16. Python Program to Read a Number n And Print the Series "1+2+…..+n= "

n=int(input("enter n:"))

s=0

for i in range(1,n):

print(i,"+",sep=" ",end=" ")

s=s+i

print(n,"=",s+n,sep=" ",end=" ")

17. Python Program to Read a Number n and Print the Natural Numbers Summation Pattern

n=int(input("enter n:"))

s=0

for i in range(n+1):

for j in range(i):

print(j,"+",sep=" ",end=" ")

s=s+i

print(i,"=",s+i,sep=" ")

18. Python Program to Print an Identity Matrix

n=int(input("enter n:"))

for i in range(n):

for j in range(n):

if(i==j):
print("1",end=" ")

else:

print("0",end=" ")

print()

19. Python Program to Print an Inverted Star Pattern

n=int(input("enter n:"))

for i in range(n+1):

print(" "*i + "*"*(n-i))

20. Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes

n=int(input("Enter upper limit of range: "))

s=set(range(2,n+1))

while s:

p=min(s)

print(p)

s-=set(range(p,n+1,p))

print()

21. Python Program to Check if a Date is Valid and Print the Incremented Date if it is

date=input("Enter the date: ")

dd,mm,yy=map(int,date.split('/'))

if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12):

max1=31

elif(mm==4 or mm==6 or mm==9 or mm==11):

max1=30

elif(yy%4==0 and yy%100!=0 or yy%400==0):

max1=29

else:

max1=28

if(mm<1 or mm>12):
print("Date is invalid.")

elif(dd<1 or dd>max1):

print("Date is invalid.")

elif(dd==max1 and mm!=12):

dd=1

mm=mm+1

print("The incremented date is: ",dd,mm,yy)

elif(dd==31 and mm==12):

dd=1

mm=1

yy=yy+1

print("The incremented date is: ",dd,mm,yy)

else:

dd=dd+1

print("The incremented date is: ",dd,mm,yy)

22. Python Program to Compute Simple Interest Given all the Required Values

p,t,r=map(int,input("enter principle,time,rate:").split(","))

si=p*t*r/100

print("simple interest=",si)

23. Python Program to Check Whether a Given Year is a Leap Year


y=int(input("enter year:"))
if(y%4==0 and y%100!=0 or y%400==0):
print("leap year")
else:
print("not leap year")

24. Python Program to Read Height in Centimeters and then Convert the Height to Feet and Inches

cm=int(input("enter height in cm:"))

inches=0.394*cm
feet=0.0328*cm

print("height in inches:",round(inches,2))

print("height in feet:",round(feet,2))

25. Python Program to Take the Temperature in Celcius and Covert it to Farenheit
celsius=int(input("Enter the temperature in celcius:"))
f=(celsius*1.8)+32
print("Temperature in farenheit is:",f)

26. Python Program to Compute Prime Factors of an Integer

27. Python Program to Generate all the Divisors of an Integer

28. Python Program to Print Table of a Given Number

29. Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd
numbers in a List

30. Python Program to Print Largest Even and Largest Odd Number in a List

31. Python Program to Form an Integer that has the Number of Digits at Ten's Place and the Least
Significant Digit of the Entered Integer at One's Place

32. Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given
Range of Numbers

33. Python Program to Check if a Number is an Armstrong Number


34. Python Program to Print the Pascal's triangle for n number of rows given by the user
35. Python Program to Check if a Number is a Perfect Number
36. Python Program to Check if a Number is a Strong Number
37. Python Program to Find the LCM of Two Numbers
38. Python Program to Find the GCD of Two Numbers
39. Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial
are stored in a List
40. Python Program to Check If Two Numbers are Amicable Numbers
41. Python Program to Find the Area of a Triangle Given All Three Sides
42. Python Program to Find the Gravitational Force Acting Between Two Objects
43. Python Program to Check if a Number is a Prime Number
44. Python Program to Print all the Prime Numbers within a Given Range
45. Python Program to Print Numbers in a Range (1,upper) Without Using any Loops
46. Python Program to Find the Sum of Sine Series
47. Python Program to Find the Sum of Cosine Series
48. Python Program to Find the Sum of First N Natural Numbers
49. Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N
50. Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n
51. Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! + 1/2!
+ …… 1/n!
52. Python Program to Determine all Pythagorean Triplets in the Range
53. Python Program to Search the Number of Times a Particular Number Occurs in a List

You might also like