0% found this document useful (0 votes)
2 views36 pages

Python Programs

The document provides a collection of Python programs that demonstrate various programming concepts, including checking if a number is even or odd, calculating factorials, identifying prime numbers, and more. Each program includes code snippets, example outputs, and explanations of the functionality. The programs cover a wide range of topics, from basic arithmetic operations to more complex algorithms and data manipulations.

Uploaded by

samarth19110
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)
2 views36 pages

Python Programs

The document provides a collection of Python programs that demonstrate various programming concepts, including checking if a number is even or odd, calculating factorials, identifying prime numbers, and more. Each program includes code snippets, example outputs, and explanations of the functionality. The programs cover a wide range of topics, from basic arithmetic operations to more complex algorithms and data manipulations.

Uploaded by

samarth19110
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

PYTHON

PROGRAMS
Program 1: Number is Even or Odd
#Code :-
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")

#Output:-
10 → Even
7 → Odd

Program 2: Factorial
#Code :-
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial of", num, "is", fact)

#Output:-
5 → 120
7 → 5040
1|Page
Program 3: Prime Number
#Code :-
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print(num, "is not Prime")
break
else:
print(num, "is Prime")
else:
print(num, "is not Prime")

#Output:-
11 → Prime
12 → not Prime

Program 4: Largest Among n Numbers


#Code :-
n = int(input("Enter how many numbers: "))
nums = []
for i in range(n):
[Link](int(input("Enter number: ")))
print("Largest number is:", max(nums))

#Output:-
[12, 45, 7, 30] → 45
[5, 9, 2] → 9

2|Page
Program 5: Swap Two Numbers
#Code :-
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print("After swapping: a =", a, "b =", b)

#Output:-
5, 10 → a=10 b=5
20, 30 → a=30 b=20

Program 6: Swap Two Numbers (Without Third


Variable)
#Code :-
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping: a =", a, "b =", b)

#Output:-
5, 10 → a=10 b=5
20, 30 → a=30 b=20

3|Page
Program 7: Fibonacci Series
#Code :-
n = int(input("Enter number of terms: "))
a, b = 0, 1
print("Fibonacci Series:")
for i in range(n):
print(a, end=" ")
a, b = b, a + b

#Output:-
5 → 0 1 1 2 3
7 → 0 1 1 2 3 5 8

Program 8: Palindrome (String)


#Code :-
s = input("Enter a string: ")
if s == s[::-1]:
print(s, "is Palindrome")
else:
print(s, "is not Palindrome")

#Output:-
madam → Palindrome
hello → not Palindrome

4|Page
Program 9: Palindrome Number
#Code :-
num = int(input("Enter a number: "))
temp = num
rev = 0
while temp > 0:
rev = rev * 10 + temp % 10
temp //= 10
if num == rev:
print(num, "is Palindrome")
else:
print(num, "is not Palindrome")

#Output:-
121 → Palindrome
123 → not Palindrome

Program 10: Leap Year


#Code :-
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 100 != 0 and
year % 4 == 0):
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")

#Output:-
2020 → Leap Year
2021 → not Leap Year

5|Page
Program 11: Perfect Number
#Code :-
num = int(input("Enter a number: "))
sum_div = 0
for i in range(1, num):
if num % i == 0:
sum_div += i
if sum_div == num:
print(num, "is a Perfect Number")
else:
print(num, "is not a Perfect Number")

#Output:-
28 → Perfect Number
12 → not Perfect Number

Program 12: Armstrong Number


#Code :-
num = int(input("Enter a number: "))
sum_pow = 0
temp = num
digits = len(str(num))
while temp > 0:
digit = temp % 10
sum_pow += digit ** digits
temp //= 10
if sum_pow == num:
print(num, "is an Armstrong Number")
else:
print(num, "is not an Armstrong Number")

#Output:-
153 → Armstrong Number
123 → not Armstrong Number
6|Page
Program 13: Strong Number
#Code :-
import math
num = int(input("Enter a number: "))
temp = num
sum_fact = 0
while temp > 0:
digit = temp % 10
sum_fact += [Link](digit)
temp //= 10
if sum_fact == num:
print(num, "is a Strong Number")
else:
print(num, "is not a Strong Number")

#Output:-
145 → Strong Number
123 → not Strong Number

Program 14: Reverse Number


#Code :-
num = int(input("Enter a number: "))
rev = 0
while num > 0:
rev = rev * 10 + num % 10
num //= 10
print("Reversed number:", rev)

#Output:-
1234 → 4321
987 → 789

7|Page
Program 15: Sum of Digits
#Code :-
num = int(input("Enter a number: "))
sum_digits = 0
while num > 0:
sum_digits += num % 10
num //= 10
print("Sum of digits:", sum_digits)

#Output:-
123 → 6
456 → 15

Program 16: Power of a Number


#Code :-
base = int(input("Enter base: "))
exp = int(input("Enter exponent: "))
print("Result:", base ** exp)

#Output:-
2^5 → 32
3^4 → 81

8|Page
Program 17: Count Digits
#Code :-
num = int(input("Enter a number: "))
count = len(str(num))
print("Number of digits:", count)

#Output:-
12345 → 5
789 → 3

Program 18: Random Number Generator


#Code :-
import random
print("Random number:", [Link](1, 100))
print("Random number:", [Link](1, 100))

#Output:-
Random number: 42
Random number: 87

Program 19: Celsius to Fahrenheit


#Code :-
9|Page
c = float(input("Enter temperature in Celsius:
"))
f = (c * 9/5) + 32
print("Temperature in Fahrenheit:", f)

#Output:-
0°C → 32°F
100°C → 212°F

Program 20: Fahrenheit to Celsius


#Code :-
f = float(input("Enter temperature in
Fahrenheit: "))
c = (f - 32) * 5/9
print("Temperature in Celsius:", c)

#Output:-
32°F → 0°C
212°F → 100°C

Program 21: Anagram


#Code :-
s1 = input("Enter first string: ")

10 | P a g e
s2 = input("Enter second string: ")
if sorted(s1) == sorted(s2):
print("Strings are Anagram")
else:
print("Strings are not Anagram")

#Output:-
listen, silent → Anagram
hello, world → not Anagram

Program 22: Super Digit


#Code :-
def super_digit(n):
if len(n) == 1:
return int(n)
return super_digit(str(sum(map(int, n))))

num = input("Enter a number: ")


print("Super Digit:", super_digit(num))

#Output:-
9875 → 2
123 → 6

Program 23: Simple Interest


#Code :-
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))

11 | P a g e
t = float(input("Enter time: "))
si = (p * r * t) / 100
print("Simple Interest:", si)

#Output:-
1000, 5%, 2 → 100
2000, 10%, 1 → 200

Program 24: Compound Interest


#Code :-
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = float(input("Enter time: "))
ci = p * ((1 + r/100) ** t) - p
print("Compound Interest:", ci)

#Output:-
1000, 5%, 2 → 102.5
2000, 10%, 1 → 200

Program 25: Area of Circle


#Code :-
import math
r = float(input("Enter radius: "))

12 | P a g e
area = [Link] * r * r
print("Area of Circle:", area)

#Output:-
r=7 → 153.94
r=10 → 314.16

Program 26: Area of Triangle


#Code :-
b = float(input("Enter base: "))
h = float(input("Enter height: "))
area = 0.5 * b * h
print("Area of Triangle:", area)

#Output:-
b=10, h=5 → 25
b=12, h=6 → 36

Program 27: Area of Square


#Code :-
side = float(input("Enter side: "))
area = side * side

13 | P a g e
print("Area of Square:", area)

#Output:-
side=4 → 16
side=7 → 49

Program 28: Area of Rectangle


#Code :-
l = float(input("Enter length: "))
w = float(input("Enter width: "))
area = l * w
print("Area of Rectangle:", area)

#Output:-
l=10, w=5 → 50
l=12, w=6 → 72

Program 29: Area of Parallelogram


#Code :-
b = float(input("Enter base: "))
h = float(input("Enter height: "))
area = b * h

14 | P a g e
print("Area of Parallelogram:", area)

#Output:-
b=10, h=5 → 50
b=12, h=6 → 72

Program 30: Area of Rhombus


#Code :-
d1 = float(input("Enter diagonal 1: "))
d2 = float(input("Enter diagonal 2: "))
area = (d1 * d2) / 2
print("Area of Rhombus:", area)

#Output:-
d1=10, d2=8 → 40
d1=12, d2=6 → 36

Program 31: Area of Trapezium


#Code :-
a = float(input("Enter first parallel side: "))
b = float(input("Enter second parallel side:
"))
h = float(input("Enter height: "))

15 | P a g e
area = 0.5 * (a + b) * h
print("Area of Trapezium:", area)

#Output:-
a=10, b=6, h=5 → 40
a=12, b=8, h=7 → 70

Program 32: Volume of Cube


#Code :-
side = float(input("Enter side: "))
volume = side ** 3
print("Volume of Cube:", volume)

#Output:-
side=3 → 27
side=5 → 125

Program 33: Volume of Cylinder


#Code :-
import math
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
volume = [Link] * r * r * h

16 | P a g e
print("Volume of Cylinder:", volume)

#Output:-
r=3, h=7 → 197.92
r=5, h=10 → 785.40

Program 34: Volume of Cone


#Code :-
import math
r = float(input("Enter radius: "))
h = float(input("Enter height: "))
volume = (1/3) * [Link] * r * r * h
print("Volume of Cone:", volume)

#Output:-
r=3, h=9 → 84.82
r=5, h=12 → 314.16

Program 35: Volume of Sphere


#Code :-
import math
r = float(input("Enter radius: "))
volume = (4/3) * [Link] * r ** 3

17 | P a g e
print("Volume of Sphere:", volume)

#Output:-
r=3 → 113.10
r=5 → 523.60

Program 36: Positive or Negative


#Code :-
num = int(input("Enter a number: "))
if num > 0:
print(num, "is Positive")
elif num < 0:
print(num, "is Negative")
else:
print("Number is Zero")

#Output:-
5 → Positive
-3 → Negative

Program 37: Largest of Three Numbers


#Code :-
a = int(input("Enter first: "))
b = int(input("Enter second: "))

18 | P a g e
c = int(input("Enter third: "))
print("Largest:", max(a, b, c))

#Output:-
10, 20, 15 → 20
7, 3, 9 → 9

Program 38: Smallest of Three Numbers


#Code :-
a = int(input("Enter first: "))
b = int(input("Enter second: "))
c = int(input("Enter third: "))
print("Smallest:", min(a, b, c))

#Output:-
10, 20, 15 → 10
7, 3, 9 → 3

Program 39: Vowel or Consonant


#Code :-
ch = input("Enter a character: ").lower()

19 | P a g e
if ch in 'aeiou':
print(ch, "is a Vowel")
else:
print(ch, "is a Consonant")

#Output:-
a → Vowel
b → Consonant

Program 40: Sum of Natural Numbers


#Code :-
n = int(input("Enter n: "))
sum_n = n * (n + 1) // 2
print("Sum of first", n, "natural numbers:",
sum_n)

#Output:-
n=10 → 55
n=5 → 15

20 | P a g e
Program 41: Factorial using Recursion
#Code :-
def fact(n):
if n == 0 or n == 1:
return 1
return n * fact(n-1)

num = int(input("Enter a number: "))


print("Factorial:", fact(num))

#Output:-
5 → 120
7 → 5040

21 | P a g e
Program 42: Calculator
#Code :-
def add(a,b): return a+b
def sub(a,b): return a-b
def mul(a,b): return a*b
def div(a,b): return a/b

print("[Link] [Link] [Link] [Link]")


choice = int(input("Enter choice: "))
a = int(input("Enter first: "))
b = int(input("Enter second: "))

if choice == 1: print("Result:", add(a,b))


elif choice == 2: print("Result:", sub(a,b))
elif choice == 3: print("Result:", mul(a,b))
elif choice == 4: print("Result:", div(a,b))
else: print("Invalid choice")

#Output:-
Choice=1, 5, 3 → 8
Choice=3, 4, 6 → 24

22 | P a g e
Program 43: Password Generator
#Code :-
import random, string
length = int(input("Enter password length: "))
chars = string.ascii_letters + [Link] +
[Link]
password = ''.join([Link](chars) for i
in range(length))
print("Generated Password:", password)

#Output:-
Length=8 → Ab3$kL9!
Length=12 → xY7@pQ2#Lm1

Program 44: Reverse String


#Code :-
s = input("Enter a string: ")
print("Reversed:", s[::-1])

#Output:-
hello → olleh
#Code :- → nohtyP

23 | P a g e
Program 45: Count Vowels in String
#Code :-
s = input("Enter a string: ").lower()
count = sum(1 for ch in s if ch in 'aeiou')
print("Number of vowels:", count)

#Output:-
hello → 2
education → 5

Program 46: List Sum


#Code :-
nums = [int(x) for x in input("Enter numbers
separated by space: ").split()]
print("Sum:", sum(nums))

#Output:-
1 2 3 4 → 10
10 20 30 → 60

24 | P a g e
Program 47: List Average
#Code :-
nums = [int(x) for x in input("Enter numbers
separated by space: ").split()]
print("Average:", sum(nums)/len(nums))

#Output:-
1 2 3 4 → 2.5
10 20 30 → 20.0

Program 48: Largest in List


#Code :-
nums = [int(x) for x in input("Enter numbers
separated by space: ").split()]
print("Largest:", max(nums))

#Output:-
1 2 3 4 → 4
10 20 30 → 30

25 | P a g e
Program 49: Smallest in List
#Code :-
nums = [int(x) for x in input("Enter numbers
separated by space: ").split()]
print("Smallest:", min(nums))

#Output:-
1 2 3 4 → 1
10 20 30 → 10

Program 50: Sort List


#Code :-
nums = [int(x) for x in input("Enter numbers
separated by space: ").split()]
print("Sorted:", sorted(nums))

#Output:-
4 2 7 1 → [1,2,4,7]
10 5 8 → [5,8,10]

26 | P a g e
Program 51: Lambda Add
#Code :-
add = lambda a,b: a+b
print("Sum:", add(5,3))
print("Sum:", add(10,20))

#Output:-
5+3 → 8
10+20 → 30

Program 52: Lambda Sub


#Code :-
sub = lambda a,b: a-b
print("Difference:", sub(10,4))
print("Difference:", sub(25,7))

#Output:-
10-4 → 6
25-7 → 18

27 | P a g e
Program 53: Lambda Mul
#Code :-
mul = lambda a,b: a*b
print("Product:", mul(6,7))
print("Product:", mul(3,9))

#Output:-
6*7 → 42
3*9 → 27

Program 54: Lambda Div


#Code :-
div = lambda a,b: a/b
print("Quotient:", div(20,5))
print("Quotient:", div(9,3))

#Output:-
20/5 → 4.0
9/3 → 3.0

28 | P a g e
Program 55: Lambda Mod
#Code :-
mod = lambda a,b: a % b
print("Remainder:", mod(10,3))
print("Remainder:", mod(25,7))

#Output:-
10 % 3 → 1
25 % 7 → 4

Program 56: Lambda Square


#Code :-
square = lambda x: x * x
print("Square:", square(5))
print("Square:", square(12))

#Output:-
5 → 25
12 → 144

29 | P a g e
Program 57: Lambda Even or Odd
#Code :-
check = lambda x: "Even" if x % 2 == 0 else
"Odd"
print("7 is", check(7))
print("10 is", check(10))

#Output:-
7 → Odd
10 → Even

Program 58: Lambda Maximum of Two


#Code :-
maximum = lambda a,b: a if a > b else b
print("Max:", maximum(10,20))
print("Max:", maximum(45,12))

#Output:-
10,20 → 20
45,12 → 45

30 | P a g e
Program 59: Lambda Minimum of Two
#Code :-
minimum = lambda a,b: a if a < b else b
print("Min:", minimum(10,20))
print("Min:", minimum(45,12))

#Output:-
10,20 → 10
45,12 → 12

Program 60: Lambda Sort List


#Code :-
nums = [5,2,9,1,7]
sorted_list = sorted(nums, key=lambda x: x)
print("Sorted List:", sorted_list)

nums2 = [12,4,8,3]
print("Sorted List:", sorted(nums2, key=lambda
x: x))

#Output:-
[5,2,9,1,7] → [1,2,5,7,9]
[12,4,8,3] → [3,4,8,12]

31 | P a g e
Program 61: Lambda Filter Even Numbers
#Code :-
nums = [1,2,3,4,5,6,7,8,9]
evens = list(filter(lambda x: x % 2 == 0,
nums))
print("Even numbers:", evens)

#Output:-
[1..9] → [2,4,6,8]
[10..20] → [10,12,14,16,18,20]

Program 62: Lambda Filter Odd Numbers


#Code :-
nums = [1,2,3,4,5,6,7,8,9]
odds = list(filter(lambda x: x % 2 != 0, nums))
print("Odd numbers:", odds)

#Output:-
[1..9] → [1,3,5,7,9]
[10..20] → [11,13,15,17,19]

32 | P a g e
Program 63: Lambda Map Squares
#Code :-
nums = [1,2,3,4,5]
squares = list(map(lambda x: x*x, nums))
print("Squares:", squares)

#Output:-
[1..5] → [1,4,9,16,25]
[6..10] → [36,49,64,81,100]

Program 64: Lambda Map Cubes


#Code :-
nums = [1,2,3,4,5]
cubes = list(map(lambda x: x**3, nums))
print("Cubes:", cubes)

#Output:-
[1..5] → [1,8,27,64,125]
[6..10] → [216,343,512,729,1000]

33 | P a g e
Program 65: Lambda Reduce Sum
#Code :-
from functools import reduce
nums = [1,2,3,4,5]
sum_all = reduce(lambda a,b: a+b, nums)
print("Sum:", sum_all)

#Output:-
[1..5] → 15
[10,20,30] → 60

Program 66: Lambda Reduce Product


#Code :-
from functools import reduce
nums = [1,2,3,4,5]
product = reduce(lambda a,b: a*b, nums)
print("Product:", product)

#Output:-
[1..5] → 120
[2,3,4] → 24

34 | P a g e
Program 67: Lambda Sort by Length
#Code :-
words = ["apple","banana","kiwi","grape"]
sorted_words = sorted(words, key=lambda x:
len(x))
print("Sorted by length:", sorted_words)

#Output:-
["apple","banana","kiwi","grape"] →
['kiwi','apple','grape','banana']
["dog","elephant","cat"] →
['cat','dog','elephant']

Program 68: Lambda Sort by Last Character


#Code :-
words = ["apple","banana","kiwi","grape"]
sorted_words = sorted(words, key=lambda x: x[-
1])
print("Sorted by last char:", sorted_words)

#Output:-
["apple","banana","kiwi","grape"] →
['banana','apple','grape','kiwi']
["dog","elephant","cat"] →
['banana','apple','grape','kiwi']

35 | P a g e
Program 69: Lambda Custom Key (Second Element)
#Code :-
pairs = [(1,2),(3,1),(5,4),(2,3)]
sorted_pairs = sorted(pairs, key=lambda x:
x[1])
print("Sorted by second element:",
sorted_pairs)

#Output:-
[(1,2),(3,1),(5,4),(2,3)] → [(3,1),(1,2),(2,3),
(5,4)]
[(7,9),(4,2),(6,5)] → [(4,2),(6,5),(7,9)]

Program 70: Lambda with Conditional


#Code :-
nums = [1,2,3,4,5]
labels = list(map(lambda x: "Even" if x%2==0
else "Odd", nums))
print("Labels:", labels)

#Output:-
[1..5] → ['Odd','Even','Odd','Even','Odd']
[6..10] → ['Even','Odd','Even','Odd','Even']

36 | P a g e

You might also like