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

50 Simple Python Programs

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)
4 views5 pages

50 Simple Python Programs

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

50 Simple Solved Python Programs for Beginners

1. Print Hello World


print('Hello, World!')

2. Add Two Numbers


a=5
b=3
print(a+b)

3. Subtract Two Numbers


a=10
b=4
print(a-b)

4. Multiply Two Numbers


a=6
b=7
print(a*b)

5. Divide Two Numbers


a=20
b=5
print(a/b)

6. Find Remainder
print(10%3)

7. Swap Two Variables


a,b=5,10
a,b=b,a
print(a,b)

8. Check Even or Odd


n=8
print('Even' if n%2==0 else 'Odd')

9. Find Largest Number


a,b=9,12
print(max(a,b))

10. Find Smallest Number


a,b=9,12
print(min(a,b))

11. Square of Number


n=5
print(n*n)

12. Cube of Number


n=3
print(n**3)

13. Find Length of String


s='Python'
print(len(s))

14. Convert to Uppercase


print('python'.upper())

15. Convert to Lowercase


print('PYTHON'.lower())

16. Reverse String


print('Python'[::-1])

17. Check Palindrome


s='madam'
print(s==s[::-1])

18. Count Vowels


s='education'
print(sum(1 for c in s if c in 'aeiou'))

19. Factorial
n=5
f=1
for i in range(1,n+1): f*=i
print(f)

20. Print 1 to 10
for i in range(1,11): print(i)

21. Sum 1 to 100


print(sum(range(1,101)))

22. Multiplication Table


n=5
for i in range(1,11): print(n*i)

23. Check Prime


n=7
print(all(n%i for i in range(2,n)))

24. Fibonacci Series


a,b=0,1
for _ in range(10): print(a); a,b=b,a+b

25. Find Maximum in List


print(max([3,8,2,9]))

26. Find Minimum in List


print(min([3,8,2,9]))

27. Sum of List


print(sum([1,2,3,4]))

28. Average of List


lst=[1,2,3,4]
print(sum(lst)/len(lst))

29. Sort List


print(sorted([4,1,3,2]))

30. Remove Duplicates


print(list(set([1,2,2,3,3])))

31. Count Elements


print(len([1,2,3,4]))

32. Check Leap Year


year=2024
print(year%4==0 and (year%100!=0 or year%400==0))

33. Area of Circle


import math
r=5
print([Link]*r*r)

34. Area of Rectangle


l,w=5,4
print(l*w)

35. Area of Triangle


b,h=10,5
print(0.5*b*h)

36. Celsius to Fahrenheit


c=30
print((c*9/5)+32)

37. Fahrenheit to Celsius


f=86
print((f-32)*5/9)

38. Random Number


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

39. Check Positive Negative


n=-5
print('Positive' if n>0 else 'Negative')

40. Count Words


s='I love Python'
print(len([Link]()))

41. Replace Word


print('I love Java'.replace('Java','Python'))

42. Dictionary Access


d={'a':1}
print(d['a'])

43. Merge Dictionaries


a={'x':1}; b={'y':2}
print(a|b)

44. Tuple Length


print(len((1,2,3)))

45. Set Union


print({1,2}|{2,3})

46. Simple Function


def add(a,b): return a+b
print(add(2,3))

47. Lambda Function


print((lambda x:x*x)(5))

48. Exception Handling


try:
print(10/0)
except:
print('Error')

49. Read File


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

50. Class Example


class Student:
pass
s=Student()
print(type(s).__name__)

You might also like