PYTHON PRACTICAL SET-A

PYTHON PRACTICAL SET-A



Q1.  PYTHON : (8 marks)

(i)             Write a program to check if given string is palindrome or not.   



Answer:                          

# Program to check if a string is a palindrome
def is_palindrome(s: str) -> bool:
    s = s.lower().replace(" ", "")  # Normalize the string (ignore case and spaces)
    return s == s[::-1]

# Test the program
string = input("Enter a string: ")
if is_palindrome(string):
    print(f"'{string}' is a palindrome.")
else:
    print(f"'{string}' is not a palindrome.")

ii)Write a program to read from a text file named “poem.txt” and display lines which starts with character “s”.       

Answer:

# Program to read and display lines starting with 's'
def display_lines_starting_with_s(filename: str):
    file = open(filename, "r")
    print("Lines starting with 's':")
    for line in file:
        if line.strip().lower().startswith('s'):
            print(line.strip())
    file.close()

# Call the function
display_lines_starting_with_s("poem.txt")


                                                                                                                                          


Q.2)








(i) Create the Table and Insert Data

Answer:

CREATE TABLE GRADUATE ( SNO INT PRIMARY KEY, NAME VARCHAR(50), STIPEND INT, SUBJECT VARCHAR(50), AVERAGE INT, DIVISION VARCHAR(5) ); INSERT INTO GRADUATE (SNO, NAME, STIPEND, SUBJECT, AVERAGE, DIVISION) VALUES (1, 'KARAN', 400, 'PHYSICS', 68, 'I'), (2, 'DIWAKAR', 450, 'CHEMISTRY', 68, 'I'), (3, 'DIVYA', 300, 'CHEMISTRY', 62, 'I'), (4, 'REKHA', 350, 'PHYSICS', 63, 'I'), (5, 'ARJUN', 500, 'MATHS', 70, 'I');




(ii) Display Names and Subjects of Students Whose Stipend is More Than 400 and Average Greater Than 65



Answer:

SELECT NAME, SUBJECT FROM GRADUATE WHERE STIPEND > 400 AND AVERAGE > 65;

(iii) Increase the Average of All Physics Students by 5

Answer:

UPDATE GRADUATE SET AVERAGE = AVERAGE + 5 WHERE SUBJECT = 'PHYSICS';

(iv) Add a New Column GRADE of Type CHAR(2)

Answer:


ALTER TABLE GRADUATE ADD GRADE CHAR(2);
PYTHON PRACTICAL SET-A PYTHON PRACTICAL SET-A Reviewed by Shubham Prajapati on January 31, 2025 Rating: 5

No comments:

If you have any doubt so you can comment me then i will 100% help you ,Through comment Chat

Powered by Blogger.