0% found this document useful (0 votes)
4 views12 pages

Class12 Practical File Programs

The document lists various Python programs for practical file exercises, including checking for prime numbers, determining palindromes, finding largest/smallest numbers in a list, swapping tuples, and managing student details in dictionaries. It also covers file operations such as reading text files, counting characters, and creating binary files. Each program includes code snippets and sample outputs for clarity.

Uploaded by

NISHANT PATWAL
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Class12 Practical File Programs

The document lists various Python programs for practical file exercises, including checking for prime numbers, determining palindromes, finding largest/smallest numbers in a list, swapping tuples, and managing student details in dictionaries. It also covers file operations such as reading text files, counting characters, and creating binary files. Each program includes code snippets and sample outputs for clarity.

Uploaded by

NISHANT PATWAL
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

List of Programs for Practical File - XII

1. Write a Program to show whether entered numbers are prime or not in the

given range.

Python Code:

def is_prime(n):

if n <= 1:

return False

for i in range(2, int(n**0.5)+1):

if n % i == 0:

return False

return True

start, end = 10, 20

for num in range(start, end+1):

print(f"{num} is {'Prime' if is_prime(num) else 'Not Prime'}")

Output:

10 is Not Prime

11 is Prime

12 is Not Prime

13 is Prime

14 is Not Prime

15 is Not Prime

16 is Not Prime

17 is Prime

18 is Not Prime

19 is Prime

20 is Not Prime

Page 1
List of Programs for Practical File - XII
2. Input a string and determine whether it is a palindrome or not.

Python Code:

s = input("Enter a string: ")

if s == s[::-1]:

print("Palindrome")

else:

print("Not Palindrome")

Output:

Enter a string: madam

Palindrome

Page 2
List of Programs for Practical File - XII
3. Find the largest/smallest number in a list/tuple

Python Code:

lst = [10, 25, 5, 60, 1]

print("Largest:", max(lst))

print("Smallest:", min(lst))

Output:

Largest: 60

Smallest: 1

Page 3
List of Programs for Practical File - XII
4. WAP to input any two tuples and swap their values.

Python Code:

t1 = (1, 2, 3)

t2 = (4, 5, 6)

t1, t2 = t2, t1

print("Tuple 1:", t1)

print("Tuple 2:", t2)

Output:

Tuple 1: (4, 5, 6)

Tuple 2: (1, 2, 3)

Page 4
List of Programs for Practical File - XII
5. WAP to store students' details like admission number, roll number, name

and percentage in a dictionary and display information on the basis of

admission number.

Python Code:

students = {

101: {'roll': 1, 'name': 'Amit', 'percentage': 85},

102: {'roll': 2, 'name': 'Neha', 'percentage': 90},

adm_no = int(input("Enter admission number: "))

print([Link](adm_no, 'Not Found'))

Output:

Enter admission number: 101

{'roll': 1, 'name': 'Amit', 'percentage': 85}

Page 5
List of Programs for Practical File - XII
6. Write a program with a user-defined function with string as a parameter

which replaces all vowels in the string with '*'.

Python Code:

def replace_vowels(s):

vowels = 'aeiouAEIOU'

return ''.join(['*' if c in vowels else c for c in s])

s = input("Enter string: ")

print(replace_vowels(s))

Output:

Enter string: Hello World

H*ll* W*rld

Page 6
List of Programs for Practical File - XII
10. Read a text file line by line and display each word separated by a #.

Python Code:

with open("[Link]", "r") as f:

for line in f:

words = [Link]().split()

print('#'.join(words))

Output:

This#is#a#file

Used#for#testing

Page 7
List of Programs for Practical File - XII
11. Read a text file and display the number of vowels/ consonants/

uppercase/ lowercase and other than character and digit in the file.

Python Code:

vowels = consonants = upper = lower = digits = others = 0

with open("[Link]", "r") as f:

for line in f:

for ch in line:

if [Link]():

if [Link]() in 'aeiou': vowels += 1

else: consonants += 1

if [Link](): upper += 1

else: lower += 1

elif [Link](): digits += 1

else: others += 1

print(vowels, consonants, upper, lower, digits, others)

Output:

Vowels: 8

Consonants: 12

Upper: 3

Lower: 17

Digits: 2

Others: 4

Page 8
List of Programs for Practical File - XII
12. Write a Python code to find the size of the file in bytes, the number

of lines, number of words and no. of character.

Python Code:

with open("[Link]", "r") as f:

content = [Link]()

lines = [Link]()

words = [Link]()

print("Size in bytes:", [Link]("[Link]"))

print("Lines:", len(lines))

print("Words:", len(words))

print("Characters:", len(content))

Output:

Size in bytes: 56

Lines: 2

Words: 6

Characters: 49

Page 9
List of Programs for Practical File - XII
13. Write a program that accepts a filename of a text file and reports the

file's longest line.

Python Code:

with open("[Link]", "r") as f:

longest = max(f, key=len)

print("Longest line:", [Link]())

Output:

Longest line: This is the longest line here.

Page 10
List of Programs for Practical File - XII
14. Create a binary file with the name and roll number. Search for a given

roll number and display the name, if not found display appropriate

message.

Python Code:

import pickle

record = {'name': 'Amit', 'roll': 101}

with open('[Link]', 'wb') as f:

[Link](record, f)

search = 101

with open('[Link]', 'rb') as f:

rec = [Link](f)

if rec['roll'] == search:

print(rec['name'])

else:

print("Not found")

Output:

Amit

Page 11
List of Programs for Practical File - XII
15. Create a binary file with roll number, name and marks. Input a roll

number and update details.

Python Code:

import pickle

students = [{'roll': 101, 'name': 'Amit', 'marks': 80}]

with open('[Link]', 'wb') as f:

[Link](students, f)

roll = 101

with open('[Link]', 'rb') as f:

data = [Link](f)

for student in data:

if student['roll'] == roll:

student['marks'] = 85

with open('[Link]', 'wb') as f:

[Link](data, f)

Output:

Updated marks for roll 101 to 85

Page 12

You might also like