The document contains a series of Python programming exercises along with their solutions. Each exercise covers a fundamental programming concept, such as input/output, conditionals, loops, and basic data structures. The solutions demonstrate practical implementations of these concepts in Python 3.12.4.
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 ratings0% found this document useful (0 votes)
6 views15 pages
Idle Python
The document contains a series of Python programming exercises along with their solutions. Each exercise covers a fundamental programming concept, such as input/output, conditionals, loops, and basic data structures. The solutions demonstrate practical implementations of these concepts in Python 3.12.4.
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/ 15
IDLE PYTHON
ALL PROGRAMS Version (3.12.4)
1. Write a Python program to print "Hello,
World!" Sol. print("Hello World") print("Welcome to IDLE python 3.12.4")
2. Write a program to take user input and
display it. Sol. n=input("Enter something: ") print("You have entered ",n)
3. Write a Python program to swap two
numbers. Sol. a=int(input("Enter value of a : ")) b=int(input("Enter value of b : ")) print("After Swapping...") print("The value of a is ",b) print("The value of b is ",a)
4. Find the maximum of three numbers.
Sol. a=float(input("Enter first number : ")) b=float(input("Enter second number : ")) c=float(input("Enter third number : ")) list=[a,b,c] print("Maximum one is ",max(list)) 5. Check if a number is even or odd. Sol. a=int(input("Enter a number : ")) if(a%2==0): print("This is an even number") else: print("This is an odd number")
6. Check if a number is positive, negative,
or zero. Sol. a=int(input("Enter a number : ")) if(a>0): print("This is a positive number.") elif(a<0): print("This is a negative number.") else: print("This is a zero.")
7. Calculate the sum and average of two
numbers. Sol. a=int(input('Enter first number : ')) b=int(input("Enter second number : ")) c=a+b d=c/2 print("Sum is ",c) print("Average is ",d)
8. Write a program to calculate the area of
a circle. Sol. a=float(input("Enter radius of the circle : ")) b=(22*a*a)/7 print("The area of the circle is ",b) 9. Convert temperature from Celsius to Fahrenheit. Sol. c=float(input("Enter temperature in celcius : ")) f=((9*c)/5)+32 print("The temperature in farenheit is ",f)
10. Write a program to find the square
root of a number. Sol. a=float(input("Enter a number : ")) b=a**(1/2) print("The square root is ",b)
11. Calculate simple interest given
principal, rate, and time. Sol. p=float(input("Enter principal amount : ")) r=float(input("Enter rate of interest : ")) t=int(input("Enter time period : ")) a=(p*r*t)/100 s=p+a print("The increament is ",a) print("The total amount is ",s)
12. Write a program to check leap year.
Sol. n=int(input("Enter a year : ")) if(n%4==0): print("It is a leap year.") elif(n%400==0): if(n%100==0): print("It is a leap year.") else: print("It is not a leap year.") else: print("It is not a leap year.")
13. Convert kilometres to miles.
Sol. k=float(input("Enter the distance in km : ")) m=k*0.62137 print("The distance in miles is ",m)
14. Write a program to print ASCII values
of a character. Sol. n=input("Enter a character :") b=ord(n) print("The ASCII code is ",b)
15. Find the largest and smallest
numbers in a list. Sol. l=eval(input("Enter a list : ")) m=max(l) s=min(l) print("The maximum value is",m) print('The minimum value is',s)
16. Write a program to check if a
number is prime. Sol. import math n=int(input("Enter a number : ")) a=math.sqrt(n) b=int(a) s=0 for i in range(1,b+1): if(n%i==0): s=s+1 else: s=s+0 if(s>1): print("It is not a prime") else: print("It is a prime")
17. Write a program to find the sum of
first N natural numbers. Sol. n=int(input("Enter a number : ")) s=0 for i in range(0,n+1): s=s+i print("The sum is",s)
18. Find the factorial of a number using a
loop. Sol. n=int(input("Enter a number : ")) f=1 if(n>1): for i in range(1,n+1): f=f*i else: f=1 print("Factorial is",f)
19. Write a program to check if a
number is a palindrome. Sol. n=input("Enter a number or a word : ") a=n[::-1] if(a==n): print("This is a palindrome") else: print("This is not a palindrome")
20. Generate a random number using
the random module. Sol. import random a=random.randrange(-10000,10000) print(a)
21. Check if a number is divisible by
both 3 and 5. Sol. n=float(input("Enter a number: ")) if(n%3==0): if(n%5==0): print("This number is both divisible by 3 & 5") else: print("This number is only divisible by 3") elif(n%5==0): print("This number is only divisible by 5") else: print("This number is neither divisible by 3 nor 5")
22. Find the greatest among three
numbers using if-else. Sol. a=float(input("Enter the first number : ")) b=float(input("Enter the second number : ")) c=float(input("Enter the third number : ")) if(a>b): if(a>c): if(b>c): print(a,">",b,">",c) else: print(a,">",c,">",b) else: print(c,">",a,">",b) else: if(b>c): if(a>c): print(b,">",a,">",c) else: print(b,">",c,">",a) else: print(c,">",b,">",a)
23. Implement a calculator using if-elif-
else. Sol. print("<|><|><|><|><|><|>__CALCULATOR__<|><|><| ><|><|><|>") a=float(input(" Enter first number : ")) b=float(input(" Enter second number : ")) print("<|><|><|><|><|><|>_____MENU_____<|><|><| ><|><|><|>") print(" 1 for Addition") print(" 2 for Subtraction") print(" 3 for Multiplication") print(" 4 for Division") c=int(input(" Enter your choice : ")) print("<|><|><|><|><|><|>____RESULT____<|><|><| ><|><|><|>") if(c==1): r=a+b print(" The result is",r) elif(c==2): r=a-b print(" The result is",r) elif(c==3): r=a*b print(" The result is",r) elif(c==4): r=a/b print(" The result is",r) else: print(" You have entered wrong choice")
24. Print the first N even numbers using
a loop. Sol. n=int(input("Enter total number of numbers you want to print : ")) for i in range(1,((2*n)+1)): if(i%2==0): print(i) else: a=1
25. Write a program to print the
multiplication table of a number. Sol. n=int(input("Enter the number whos table you want : ")) b=int(input("Enter number of factors you want : ")) for i in range(1,b+1): print(n," * ",i," = ",(n*i))
26. Find the sum of digits of a number.
Sol. n=int(input("Enter a number : ")) s=0 while(n>0): a=n%10 s=s+a n=n//10 print("The sum is ",s)
27. Reverse a number using a loop.
Sol. n=int(input("Enter a number : ")) s=0 while(n>0): a=n%10 n=n//10 s=(s*10)+a print("The reversed one is ",s)
28. Write a program to generate
Fibonacci series up to N terms. Sol. n=int(input("Enter a number : ")) a=1 b=1 if(n==1): print(a) elif(n==2): print(a) print(b) else: print(a) print(b) for i in range(2,n): c=a+b print(c) a,b=b,c 29. Count the number of digits in a number. Sol. n=int(input("Enter a number : ")) n=str(n) n=list(n) print("Length of the number is",len(n))
30. Check whether a number is an
Armstrong number. Sol. n=int(input("Enter a number : ")) b=n a=str(n) a=list(a) c=len(a) s=0 while(b>0): d=b%10 s=s+(d**c) b=b//10 if(n==s): print("It is an armstrong number") else: print("It is not an armstrong number") 31. Find the HCF (GCD) of two numbers. Sol. import math a=int(input("Enter first number : ")) b=int(input("Enter second number : ")) c=math.gcd(a,b) print("The highest common divisor",c)
32. Find the LCM of two numbers.
Sol. import math a=int(input("Enter first number : ")) b=int(input("Enter second number : ")) c=math.gcd(a,b) d=(a*b)/c print("The Least common multiple",d)
33. Print all prime numbers within a
given range. Sol. import math x=int(input("Enter a number : ")) z=y=1 for n in range(2,x+1): a=math.sqrt(n) b=int(a) s=0 for i in range(1,b+1): if(n%i==0): s=s+1 else: s=s+0 if(s>1): z=z+y else: print(n) 34. Count the number of vowels and consonants in a string. Sol. n=str(input("Enter a word : ")) v=c=s=0 for i in n: if(i=="a"): v=v+1 elif(i=="e"): v=v+1 elif(i=="i"): v=v+1 elif(i=="o"): v=v+1 elif(i=="u"): v=v+1 elif(i=="A"): v=v+1 elif(i=="E"): v=v+1 elif(i=="I"): v=v+1 elif(i=="O"): v=v+1 elif(i=="U"): v=v+1 elif(i==" "): s=s+1 else: c=c+1 print("The number of consonants",c) print("The number of spaces is",s) print("The number of vowels is",v) 35. Find the sum of all elements in a list. Sol. n=eval(input("Enter a list : ")) print("The sum is ",sum(n))
36. Find the second largest number in a
list. Sol. n=eval(input("Enter a list : ")) a=list(n) a.remove(max(a)) print("The second maximum value is ",max(a))
37. Remove duplicates from a list.
Sol. n=eval(input("Enter a list : ")) b=list(set(n)) print("The final list is",b)
38. Sort a list in ascending and
descending order. Sol. n=eval(input("Enter a list : ")) a=list(n) a.sort() print("In ascending order",a) a.sort(reverse=True) print("In desending order",a) 39. Find the intersection of two lists. Sol. a=eval(input("Enter first list : ")) b=eval(input("Enter second list : ")) c=list(a) d=list(b) e=[] for i in c: for j in d: if(i==j): e=e+[i,] else: e=e+[] print("The intersection is ",e)