You are on page 1of 30

ACADENIY, C

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:

Mr. Ravi Srivastav


Principal
Index
PROJECT OBJECTIVE DATE PAGE NO. SIGN

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

WAP toacegpta number, find and


5 dlisplay whether ts afArmstrong nunber 9

or not

6
WAP Daccgpta number andfrd out 10
whether itis aperfectnumber or not
WAP bprint the folowing patten:

8 WAP to accqptastrig and display 12


whether itis a Palndrome
PROJECT OBJECTIVE DATE PAGE NO. SIGN

WAP that ownts the nunber of alphabes and


9 digits, ybpercase letrs, lowercase eter, 13

sbaces ndother charactrs in the sting enitred


WAP b accgpta string (a sontnce) and
10 retuns a string having Tirstletr of ach
word lh Capital leter
11
WAP to renove all Oddnunbers from the 15
gin ist

12 WAP ODisplay SccondLargestSlement 16


fagion list

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

15 WAP baccqpt valucs from user and 19


Creatt a Tple
WAP b reat a Simple Caleulator for
16
poforming the differontarimatical 20
qperations, such as Aaalton, Subtraction,
Multiplication, and Diuision using function
PROJECT OBJECTIVE DATE PAGE NO. SIGN

17
WAP O Reada Textflt bns by bns and 21

Digplay cach word sqparatdbya e


WAP oget lmply Datafrom the user
18 andWrtt on to a Binary File called 22

WAP Dgt Roll no, Name and Marks


19 fany fe studontof aclass and store 23

these detais ina File called 'marks. text"

WAP Count thenumber of Aphabets,


20 Digi's, typorcase letr, owercase 24

letr and Space in the gwn sting

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

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 bi
Type "help", "copyright", "credits" or "license () " for more information.
>>>

= RESTART: C:/Users/ABHISHEK/AppData/ILocal/Programs/Python/ Python311/1.py


Enter the base (A): 5
Enter the exponent (B): 7
5 to the power of 7 is 78125
>>>|
WAP to accept Percentage of a
3. Student and display its grade
accordingly
INPUT
3.py- CUsers/ABHISHEK/AppData/Local/Programs/Python/Python311/3.py (3.114)
File Edit Format Run Options Window Help
# Function to determine and display the grade
|def calculate grade (percentage) :
if percentage = 90:
return 'A+
elif percentage >= 80:
return 'A!
elif percentage >= 70:
return 'B
elif percentage = 60:
return 'c!
elif percentage >= 50:
return 'D'
else:
return 'F!

# 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

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 bit
Type "help", "copyright", "credits" or "license () " for more information.
>>>
= RESTART: C:/Users/ABHISHEK/AppData/Local/ Programs/Python/ Python3 11/3 -py
Enter the student's percentage: 98
Grade: A+
>>>
2. WAP ToCalculate Compound
Interest

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

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/2.py
Enter the principal amount (P) : 23
Enter the annual interest rate (as a decimal, r): 32
Enter the number of times interest is compounded per year (n) : 4
Enter the number of years (t): 5
Compound Interest: 279626305558309371904.00
>>>
4. WAP to print Fibonacci series up
tocertain limit

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

proper divisors = [1] #1 is always a proper divisor

for i in range (2, number // 2 + 1):


if number % i = 0:
proper_divisors . append (i)
divisor sum = sun (proper divisors)
return number = divisor sum

# 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

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 t
Type "help", "copyright", "credits" or "license () " for more informat.ion.
>>>
= RESTART: C:/Users/ABAISHEK/AppData/Local/Programs/Python/ Python311/8 -py
Enter a string: 4367586
The entered string is not a palindrome.
>>>||
WAPthat counts the number of alphabets and
. digits,uppercase letters, lowercase letter, spaces
and other characters in the string entered

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

for char in input string:


if char.isalpha () :
num alphabets t= 1
if char. isupper () :
num ppercase += 1
elif char.islower () :
num lowercase t= 1
elif char.isdigit () :
num digits += 1
elif char. isspace () :
num spaces t= 1
else:
num others += 1

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)

print ("Alphabets:", alphabets)


print ("Digits:", digits)
print ("Uppercase letters:", uppercase)
print ("Lowercase letters:", lowercase)
print ("Spaces:" spaces)
print ("other characters:", others)

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

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\Python311\ 11.py


Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List with odd numbers removed: [2, 4, 6, 8, 10]
>>>||
12. WAP toDisplay Second Largest
Element of a given list

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"

largest = second largest = float ('-inf')


for num in input list:
if num > largest:
second largest = largest
largest = nm
elif num > second largest and num != largest:
second largest = num

if second largest == float ('-inf') :


return "There is no second largest element"

return second largest

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

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 bi
Type "help", "copyright", "credits" or "license () " for more information.
>>>
= RESTART: C:\Users\ABHISHEK\AppData\Local\Programs\Python\ Python311\12.py
List: [12, 56, 34, 78, 90, 42, 55]
Second largest element: 78
>>>
13 WAP in Python to display those strings which
are string with 'A' of given list

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. ")

user tuple = tuple (values) # Convert the list to a tuple


return user tuple

# 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

# Function to perform subtraction if user input in ("add", "subtract", "multiply"


def subtract (x, y): num1 = float (input ("Enter the first number:
return x - Y
num2 = float (input ("Enter the second number
# Function to perform multiplication if user input = "add":
def multiply (x, y): print ("Result :", add (numl, num2) )
return x * y elif user input = "subtract":
print ("Result:", subtract (num1, num2))
Function to perform division elif user input = "multiply":
def divide (x, y): print ("Result:", multiply(numl, num2))
if y = 0: elif user input = "divide" :
return "Cannot divide by zero" result = divide (numl, num2)
return x / y if isinstance (result, str) :
print ("Error:", result)
# Main function for the calculator else:
def calculator () : print ("Result:", result)
print ("Options:") else:
print (" Enter 'add' for addition ") print (" Invalid input. Please try again. ")
print ("Enter 'subtract' for subtraction")
print ("Enter 'multiply' for multiplication") # Run the calculator
print ("Enter 'divide for division") calculator ()
print ("Enter exit' to end the program")

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.
>>>

= RESTART: C:\Users\ABHISHEK\AppData\Local\Programs\Python\Python311\ 16.py


Options:
Enter add' for addition
Enter ' subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'exit' to end the program
: add
Enter the first number: 2
Enter the second number: 5
Result: 7.0
: subtract
Enter the first number: 7
Enter the second number: 10
Result:.: -3.o
:multiply
Enter the first number: -3
Enter the second number: 10
|Result: -30.0
: divide
Enter the first number: -30
Enter the second number: -15
Result: 2.0
: exit
WAP to Read a Text File line by line
17.and Display cach word separated bya

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

# Function to get employee data from the user


def get employee data () :
emp id = int (input ("Enter Employee ID: "))
emp name = input ("Enter Employee Name: ")
emp salary = float (input ("Enter Employee Salary: "))

return emp id, emp name, emp salary

# Function to write employee data to a binary file


def write employee data to file (filename, data):
try:
with open (filename, ab') as file:
# Pack the data into a binary format using struct
packed data = struct.pack ("il5s f", data [0], data [1] .encode (" utf-8"), data [2])
file.write (packed data)
print ("Employee data written to", filename)
except Exception as e:
print ("Error writing to the file:", str (e))
# Main program
if name main
employee data = get employee data()
write employee data to file ("emp. dat", employee data)

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 }
Type "help", "copyright", "credits" or "license () " for more information.
>>>
= RESTART: C:\Users\ABHISHEK\AppData\Local\Programs\ Python\Python311\ 18.py
Enter Employee ID: 1001
Enter Employee Name: Aryan
Enter Employee Salary: 1000000
Employee data written to emp.dat
>>>
WAP to get Roll no, Name and Marks of anys
19 student of a class and store these details in a
File called "marks.text"

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

# Function to Write student details to a text file


def write student details to file (filename, data):
try:
with open (filename, 'w') as file:
file.writelines (data)
print ("Student details written to", filename)
except Exception as e:
print ("Error writing to the file:", str(e) )
# Main program
if name main ":
student details = get student details (0
write student details to file ("marks.txt", student details)

IDLE Shell 3.11.4


OUTPUT
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 I
Type "help", "copyright", "credits" or "license () " for more information.
>>>
RESTART: C:\Users\ABHISHEK\AppData\ Local\ Programs\ Python\Python311\19.py
Enter details for Student 1:
Roll Number: 1
Name: Aryan
Marks: 100
Enter details for Student 2:
Roll Number: 2
Name: Shashwat
Marks: 200
Enter details for Student 3:
Roll Number: 3
Name: Rishi
Marks: 300
Enter details for student 4:
Roll Number: 4
Name: Ananya
Marks: 400
Enter details for Student 5:
Roll Number: 5
Name: Shreyansh
Marks: 500
student details written to marks.txt
>>>||
WAPto Count the number of Alphabets, Digits,
20. Uppercase letter,theLowercase letter and Space in
given string

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

for char in string:


if char.isalpha () :
num alphabets t= 1
if char.isupper ()
num uppercase += 1

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

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 infornation.
>>>
= RESTART: C:\Users\ABHISHEK\AppData\Local\Programs\Python\Python311\20.py
Alphabets: 1
Digits: 0
Uppercase letters: 2
LOwercase letters: 9
Spaces: 1
>>>
4f

My SQL
(15 QUERIES )
Command For
1. Creating a Database
mysql> create database hospital;
Query OK, 1 row affected (0.17 sec)

2. using the Database


mysql> use hospital;
Database changed
3.Creating a Table
mysql> create table clinic
->(
-> s_no int (4) ,
-> name varchar (15),
-> field varchar (20),
-> salary int (9),
-> age int (5)

Query OK, O rows affected, 3 warnings (0.61 sec)


4. Showing the Structure of Table
mysql> desc clinic;
| Field | Type | Null | Key | Default Extra|
5_no int YES NULL
name varchar (15) YES NULL
field | varchar(20) | YES |NULL
salary | int YES | NULL
age int YES |NULL

5 rows in set (0.24 sec)

5. mserting Data
mysql> insert ito clinic values (101, "Amit singh", "cardiologist", 99000, 38);
Query OK, 1 row affected (0.59 sec)

mysql> insert into clinic values (102, "Mohan singh", "neurologist",10000d,41);


Query OK, 1 row affected (0.20 sec)
mysql> insert into clinic values (103, "Krishna sinha", "dentist",55000, 32);
Query OK, 1 row affected (0.11 sec)
mysql> insert into clinic values (104, "Trisha pandey", "surgeon",98000,45);
Query OK, 1 row affected (0.16 sec)
mysql> insert into clinic values (105, "Manoj tripathi", "radiologist", 120000,49);
Query OK, 1 row affected (2.02 sec)

6.Vieving Contentsof Table


mysql> select*from clinic;
|| s_noname | field | salary | age
+ +
101 Amit singh cardiologist 90000 38
102 Mohan singh neurologist 100000 41
103 Krishna sinha dentist 55000 32
104 Trisha pandey 98000 45
Surgeon
105 | Manoj tripathi | radiologist 120000 49
+

5 rows in set (0.00 sec)


7. Retrieving Data
mysql> select name, field from clinic;
| name | field

| Amit singh | cardiologist |


Mohan singh | neurologist
Krishna sinha dentist
Trisha pandey I surgeon
| Manoj tripathi | radiologist |
5 rows in set (0.01 sec)

8. using Keyword »Distinct


mysql> select name from clinic where salary>9e000
name

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

1. using Where' clanse


nysql> select name, field from clinic;
name | field

Amit singh | cardiologist |


Mohan singh neurologist
Krishna sinha dentist
Trisha pandey surgeon
Manoj tripathi | radiologist
5 rows in set (0.01 sec)

12. Using lpdate


mysql> update clinic set salary=95000 where name="Amit singh";
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings:
13. using Max
mysql> select max(salary) from clinic;
-+

max(salary) |
-+
120000
-+
1 row in set (0.00 sec)

14. using Min


nysql> select min(salary) from clinic;
+

min(salary) |
+

55000 |
+

row in set (0.00 sec)

15. Usíng Greater Than (<)


mysql> select name from clinic where salary<95008;
name

| Krishna sinha |
1 row in set (1.59 sec)
mysql>

You might also like