PYTHON PROGRAMS
1. smallest of 3 numbers by user input
2. fine the given umber is positive negative or zero
3. find numbers which are divisible by 7 and 11 bet 100 and 1000 using for loop
4. find the number is Armstrong or not
5. find number is palindrome or not
6. find the roots of quadratic equation
7. square root
8. area of triangle when 3 sides are given
9. display prime number bet 1 and 100
10. sum of digits of a single number
11. perform arithmetic operation using function. a menu used for display operation
12. count no of vowels and consonants in given string
13. no of words in sentence
14. string is palindrome or not
15. convert first and last letter uppercase
16. remove duplicate from given string
17. find frequency of each character in a string
18. find second smallest number from list
19. sorting without built-in function
20. store mark of 20 studs. find sum and average without using build in function
21. find digits in even position of the number
22. program to validate mobile number and email
23. create DF to store details of 5 books
24. python code t create re expression to match with any 3digit number between 100 to 999
ANSWERS:
1. def smallest_of_three(a, b, c):
return min(a, b, c)
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
smallest = smallest_of_three(num1, num2, num3)
print(f"The smallest number is: {smallest}")
2. def check_number(num):
if num > 0:
print("The number is Positive.")
elif num < 0:
print("The number is Negative.")
else:
print("The number is Zero.")
number = float(input("Enter a number: "))
check_number(number)
3. divisible_by_7 = []
divisible_by_11 = []
for num in range(100, 1001):
if num % 7 == 0:
divisible_by_7.append(num)
if num % 11 == 0:
divisible_by_11.append(num)
print("Numbers divisible by 7 between 100 and 1000:")
print(divisible_by_7)
print("\nNumbers divisible by 11 between 100 and 1000:")
print(divisible_by_11)
4. def is_armstrong(number):
num_str = str(number)
num_digits = len(num_str)
total = sum(int(digit) ** num_digits for digit in num_str)
if total == number:
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is NOT an Armstrong number.")
num = int(input("Enter a number to check if it's an Armstrong number: "))
is_armstrong(num)
5. def is_palindrome_number(number):
num_str = str(number)
if num_str == num_str[::-1]:
print(f"{number} is a Palindrome number.")
else:
print(f"{number} is NOT a Palindrome number.")
num = int(input("Enter a number to check if it's a Palindrome: "))
is_palindrome_number(num)
6. ax2+bx+c=0
import cmath
def find_roots(a, b, c):
# Calculate discriminant
d = (b ** 2) - (4 * a * c)
root1 = (-b + cmath.sqrt(d)) / (2 * a)
root2 = (-b - cmath.sqrt(d)) / (2 * a)
print(f"The roots of the quadratic equation are: {root1} and {root2}")
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
if a == 0:
print("This is not a quadratic equation (a cannot be 0).")
else:
find_roots(a, b, c)
7. import math
num = float(input("Enter a number to find its square root: "))
if num >= 0:
sqrt = math.sqrt(num)
print(f"The square root of {num} is {sqrt}")
else:
print("Square root of a negative number is not real.")
8. Area=s(s−a)(s−b)(s−c)
import math
a = float(input("Enter length of side a: "))
b = float(input("Enter length of side b: "))
c = float(input("Enter length of side c: "))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print(f"The area of the triangle is {area}")
9. for num in range(2, 101):
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)
10. num = int(input("Enter a number: "))
sum_of_digits = 0
while num > 0:
sum_of_digits += num % 10
num //= 10
print(f"The sum of digits is {sum_of_digits}")
11. def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error! Division by zero."
def menu():
print("\nSelect an Arithmetic Operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
while True:
menu()
choice = input("Enter your choice (1-5): ")
if choice == '5':
print("Exiting the program. Goodbye!")
break
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"Result: {add(num1, num2)}")
elif choice == '2':
print(f"Result: {subtract(num1, num2)}")
elif choice == '3':
print(f"Result: {multiply(num1, num2)}")
elif choice == '4':
print(f"Result: {divide(num1, num2)}")
else:
print("Invalid choice! Please select from 1 to 5.")
12. def count_vowels_consonants(text):
vowels = "aeiouAEIOU"
vowel_count = 0
consonant_count = 0
for char in text:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
print(f"Number of vowels: {vowel_count}")
print(f"Number of consonants: {consonant_count}")
user_input = input("Enter a string: ")
count_vowels_consonants(user_input)
13. def count_words(sentence):
words = sentence.split()
print(f"Number of words in the sentence: {len(words)}")
user_input = input("Enter a sentence: ")
count_words(user_input)
14. def is_palindrome(text):
text = text.replace(" ", "").lower()
if text == text[::-1]:
print("The string is a palindrome.")
else:
print("The string is not a palindrome.")
user_input = input("Enter a string: ")
is_palindrome(user_input)
15. def convert_first_last_upper(text):
if len(text) < 1:
print("Empty string provided.")
elif len(text) == 1:
print(text.upper())
else:
modified_text = text[0].upper() + text[1:-1] + text[-1].upper()
print(f"Modified string: {modified_text}")
user_input = input("Enter a string: ")
convert_first_last_upper(user_input)
16. def remove_duplicates(text):
result = ""
for char in text:
if char not in result:
result += char
print(f"String after removing duplicates: {result}")
user_input = input("Enter a string: ")
remove_duplicates(user_input)
17. def char_frequency(text):
frequency = {}
for char in text:
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
print("Character Frequency:")
for char, count in frequency.items():
print(f"'{char}': {count}")
user_input = input("Enter a string: ")
char_frequency(user_input)
18. def second_smallest(numbers):
unique_numbers = list(set(numbers))
if len(unique_numbers) < 2:
print("List doesn't have enough unique numbers.")
return
unique_numbers.sort()
print(f"The second smallest number is: {unique_numbers[1]}")
user_input = input("Enter numbers separated by spaces: ")
numbers_list = list(map(int, user_input.split()))
second_smallest(numbers_list)
19. def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
user_input = input()
numbers = list(map(int, user_input.split()))
bubble_sort(numbers)
print(numbers)
20. marks = []
print("Enter marks for 20 students:")
for i in range(20):
mark = float(input(f"Enter mark for student {i + 1}: "))
marks.append(mark)
total = 0
for mark in marks:
total += mark
average = total / len(marks)
print(f"\nTotal Marks: {total}")
print(f"Average Marks: {average}")
21. number = input()
even_position_digits = ""
for i in range(len(number)):
if (i + 1) % 2 == 0:
even_position_digits += number[i]
print(even_position_digits)
22. import re
def validate_mobile(number):
pattern = r'^[6-9]\d{9}$'
if re.match(pattern, number):
print("Valid mobile number")
else:
print("Invalid mobile number")
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if re.match(pattern, email):
print("Valid email address")
else:
print("Invalid email address")
mobile = input(“enter mobile number:”)
email = input(“enter email address:”)
validate_mobile(mobile)
validate_email(email)
23. import pandas as pd
data = {
'ID': [1, 2, 3, 4, 5],
'Name': ['Book A', 'Book B', 'Book C', 'Book D', 'Book E'],
'Author': ['Author A', 'Author B', 'Author C', 'Author D', 'Author E'],
'Price': [150, 200, 180, 220, 170]
df = pd.DataFrame(data)
print(df)
24.