100 Python Programs
100 Python Programs
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
12.
num = 17
if num > 1:
for i in range(2, int(num/2)+1):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
13.
num = 5
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("Factorial of 0 is 1")
else:
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is", factorial)
14.
num = 9
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
17.
num = 10
sum = 0
if num < 0:
print("Enter a positive number")
else:
for i in range(1, num + 1):
sum += i
print("Sum:", sum)
18.
decimal = 10
binary = bin(decimal)
print("Binary representation:", binary)
19.
octal = '17'
hexadecimal = hex(int(octal, 8))
print("Hexadecimal representation:", hexadecimal)
20.
char = 'A'
ascii_value = ord(char)
print("ASCII value of", char, "is", ascii_value)
21.
def find_hcf(x, y):
while(y):
x, y = y, x % y
return x
num1 = 24
num2 = 36
num1 = 24
num2 = 36
num1 = 10
num2 = 5
if nterms <= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(fibonacci(i))
26.
def sum_of_natural_numbers(n):
if n <= 1:
return n
else:
return n + sum_of_natural_numbers(n-1)
num = 10
print("Sum of first", num, "natural numbers is", sum_of_natural_numbers(num))
27.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = 5
print("Factorial of", num, "is", factorial(num))
28.
def decimal_to_binary(n):
if n > 1:
decimal_to_binary(n//2)
print(n % 2, end='')
decimal = 10
print("Binary representation:", end=' ')
decimal_to_binary(decimal)
29.
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
30.
X = [[1,2],
[4 ,5],
[7 ,8]]
result = [[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
31.
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
32.
def is_palindrome(s):
s = s.lower()
return s == s[::-1]
string = "radar"
if is_palindrome(string):
print("Palindrome")
else:
print("Not a Palindrome")
33.
import string
def remove_punctuation(s):
return s.translate(str.maketrans('', '', string.punctuation))
rows = 5
print_pyramid(rows)
38.
def merge_dicts(dict1, dict2):
return {**dict1, **dict2}
if not my_list:
print("List is empty")
else:
print("List is not empty")
43.
import shutil
shutil.copy("source_file.txt", "destination_file.txt")
print("File copied successfully.")
44.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
key_to_check = 'a'
if key_to_check in my_dict:
print(key_to_check, "is present in the dictionary")
else:
print(key_to_check, "is not present in the dictionary")
46.
my_list = [1, 2, 3, 4, 5]
last_element = my_list[-1]
print("Last element of the list:", last_element)
47.
my_string = "Hello, World!"
substring = my_string[7:]
print("Substring:", substring)
48.
with open("file.txt", "r") as file:
lines = file.readlines()
lines = [line.strip() for line in lines]
my_list = [1, 2, 3, 4, 5]
random_element = random.choice(my_list)
print("Randomly selected element:", random_element)
50.
my_list = [1, 2, 3, 4, 1, 2, 1, 1]
count_of_ones = my_list.count(1)
print("Count of 1s in the list:", count_of_ones)
51.
with open("file.txt", "r") as file:
line_count = sum(1 for line in file)
reversed_num = int(str(num)[::-1])
print("Reversed number:", reversed_num)
53.
base = 2
exponent = 3
num_of_digits = len(str(num))
print("Number of digits in", num, ":", num_of_digits)
55.
def are_anagrams(str1, str2):
return sorted(str1) == sorted(str2)
string1 = "listen"
string2 = "silent"
if are_anagrams(string1, string2):
print("Strings are anagrams")
else:
print("Strings are not anagrams")
56.
def capitalize_first_char(s):
return s.capitalize()
my_list = [1, 2, 3, 2, 4, 3, 5]
print("List after removing duplicates:", remove_duplicates(my_list))
59.
def find_largest(arr):
return max(arr)
number = 28
if is_perfect_number(number):
print(number, "is a perfect number")
else:
print(number, "is not a perfect number")
63.
def sum_of_digits(num):
return sum(int(digit) for digit in str(num))
number = 12345
print("Sum of digits in", number, "is:", sum_of_digits(number))
64.
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
print("Intersection of lists:", intersection(list1, list2))
65.
from itertools import chain, combinations
def powerset(s):
return list(chain.from_iterable(combinations(s, r) for r in range(len(s)+1)))
my_set = {1, 2, 3}
print("Power set of the set:", powerset(my_set))
66.
def union(set1, set2):
return set1.union(set2)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print("Union of sets:", union(set1, set2))
67.
from statistics import mean, median, mode
my_list = [1, 2, 2, 3, 4, 4, 4, 5]
mean_value = mean(my_list)
median_value = median(my_list)
mode_value = mode(my_list)
print("Mean:", mean_value)
print("Median:", median_value)
print("Mode:", mode_value)
68.
def find_smallest(arr):
return min(arr)
my_array = [10, 20, 30, 40, 50]
print("Smallest element in the array:", find_smallest(my_array))
69.
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
merge_sort(L)
merge_sort(R)
i=j=k=0
number = 5
print("Cube of", number, "is:", cube(number))
78.
def sum_of_cubes(n):
return sum(i**3 for i in range(1, n+1))
n=3
print("Sum of cubes of first", n, "natural numbers is:", sum_of_cubes(n))
79.
def simple_interest(principal, rate, time):
return (principal * rate * time) / 100
principal = 1000
rate = 5
time = 2
print("Simple Interest:", simple_interest(principal, rate, time))
80.
def compound_interest(principal, rate, time):
return principal * ((1 + rate / 100) ** time - 1)
principal = 1000
rate = 5
time = 2
print("Compound Interest:", compound_interest(principal, rate, time))
81.
import math
def area_of_circle(radius):
return math.pi * radius**2
radius = 5
print("Area of the circle with radius", radius, "is:", area_of_circle(radius))
82.
import math
def circumference_of_circle(radius):
return 2 * math.pi * radius
radius = 5
print("Circumference of the circle with radius", radius, "is:", circumference_of_circle(radius))
83.
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False
year = 2024
if is_leap_year(year):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
84.
def gcd(x, y):
while y:
x, y = y, x % y
return x
num1 = 12
num2 = 15
print("LCM of", num1, "and", num2, "is:", lcm(num1, num2))
85.
def area_of_rectangle(length, width):
return length * width
length = 5
width = 4
print("Area of the rectangle with length", length, "and width", width, "is:", area_of_rectangle(length, width))
86.
def perimeter_of_rectangle(length, width):
return 2 * (length + width)
length = 5
width = 4
print("Perimeter of the rectangle with length", length, "and width", width, "is:",
perimeter_of_rectangle(length, width))
87.
def find_smallest(num1, num2, num3):
return min(num1, num2, num3)
num1 = 10
num2 = 5
num3 = 7
print("Smallest number among", num1, ",", num2, ", and", num3, "is:", find_smallest(num1, num2, num3))
88.
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
n = 10
print("The", n, "th Fibonacci number is:", fibonacci(n))
89.
def sum_of_series(n):
return sum(1/i for i in range(1, n+1))
n=5
print("Sum of the series 1 + 1/2 + 1/3 + ... + 1/{} is:".format(n), sum_of_series(n))
90.
import string
def is_pangram(s):
alphabet = set(string.ascii_lowercase)
return set(s.lower()) >= alphabet
string_to_check = "The quick brown fox jumps over the lazy dog"
if is_pangram(string_to_check):
print("The string is a pangram")
else:
print("The string is not a pangram")
91.
def string_length(s):
return len(s)
def max_occurrence(s):
counter = Counter(s)
max_count = max(counter.values())
max_chars = [char for char, count in counter.items() if count == max_count]
return max_chars
def factorial(n):
return math.factorial(n)
number = 10
print("Factorial of", number, "is:", factorial(number))
95.
def count_digits(n):
if n == 0:
return 0
return 1 + count_digits(n // 10)
number = 12345
print("Number of digits in", number, "is:", count_digits(number))
96.
def average(lst):
return sum(lst) / len(lst)
my_list = [1, 2, 3, 4, 5]
print("Average of the numbers in the list is:", average(my_list))
97.
def prime_factors(n):
factors = []
divisor = 2
while n > 1:
while n % divisor == 0:
factors.append(divisor)
n //= divisor
divisor += 1
return factors
number = 60
print("Prime factors of", number, "are:", prime_factors(number))
98.
def intersection(set1, set2):
return set1.intersection(set2)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print("Intersection of sets:", intersection(set1, set2))
99.
def reverse_number(n):
rev = 0
while n > 0:
rev = rev * 10 + n % 10
n //= 10
return rev
number = 12345
print("Reverse of", number, "is:", reverse_number(number))
100.
import random
start = 1
end = 100
print("Random number between", start, "and", end, "is:", generate_random_number(start, end))