Computer Practical (Python, MySQL)
Computer Practical (Python, MySQL)
APMAC
RPMA
KNO
ACADEM
REDGEI
GOA YAPUA
ACo-Educational Senior Secondary School
Session 2023-24
PRACTICAL FILE
COMPUTER SCIENCE
(PYTHON, MY SQL)
Materialized By. Subject Teacher
Name: ARYAN SINGH TOMAR MR. SANJAY
Class: 12TH MATHS [M-2) TRIPATHI
Roll No:
1
WAP to comjute At the power Bof 5
gvn Tuo ittgers AandB
2 WAP bcaleulat Compoundlntsrest
WAP Dacegpt Prcontage of a
3 Stdnt and display its grad 7
accordingly
4
WAP to print Fibonacciserics yp to 8
certain lnit
or not
6
WAP Daccgpta number andfrd out 10
whether itis aperfectnumber or not
WAP bprint the folowing patten:
13
WAP i Pthon to display those stings 7
which are sting with Aofgien list
WAP n Pyhon to fnd and digplay the
14 Sun of all the Values which are nding with 18
sfrom a list
17
WAP O Reada Textflt bns by bns and 21
25 TO 30
21 15 Queries
WAPto compute Ato the power Bof
1 given Two Integers Aand B
INPUT
1.py - CUsers/ABHISHEK/AppData/Local/Programs/Python/Python311/1.py (3.114)
File Edit Format Run Options Window Help
# Function to compute A to the power of B
def compute power (A, B) :
result = À ** B
return result
# Example usage
A = int (input ("Enter the base (A) : "))
B = int (input ("Enter the exponent (B) : "))
result = compute power (A, B)
print (f" {A} to the power of {B) is {result}")
OUTPUT
IDLE Shell 3.11.4
# Example usage
try:
percentage = float (input ("Enter the student's percentage: "))
if 0 <= percentage <= 100:
grade - calculate grade(percentage)
print (f"Grade: (grade)")
else:
print ("Invalid percentage. Please enter a percentage between 0 and 100. ")
except ValueError:
print ("Invalid input. Please enter a valid percentage. ")
OUTPUT
IDLE Shell 3.11,4
INPUT
2py - CUsers/ABHISHEK/AppData/Local/Programs/Python/Python311/2.py (3.114)
File Edit Format Run Options Window Help
# Function to calculate compound interest
def calculate compound interest (P, r, n, t):
A = P * (1 + r/n) ** (n*t) - P
return A
# Example usage
principal = float (input ("Enter the principal amount (P): "))
rate = float (input ("Enter the annual interest rate (as a decimal, r): "))
compounding frequency = int (input ("Enter the number of times interest is compounded per year (n): "))
time period = int (input ("Enter the number of years (t): "))
compound interest = calculate compound interest (principal, rate, compounding frequency, time period)
print (f"Compound Interest: (compound interest:.2f) ")
OUTPUT
IDLE Shell 3.11.4
INPUT
4py- C/Users/ABHISHEK/AppData/Local/Programs/Python/Python311/4.py(3.114)
File Edit Format Run Options Window Help
# Function to print Fibonacci series up to a given limit
def fibonacci (limit) :
fib series = [0, 1] # Initialize the series with the first two Fibonacci numbers
while fib series [-1] + fib series [-2] <= limit:
next num = fib series (-1] + fib series [-2]
fib series.append (next nun)
return fib series
# Example usage
limit = int (input ("Enter the limit for the Fibonacci series: "))
if linit < 0:
print ("Please enter a non-negative limit.")
else:
result = fibonacci (l imit)
print ("Eibonacci series up to the limit:")
print (result)
OUTPUT
IDLEShell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3. 11.4:d234 Oef, Jun 7 2023, 05:45:37)) [MSC v.1934 64 bit (
Type "help", "copyright", "credits" or "license () " for more information.
>>>
== RESTART: C:\Users\ABHISHEK\AppData\ Local\Programs\Python\Python311\4.py =
Enter the limit for the Fibonacci series: 1000
Fibonacci series up to the limit:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
>>>
WAPtoaccept a number, find and display whether
5. it's aArmstrong number or not
INPUT
5.py -C/Users/ABHISHEK/AppData/Local/Programs/Python/Python311/5.py (3.11.4)
File Edit Format Run Options WindowHelp
Function to check if a number is an Armstrong number
def is armstrong number (number) :
num str = str (number)
num digits = len (num str)
digit sum = sum (int (digit) ** num digits for digit in num str)
return nunber = digit sum
# Example usage
try:
num = int (input ("Enter a number to check if it's an Armstrong number: "))
if num < 0:
print ("Please enter a non-negative number. ")
else:
if is armstrong number (nm) :
print (f" {num) is an Armstrong number. ")
else:
print (f" {num) is not an Armstrong number. ")
except ValueError:
print (" Invalid input. Please enter a valid number. ")
OUTPUT
AIDLE Shell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3. 11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64
Type "help", "copyright", "credits" or "license () " for more information.
>>>
= RESTART: C:/0sers/ABHISHEK/AppData/Local/Programs/Python/ Python3 11/5.py
Enter a nunber to check if it's an Armstrong number: 43508
43508 is not an Armstrong number.
>>>
WAP toaccept a number and find out
6. whether it is a perfect number or not
INPUT
D 6py -C/Users/ABHISHEK/AppData/Local/Programs/Pythonr/Python311/6,py(3.114)
File Edit Format Run Options Window Help
# Function to check if a number is a perfect number
def is perfect number (number):
if nuber <= 0:
return False
# Example usage
try:
num = int (input ("Enter a number to check if it's a perfect number: "))
if num (= 0:
print ("Please enter a positive number. ")
else:
if is perfect number (num):
print (f" {num) is a perfect number. ")
else:
print (f" {num} is not a perfect number.")
except ValueError:
print ("Invalid input. Please enter a valid number. ")
OUTPUT
AIDLE Shell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4: d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit
Type "help", "copyright ", "credits" or "license ()" for more information.
>>>
= RESTART: C:/Users/ABHISHEK/AppData/Local/Programs/ Python/Python311/6.py
Enter a number to check if it's a perfect number: 876458
876458 is not a perfect number.
>>>
WAP toprint the following pattern:
7.
INPUT
7.py - C/Users/ABHISHEK/AppData/Local/Programs/Python/Python311/7-py (3.114)
File Edit Format Run Options Window Help
# Function to print the pattern
def print pattern (rows) :
for i in range (1, rows + 1):
for j in range (1, i + 1) :
if j < i:
print ("@", end=" ")
else:
print ("@")
# Example usage
rows = 3 # You can change this to the number of rows you want
print pattern (rows)
OUTPUT
IDLEShell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4: d2340ef, Jun 7 2023, 05:45:37) [MSC V.1934 64 bit
Type "help", "copyright", "creditsn or "license () " for more information.
>>>
= RESTART: C:/0sers/ABHISHEK/AppData/Local/Programs/Python/Python311/7.py
>>>||
8. WAPto accept a string and display
whether it is a Palindrome
INPUT
8.py -C/Users/ABHISHEK/AppData/Local/Programs/Python/Python311/8.py (3.114)
File Edit Format Run Options Window Help
# Function to check if a string is a palindrome
def is palindrome (input string) :
# Remove spaces and convert to lowercase to make it case-insensitive
cleaned string = input string.replace (" ", "").lower ()
# Compare the cleaned string with its reverse
return cleaned string =cleaned string [::-1]
#Example usage
user input = input ("Enter a string: ")
if is palindrome(user input) :
print ("The entered string is a palindrome.")
else:
print ("The entered string is not a palindrome. ")
OUTPUT
IDLE Shell 3.11.4
INPUT
9.py -C/Users/ABHISHEK/AppData/loca/Prograns/Python/Python311/9.py (3.114)
File Edit Format Run Options Window Help
Function to count various element.s in a string
def count elements in string(input_string) :
# Initialize counts
num alphabets = num digits = num uppercase = num lowercase = num spaces = num others = 0
return num alphabets, num digits, num uppercase, num lowercase, num spaces, nm others
Example usage
user_input input ("Enter a string: ")
alphabets, digits, uppercase, lowercase, spaces, others = count elements in string (user input)
OUTPUT
DIDLE Shell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4:d234Oef, Jun 7 2023, 05:45:37) [MSC v.1934 64 b.
Type "help", "copyright", "credits" or "license () " for more infornation.
>>>
= RESTART: C:/Users/ABHISHEK/AppData/Local/Programs/Python/Python311/9.py
Enter a string: 2265 6RPM
Alphabets: 3
Digits: 5
Uppercase letters: 3
Lowercase letters: 0
Spaces: 0
Other characters: 0
>>>
WAP toaccept a string (a sentence) and returns a
10. string having First Letter of each word in Capital
Letter
INPUT
10.py - C/Users/ABHISHEKJAppData/Local/Programs/Python/Python311/10.py (3.11.4)
File Edit Format Run Options Window Help
# Function to capitalize the first letter of each word in a sentence
def capitalize first letter (sentence):
words = sentence. split () # Split the sentence into words
capitalized words = [word.capitalize () for word in words] # Capitalize the first letter of each word
result ='-join(capitalized words) # Join the words back into a sentence
return result
Example usage
user input = input ("Enter a sentence: ")
capitalized sentence = capitalize first letter (user input)
print ("Capitalized sentence:", capitalized sentence)
OUTPUT
AIDLE Shell3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit
Type "help", "copyright", "credits" or "license () " for more information.
>>>
RESTART : C:/Users/ABHISHEK/AppData/Local/Programs/Python/Python3 11/10.py
Enter a sentence: dark phoenix
Capitalized sentence: Dark Phoenix
1. WAP tofrom
remove allOdd Numbers
the given list
INPUT
11py - CAUsers\ABHISHEK;AppData\Local\ Programs\ Python\Python311\11.py (3.11.4)
File Edit Format Run Options Window Help
# Function to remove odd numbers from a list
def remove odd numbers (input list) :
# Use a list comprehension to create a new list with even numbers only
result list = [x for x in input list if x % 2 = 0]
return result list
# Example usage
input list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = remove odd nunbers (input list)
print ("Original1 list:", input list)
print ("List with odd numbers removed: ", result)
OUTPUT
IDLE Shell 3.11.4
INPUT
Þ12py - CAUsers\ABHISHEKVAppData\Local\ Programs\Python\Python311\12.py (3.1
File Edit Format Run Options Window Help
# Function to find the second largest element in a list
def find second largest (input list):
if len (input list) < 2:
return "List should have at least two elements"
Example usage
input list = [12, 56, 34, 78, 90, 42, 55]
result = findsecond largest (input list)
print ("List:", input list)
print ("Second largest element:", result)
OUTPUT
IDLE Shell 3.11.4
INPUT
13.py - CAUsers\ABHISHEKVAppData\Local\Programs\Python\ Python31 1\13,py (3.11.4)
File Edit Format Run Options Window Help
#Function to display strings containing 'A' from a list
def find strings with A(input list) :
result list = [string for string in input list if 'A' in string]
return result list
# Example usage
input list = ("Apple", "Banana", "Cherry", "Date", "Eig"]
result = find strings with A (input list)
print ("Original list:", input list)
print ("Strings containing 'A':", result)
OUTPUT
IDLEShell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4:d234 0ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit
Type "help", "copyright", "credits" or "license () " for more information.
= RESTART: C:\Users\ABHISHEK\AppData\Local\Programs\Python\Python311\13.py
Original list: 'Apple', 'Banana', Cherry', 'Date', 'Eig']
Strings containing 'A': ['Apple']
>>>||
WAP in Python to find and display the Sum of all the Values
14. which areending with 3fromna
list
INPUT
14py- CAUsers\ABHISHEK\AppData\Local\ Programs\PythonPython311\14.py (3.114)
File Edit Format Run Options Window Help
# Function to find and display the sum of values ending with '3
def sum values ending with 3(input list) :
total = 0
for value in input list:
if str (value) .endswith('3'):
total t= value
return total
# Example usage
input list = [13, 23, 33, 42, 53, 63, 73, 83, 93]
result =sum values ending with 3 (input list)
print ("List:", input list)
print ("Sum of values ending with '3':", result)
OUTPUT
AIDLE Shell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4:d234Oef, Jun 7 2023, 05:45:37) [MSC v.1934 64
Type "help", "copyright", "credits" or "license () " for more information.
= RESTART: C:\Users\ABHISHEK\AppData\Local\ Programs\Python\Python3 11\ 14-py
List: [13, 23, 33, 42, 53, 63, 73, 83, 93]
Sum of values ending with '3': 434
>>>
15. WAP to accept values fronm user
and Create a Tuple
INPUT
15.py - CAUsers\ABHISHEKAppData\l.ocal\ Programs\Python\ Python311\15.py(3.11.4)
File Edit Format Run Options Window Help
# Function to accept values from the user and create a tuple
def create tuple from user input ():
values = 0 # Initialize an empty list to store the values
while True:
user input = input ("Enter a value (or 'q' to quit): ")
if user input.lower() == 'g':
break #Exit the loop if the user enters g'
try:
value = int (user input) # You can change int to the appropriate data type
values.append (value)
except ValueError:
print (" Invalid input. Please enter a valid value. ")
# Example usage
user tuple = create tuple from user input()
print ("User input tuple:", user tuple)
OUTPUT
AIDLEShell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC
Type "help", "copyright", "credits" or "license () " for more info
>>>
= RESTART: C:\Users\ABHISHEK\AppData\ Local\Programs\ Python\Pytho
Enter a value (or 'g' to quit): 100
Enter a value (or 'g' to quit): 200
Enter a value (or 'g' to quit) : 300
Enter a value (or g' to quit) : 400
Enter a value (or 'g' to quit) : 500
Enter a value (or 'g' to quit): 600
Enter a value (or 'g'700to quit) :
Enter a value (or 'g'800to quit):
Enter a value (or 'g'900to quit) :
Enter a value (or 'g' to quit): g
User input tuple: (100, 200, 300, 400, 500, 600, 700, 800, 900)
>>>
WAP to createa Simple Calculator for performing the
different arithmetical operations, such as Addition,
Subtraction, Multiplication, and Division using function
INPUT
16.py- CAUsers\ABHISHEK\AppData\Local\ Programs\Python\Python3
while True :
|File Edit Format Run Options Window Help
# Function to perform addition
user input = input (": ")
def add (x, y): if user input == "exit":
return x + y break
OUTPUT
IDLE Shell 3.1 1.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4: d2340ef, Jun 7 2023, 05:45:37) (MSC v.1934 64 E
Type "help", "copyright", "credits" or "license ()" for more information.
>>>
INPUT
17py- CAUsers\ABHISHEKVAPpData\L.ocal\Programs\Python\Python311\17.py (3.114)
File Edit Format Run Options Window Help
# Function to read a text file line by line and display words separated by '#"
def display words with separator (filename):
try:
with open (filename, 'r') as file:
for line in file:
words = line.strip() .split ()
formatted line = '#"-join(words)
print (formatted line)
except FileNotFoundError:
print ("Eile not found: ", filename)
Example usage
file name = '11.py' # Replace with the path to your text file
display_words with separator (filename)
OUTPUT
IDLE Shell 3.11.4
File Edit Shell Debug Options Window Help
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v. 1934 64 1
Type "he lp", "copyright", "credits" or "license () " for more information.
>>>
= RESTART: C:\Users\ABAISHEK\AppData\Local\ Programs\Python\Python3 11\17.py
##Function#to#remove#odd#numbers#from#a#list
def#remove odd numbers (input list) :
##Use #a#list#comprehension#to#create#aanew#list #withteven#numbers#only
result list#=# [x#for#x*in#input list#if#x#%#2#==#0]
return#result list
##Exanple #usage
input list#=#[1,#2, #3, #4,#5, #6, #7,#8, #9,#10]
result#=#*remove odd numbers (input list)
print ("Original#list: ", #input list)
print ("List#withtodd#numbers#removed: ", #result)
>>>
WAPtoget Imply Datafrom the user
18. and Write on toa Binary File called
"emp.dat"
INPUT
18,py - CAUsers\ABHISHEKAppData\Local\ Programs\Python\Python311\18.py (3.11.4)
File Edit Format Run Options Window Help
import struct
OUTPUT
IDLE Shell 3.11.4
INPUT
19.py - CAUsers\ABHISHEKVAppData\Local\Programs\Python\Python311\19,py(3.11.4)
File Edit Format Run Options Window Help
# Function to get student details
def get student details() :
student data = U
for i in range (5) :
print (f"Enter details for Student (i + 1):")
roll no = input ("Roll Number: ")
name = input ("Name: ")
marks = input ("Marks: ")
student data.append(f"{roll no}, (name), {marks) \n")
return student data
INPUT
20,py - CAUsers\ABHISHEKVAppData\local\Programs\Python\Python311\20,py (3.114)
File Edit Format Run Options Window Help
Function to count various characters in a string
def count characters(string):
Initialize counters
num alphabets = 0
num digits = 0
num uppercase 0
num lOwercase = 0
num spaces = 0
elif char.islower () :
num lowercase t= 1
elif char. isdigit () :
num digits += 1
elif char.isspace ()
num spaces t= 1
return num alphabets, num digits, num uppercase, num lowercase, num spaces
# Input string
input string "Dark Phoenix"
# Call the function to count characters
result = countcharacters (input string)
# Display the results
print ("Alphabets:", result [0])
PELnt D1g1ts: resuit|1)
result [2])
Drine Ower result[3] )
EtCase
print ("Spaces:", result [41)
OUTPUT
IDLE Shell 3.11.4
My SQL
(15 QUERIES )
Command For
1. Creating a Database
mysql> create database hospital;
Query OK, 1 row affected (0.17 sec)
5. mserting Data
mysql> insert ito clinic values (101, "Amit singh", "cardiologist", 99000, 38);
Query OK, 1 row affected (0.59 sec)
Mohan singh
Trisha pandey
| Manoj tripathi |
B rows in set (0.00 sec)
9. using Alter
ysql> alter table clinic add phoneNo int;
uery OK, rows affected (8.65 sec)
ecords: e Duplicates: Warnings:
10. Using 'Order By' clause
selectfrom clinic order by name, age;
name | field | salary | age |
Amit singh | cardiologist 98000 38
Krishna sinha | dentist 55000 32
Manoj trripathi | radiologist 120000 49
Mohan singh | neurologist 100000 41
Trisha pandey | surgeon 98000 | 45
max(salary) |
-+
120000
-+
1 row in set (0.00 sec)
min(salary) |
+
55000 |
+
| Krishna sinha |
1 row in set (1.59 sec)
mysql>