Python Practical MCA
Python Practical MCA
(SESSION :- 2023-2025)
PRACTICLE FILE OF python programming
SUBMITTED TO :- SUBMITTED BY :-
ROLL NO. :- 05
9. Create a binary file with name and roll no. Search for a
given roll number and display the name, if not found
display appropriate message.
output:
Addition: 13.0
Subtraction: 7.0
Multiplication: 30.0
Division: 3.3333333333333335
Floor Division: 3.0
Modulus: 1.0
2. Write a Program to check if the entered number is Armstrong or not:
def is_armstrong(number):
# Convert number to string to get its length
num_str = str(number)
num_digits = len(num_str)
# Calculate the sum of digits raised to the power of the number of digits
armstrong_sum = sum(int(digit)**num_digits for digit in num_str)
Output:
153 is an Armstrong number.
3. Write a Program to find factorial of the entered number.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
# Calculate factorial
result = factorial(number)
Enter a number: 5
Output:
Factorial of 5 is: 120
4. Write a Program to enter the number of terms and to print the Fibonacci Series:
def fibonacci_series(n):
fib_series = [0, 1] # Initialize Fibonacci series with first two terms
return fib_series
Output:
Fibonacci Series up to 10 terms:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
5. Write a Program to enter the string and to check if it’s palindrome or not using loop:
def is_palindrome(string):
# Convert string to lowercase and remove spaces
string = string.lower().replace(" ", "")
Output:
racecar is a palindrome.
6. Write a program to draw a pie chart for four part with lable using Matplotlib packages:
# Adding a title
plt.title('Pie Chart for Four Parts')
# Remove lines containing the character from input file and write to output file
remove_lines_with_char(input_filename, char_to_remove, output_filename)
print("Lines containing the character '{}' removed from '{}' and written to
'{}'.".format(char_to_remove, input_filename, output_filename))
Output:
Lines containing the character 'a' removed from 'input.txt' and written to 'output.txt'.
8. Read a text file and display the number of vowels/consonants/uppercase/lowercase
characters in the file:
def count_characters(filename):
vowels = 'aeiouAEIOU'
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
# Initialize counters
num_vowels = 0
num_consonants = 0
num_uppercase = 0
num_lowercase = 0
# Input filename
filename = 'text_file.txt'
# Count characters
num_vowels, num_consonants, num_uppercase, num_lowercase =
count_characters(filename)
Output:
Number of vowels: 10
Number of consonants: 16
Number of uppercase characters: 4
Number of lowercase characters: 26
9. Create a binary file with name and roll no. Search for a given roll number and display
the name, if not found display appropriate message:
import pickle
Suppose the binary file student_data.bin is created with the following data:
{'Alice': 101, 'Bob': 102, 'Charlie': 103, 'David': 104}
Output:
Enter roll number to search: 102
Name for roll number 102: Bob
10. Write a random number generator that generates random numbers between 1 and
6(simulates a dice):
import random
def roll_dice():
return random.randint(1, 6)
Output:
The dice rolled: 4
11. Create a student table and insert data. Implement the following SQL commands on
the student table:
ALTER table to add new attributes / modify data type / drop attribute
UPDATE table to modify data
ORDER By to display data in ascending / descending order
DELETE to remove tuple(s)
GROUP BY and find the min, max, sum, count and average:
import sqlite3
execute_sql_command('''
INSERT INTO student (name, age, score) VALUES
('Alice', 20, 85.5),
('Bob', 22, 78.2),
('Charlie', 21, 90.0),
('David', 23, 92.5),
('Eve', 19, 88.7)
''')
# Drop attribute
execute_sql_command('ALTER TABLE student DROP COLUMN score')
# Update table to modify data
execute_sql_command("UPDATE student SET age = 20 WHERE name = 'David'")
# Delete tuple(s)
execute_sql_command("DELETE FROM student WHERE name = 'Eve'")
Statistics by gender:
Gender: Female
Minimum age: 19
Maximum age: 20
Sum of ages: 39
Count of students: 2
Average age: 19.5
Gender: Male
Minimum age: 20
Maximum age: 22
Sum of ages: 63
Count of students: 3
Average age: 21.0
12. Integrate SQL with Python by importing the MySQL module, pymysql module:
import pymysql
# Commit changes
connection.commit()
# Delete tuple(s)
delete_data_query = '''
DELETE FROM student WHERE name = 'Eve'
'''
cursor.execute(delete_data_query)
# Close connection
connection.close()