Problems
a = int(input("Enter coefficient of x^2: "))
b = int(input("Enter coefficient of x: "))
c = int(input("Enter constant: "))
print(f"Your eqn. is: {a}x^2 + {b}x + {c}")
d = (b**2) - (4*a*c)
if (d < 0):
print("Real roots does not exist")
else:
x1 = ((-b) + (d**0.5)) / (2 * a)
x2 = ((-b) - (d**0.5)) / (2 * a)
print("First sol. is: " , x1)
print("Second sol. is: " , x2)
n = int(input("Enter a number to check prime: "))
if (n<0):
print("It is a negtaive number")
elif (n==0):
print("0 is not a prime number")
elif (n==1):
print("1 is not a prime number")
else:
l = []
for i in range(2 , n+1):
if (n%i == 0):
l.append(i)
if (l[0] == n):
print('It is a prime number')
else:
print("It is not a prime number")
n = int(input("Enter a number till where you want all prime number: "))
z = {}
for i in range(2,n+1):
for j in range (2,i+1):
l = []
if (i%j == 0):
l.append(j)
if (l[0]==j):
z.append(i)
print(z)
def is_prime(x):
if x<2:
return False
for i in range(2,int(x**0.5)+1):
if x%i==0:
return False
return True
def first_n_primes(n):
primes=[]
num=2
while len(primes)<n:
if is_prime(num):
primes.append(num)
num+=1
return primes
n=int(input("Enter number of primes to generate: "))
print(first_n_primes(n))
rows = int(input("Enter number of rows: "))
for i in range(1, rows+1):
print(" "*(rows-i) + "*"*(2*i-1))
for i in range(rows-1, 0, -1):
print(" "*(rows-i) + "*"*(2*i-1))
c. if the character is a numeric digit, prints its name in text (e.g., if input is 9, output is NINE)
def analyze_character(char):
# Ensure only a single character is processed
if len(char) != 1:
print("Error: Please enter exactly one character.")
return
print(f"Input Character: '{char}'")
print("-" * 30)
# a. Print whether the character is a letter, numeric digit, or special character.
if char.isalpha():
print("**Type:** Letter")
# b. If the character is a letter, print whether it is uppercase or lowercase.
if char.isupper():
print("**Case:** Uppercase")
elif char.islower():
print("**Case:** Lowercase")
elif char.isdigit():
print("**Type:** Numeric Digit")
# c. If the character is a numeric digit, print its name in text.
digit_names = {
'0': 'ZERO', '1': 'ONE', '2': 'TWO', '3': 'THREE', '4': 'FOUR',
'5': 'FIVE', '6': 'SIX', '7': 'SEVEN', '8': 'EIGHT', '9': 'NINE'
}
print(f"**Name in Text:** {digit_names.get(char, 'UNKNOWN DIGIT')}")
else:
print("**Type:** Special Character")
# --- Example Usage ---
# Get input from the user
user_input = input("Enter a single character: ")
# Call the function to analyze the input
analyze_character(user_input)
para = str(input("Enter string: "))
char = str(input("Enter char to be searched: "))
count = 0
for k in para:
if (k==char):
count += 1
print("No. of occurence of" , char , "is" , count)
para = str(input("Enter string: "))
char1 = str(input("Enter char to be replaced: "))
char2 = str(input("Enter new char you want: "))
print(para.replace(char1 , char2))
para = str(input("Enter string: "))
char = str(input("Enter char whose first occurence to be removed: "))
print(para.replace(char , ""))
para = str(input("Enter string: "))
char = str(input("Enter char whose first occurence to be removed: "))
print(para.replace(char , ""))
s1 = input("Enter first string: ")
s2 = input("Enter second string: ")
n = int(input("Enter number of characters to swap: "))
s1_new = s2[:n] + s1[n:]
s2_new = s1[:n] + s2[n:]
print("After swapping:")
print("String 1:", s1_new)
print("String 2:", s2_new)
7. Write a function that accepts two strings and returns the indices of all the occurrences of the second string in the first string as a list. If the second string is not present in the first string then it should return -1
def find_all_occurrences(s, sub):
indices = []
start = 0
while True:
index = s.find(sub, start)
if index == -1:
break
indices.append(index)
start = index + 1
return indices if indices else -1
print(find_all_occurrences("banana", "ana"))
8. Write a program to create a list of the cubes of only the even integers appearing in the input list (may have elements of other types also)
lst = [1,2,3,4,5,6,"a",8.5]
cubes = []
for i in lst:
if isinstance(i,int) and i%2==0:
cubes.append(i**3)
print("Cubes using loop:", cubes)
cubes2 = [i**3 for i in lst if isinstance(i,int) and i%2==0]
print("Cubes using comprehension:", cubes2)
import string
with open("new.txt") as f:
data = f.read()
words = data.split()
lines = data.split("\n")
count = 0
count2 = 0
count3 = 0
for i in data:
if i in string.ascii_letters:
count+=1
for j in words:
count2 +=1
for line in lines:
count3 +=1
print("No. of characters is " , count)
print("No. of words is " , count2)
print("No. of lines is " , count3)
b. Calculate the frequency of each character in the file. Use a variable of dictionary type to maintain the count.
with open("new.txt") as f:
data = f.read()
dict = {}
for i in data:
dict[i] = dict.get(i ,0 ) + 1
print(dict)
```
# c. Print the words in reverse order.
```python
with open("new.txt") as f:
data = f.read()
words = data.split()
print(words[::-1])
with open("new.txt", "r") as f, \
open("file1.txt", "w") as f1, \
open("file2.txt", "w") as f2:
for i, line in enumerate(f, start=1):
if i % 2 == 0: # even line
f1.write(line)
else: # odd line
f2.write(line)
print("Lines copied")
10. Write a program to define a class Point with coordinates x and y as attributes. Create relevant methods and print the objects. Also define a method distance to calculate the distance between any two point objects.
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"Point({self.x}, {self.y})"
def distance(self, other):
return math.sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
p1 = Point(2, 3)
p2 = Point(5, 7)
print(p1)
print(p2)
print("Distance:", p1.distance(p2))
11. Write a function that prints a dictionary where the keys are numbers between 1 and 5 and the values are cubes of the keys.
def cube_dict():
d = {i: i**3 for i in range(1,6)}
print(d)
cube_dict()
12. Consider a tuple t1=(1, 2, 5, 7, 9, 2, 4, 6, 8, 10). Write a program to perform following operations:
t1=(1,2,5,7,9,2,4,6,8,10)
half = len(t1)//2
print(t1[:half])
print(t1[half:])
even_tuple = tuple(i for i in t1 if i%2==0)
print(even_tuple)
t2=(11,13,15)
print(t1+t2)
print("Max:", max(t1))
print("Min:", min(t1))
13. Write a program to accept a name from a user. Raise and handle appropriate exception(s) if the text entered by the user contains digits and/or special characters.
class InvalidNameError(Exception):
"""Custom exception for invalid names"""
pass
def validate_name(name):
if not name.isalpha():
raise InvalidNameError("Name must contain only alphabets (no digits/special characters).")
return name
try:
user_input = input("Enter your name: ")
valid_name = validate_name(user_input)
print(f"Hello, {valid_name}!")
except InvalidNameError as e:
print("Error:", e)