0% found this document useful (0 votes)
3 views5 pages

50 Python Practical Questions With Answers

The document contains a series of Python programming exercises and their corresponding solutions. Each exercise covers fundamental programming concepts such as input/output, arithmetic operations, control structures, data structures, and file handling. The solutions are concise and demonstrate various Python functionalities, including functions, classes, and libraries.

Uploaded by

taunsvigamers
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)
3 views5 pages

50 Python Practical Questions With Answers

The document contains a series of Python programming exercises and their corresponding solutions. Each exercise covers fundamental programming concepts such as input/output, arithmetic operations, control structures, data structures, and file handling. The solutions are concise and demonstrate various Python functionalities, including functions, classes, and libraries.

Uploaded by

taunsvigamers
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

1. Write a Python program to print Hello World.

Answer:
print('Hello World')

2. Write a program to input a number and display it.


Answer:
num = int(input('Enter number: '))
print(num)

3. Write a Python program to add two numbers.


Answer:
a = int(input())
b = int(input())
print(a+b)

4. Write a Python program to subtract two numbers.


Answer:
a = int(input())
b = int(input())
print(a-b)

5. Write a Python program to multiply two numbers.


Answer:
a=int(input())
b=int(input())
print(a*b)

6. Write a Python program to divide two numbers.


Answer:
a=int(input())
b=int(input())
print(a/b)

7. Write a program to check even or odd.


Answer:
n=int(input())
if n%2==0:
print('Even')
else:
print('Odd')

8. Write a program to check positive or negative.


Answer:
n=int(input())
if n>0:
print('Positive')
elif n<0:
print('Negative')
else:
print('Zero')

9. Write a program to find square of a number.


Answer:
n=int(input())
print(n*n)

10. Write a program to find cube of a number.


Answer:
n=int(input())
print(n**3)

11. Print numbers from 1 to 10.


Answer:
for i in range(1,11):
print(i)

12. Print numbers from 10 to 1.


Answer:
for i in range(10,0,-1):
print(i)

13. Find factorial of a number.


Answer:
n=int(input())
f=1
for i in range(1,n+1):
f*=i
print(f)

14. Print multiplication table.


Answer:
n=int(input())
for i in range(1,11):
print(n*i)

15. Find sum from 1 to n.


Answer:
n=int(input())
s=0
for i in range(1,n+1):
s+=i
print(s)

16. Reverse a string.


Answer:
s=input()
print(s[::-1])

17. Count length of string.


Answer:
s=input()
print(len(s))

18. Check palindrome string.


Answer:
s=input()
if s==s[::-1]:
print('Palindrome')

19. Check prime number.


Answer:
n=int(input())
flag=True
for i in range(2,n):
if n%i==0:
flag=False
if flag:
print('Prime')

20. Fibonacci series.


Answer:
n=int(input())
a,b=0,1
for i in range(n):
print(a)
a,b=b,a+b

21. Create list and print.


Answer:
a=[1,2,3,4]
print(a)

22. Find max in list.


Answer:
a=[3,7,2,9]
print(max(a))

23. Find min in list.


Answer:
a=[3,7,2,9]
print(min(a))

24. Sum list elements.


Answer:
a=[1,2,3]
print(sum(a))

25. Sort list ascending.


Answer:
a=[5,2,8]
[Link]()
print(a)

26. Sort list descending.


Answer:
a=[5,2,8]
[Link](reverse=True)
print(a)

27. Remove duplicate from list.


Answer:
a=[1,1,2,3]
a=list(set(a))
print(a)

28. Count element in list.


Answer:
a=[1,2,2,3]
print([Link](2))

29. Create dictionary.


Answer:
d={'Ali':80,'Sara':90}
print(d)
30. Access dictionary value.
Answer:
d={'Ali':80}
print(d['Ali'])

31. Update dictionary value.


Answer:
d={'Ali':80}
d['Ali']=85
print(d)

32. Loop through dictionary.


Answer:
d={'A':1,'B':2}
for k,v in [Link]():
print(k,v)

33. Function to square number.


Answer:
def square(n):
return n*n

34. Function to add numbers.


Answer:
def add(a,b):
return a+b

35. Handle division by zero.


Answer:
try:
a=10/0
except:
print('Error')

36. Write file.


Answer:
f=open('[Link]','w')
[Link]('Hello')
[Link]()

37. Read file.


Answer:
f=open('[Link]','r')
print([Link]())

38. Append file.


Answer:
f=open('[Link]','a')
[Link]('More')

39. Count words in text.


Answer:
text='hello world python'
print(len([Link]()))

40. Class example.


Answer:
class Student:
def __init__(self,name):
[Link]=name

41. Create object.


Answer:
s=Student('Ali')

42. List comprehension.


Answer:
a=[i for i in range(10)]

43. Lambda function.


Answer:
add=lambda a,b:a+b

44. Map example.


Answer:
a=list(map(int,['1','2']))

45. Filter example.


Answer:
a=list(filter(lambda x:x%2==0,[1,2,3,4]))

46. Zip example.


Answer:
a=list(zip([1,2],[3,4]))

47. Tuple example.


Answer:
t=(1,2,3)

48. Set example.


Answer:
s=set([1,2,2,3])

49. Random number.


Answer:
import random
print([Link](1,10))

50. Current date.


Answer:
from datetime import date
print([Link]())

You might also like