Anjali Python Practical File
Anjali Python Practical File
Solution:
OUTPUT :
Program 2: Write a program to find whether an inputted number is perfect or not.
Solution:
OUTPUT:
Program 3: Write a Program to check if the entered number is Armstrong or not.
Solution:
# Calculate the sum of each digit raised to the power of the number of digits
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)
OUTPUT:
Program 4: Write a Program to find factorial of the entered number.
Solution:
OUTPUT :
Program 5: Write a Program to enter the number of terms and to print the Fibonacci
Series.
Solution:
def fibonacci_series(n):
a, b = 0, 1
count = 0
a, b = b, a + b
count += 1
if num_terms <= 0:
else:
print("Fibonacci Series:")
fibonacci_series(num_terms)
OUTPUT:
Program 6: Write a Program to enter the string and to check if it’s palindrome or not using loop.
Solution :
# Function to check if a string is a palindrome
def is_palindrome(s):
# Initialize pointers at the start and end of the string
left = 0
right = len(s) - 1
Output :
Program 7: Write a Program to show the outputs based on entered list.
Solution:
# Function to calculate various outputs based on the entered list
def list_operations(numbers):
# Calculate the sum of the numbers
total_sum = sum(numbers)
Output :
Program 8: Write a Program to enter the numbers in a list using split () and to use all the functions
related to list.
Solution:
Output :
Program 9: Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order.
Solution:
Output :
Program 10: Write a Program to find factorial of entered number using user-defined modulefact().
Solution:
Step 1:
# fact_module.py
Output :
Program 11: Write a Program to enter the numbers and find Linear Search, Binary Search,
Lowest Number and Selection Sort using list/array code.
Solution:
def find_lowest_number(arr):
if len(arr) == 0:
return None
lowest = arr[0]
for num in arr:
if num < lowest:
lowest = num
return lowest
def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
Output :
Program 12: Write a Program to read data from data file and show Data File Handlingrelated
functions utility in python.
Solution:
from a file
def read_file(file_path):
try:
with open(file_path,
'r') as file:
data = file.read()
print("Data read
from file:")
print(data)
except
FileNotFoundError:
print("File not
found.")
a file
def write_file(file_path,
data):
as file:
file.write(data)
print("Data written to
file.")
to a file
def append_file(file_path,
data):
as file:
file.write(data)
print("Data appended
to file.")
def
read_file_by_line(file_pat
h):
try:
with open(file_path,
'r') as file:
print("Reading file
line by line:")
print(line,
end="")
except
FileNotFoundError:
print("File not
found.")
file_path = 'data.txt'
read_file(file_path)
file.\n"
write_file(file_path,
write_data)
# Append data to the file
append_data = "This is
appended text.\n"
append_file(file_path,
append_data)
line by line
read_file_by_line(file_pat
h)
Output :
Program 13: Write a Program to read data from data file in append mode and usewriteLines
function utility in python.
Solution:
# Read data from the file again to show the appended content
read_file(file_path)
#Program to read data from data file in
append modeaf=open("test.txt",'a')
here”,\“and another
line here”,\
af.writelines('\n' +
lines_of_text)af.close()
Output :
Program 14: Write a Program to read data from data file in read mode and count the
particular word occurrences in given string, number of times in python.
Solution:
try:
data = file.read()
data_lower = data.lower()
words = data_lower.split()
count = words.count(word_to_count.lower())
return count
except FileNotFoundError:
return 0
file_path = 'data.txt'
Solution:
# Optional: Read and print the content of the target file to verify
with open(target_file_path, 'r') as file:
print("Content of the target file:")
print(file.read())
Output :
Program 16: Write a Program to show MySQL database connectivity in python.
Solution:
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password='',db='school')
stmt=con.cursor()
stmt.execute(query)
data=stmt.fetchone()
print(data)
Program 17: Write a Python program to implement all basic operations of a stack, such as
adding element (PUSH operation), removing element (POP operation) and displaying the
stack elements (Traversal operation) using lists.
Solution:
my_stack.pop()
my_stack.display()
my_stack.pop()
my_stack.pop()
my_stack.pop() # This will show the stack is empty
Output :
Program 18: Write a program to display unique vowels present in the given word using
Stack.
Solution:
def unique_vowels(word):
stack = []
unique_vowels_set = set()
if char in vowels:
# If the character is a vowel and not already in the set, add it to the stack
unique_vowels_set.add(char)
stack.append(char)
while stack:
print()
unique_vowels(word)
Output :
Program 19: Write a program in Python to add, delete and display elements from a queue
using list.
Solution:
my_queue.dequeue()
my_queue.display()
my_queue.dequeue()
my_queue.dequeue()
my_queue.dequeue() # This will show the queue is empty
Output :
Program 20: Perform all the operations with reference to table ‘Employee’ through
MySQL-Python connectivity.
Solution:
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
sql = "CREATE TABLE EMP(empno integer primary key,ename varchar(25) not null,salary
float);"
cursor.execute(sql)
db1.close()
Inserting a record in ‘emp’
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
# Prepareing SQL statement to insert one record with the given values
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Fetching all the records from EMP table having salary more than 70000.
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store in
resultset
resultset = cursor.fetchall()
print (row)
except:
db1.close()
Updating record(s) of the table using UPDATE
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
#Preparing SQL statement to increase salary of all employees whose salary is less than
80000
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Deleting record(s) from table using DELETE
import MySQLdb
db1 = MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
Output:
1 record(s) deleted
>>>