0% found this document useful (0 votes)
10 views

Python_Programming_Exercises_with_Solutions

Uploaded by

Sibling War
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python_Programming_Exercises_with_Solutions

Uploaded by

Sibling War
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programming Exercises

Index
1. 1. Fibonacci Series using Function

2. 2. Palindrome Check using Function

3. 3. Random Number Generator (Dice Simulator)

4. 4. Creating and Importing a Library

5. 5. Reading Text File Line by Line and Separating Words

6. 6. Removing Lines Containing 'a' and Writing to Another File

7. 7. Counting Vowels, Consonants, Uppercase, and Lowercase Characters

8. 8. Adding 'ing' to Words Ending with 't', 'p', 'd'

9. 9. Binary File Operations with Name and Roll Number

10. 10. Binary File Operations (Roll Number, Name, Marks)

11. 11. Creating and Displaying CAMERA.csv

12. 12. Updating Voter Status in VOTERS.csv

13. 13. Implementing Stack using List

14. 14. Implementing Queue using List

15. 15. Finding Most Common Words in Text File (Phishing Emails)

Solution 1
# Task 1: Fibonacci Series using Function
def fibonacci_series(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

n_terms = int(input("Enter the number of terms: "))


fibonacci_series(n_terms)
Solution 2
# Task 2: Palindrome Check using Function
def is_palindrome(s):
return s == s[::-1]

string = input("Enter a string: ")


if is_palindrome(string):
print(f"{string} is a palindrome.")
else:
print(f"{string} is not a palindrome.")

Solution 3
# Task 3: Random Number Generator (Dice Simulator)
import random

def roll_dice():
return random.randint(1, 6)

print(f"You rolled a {roll_dice()}")

Solution 4
# Task 4: Creating and Importing a Library
# library.py
def add(a, b):
return a + b

def subtract(a, b):


return a - b

# main.py
import library

print(library.add(5, 3))
print(library.subtract(5, 3))

Solution 5
# Task 5: Reading Text File Line by Line and Separating Words
with open('sample.txt', 'r') as file:
for line in file:
words = line.split()
print('#'.join(words))

Solution 6
# Task 6: Removing Lines Containing 'a' and Writing to Another File
with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
if 'a' not in line:
outfile.write(line)

Solution 7
# Task 7: Counting Vowels, Consonants, Uppercase, and Lowercase Characters
def count_chars(file_path):
vowels = 'aeiouAEIOU'
vowel_count = consonant_count = uppercase_count = lowercase_count = 0

with open(file_path, 'r') as file:


for line in file:
for char in line:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
if char.isupper():
uppercase_count += 1
else:
lowercase_count += 1

return vowel_count, consonant_count, uppercase_count, lowercase_count

print(count_chars('sample.txt'))

Solution 8
# Task 8: Adding 'ing' to Words Ending with 't', 'p', 'd'
with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:
for line in infile:
words = line.split()
updated_words = [word + 'ing' if word[-1] in 'tpd' else word for word in words]
outfile.write(' '.join(updated_words) + '\n')
Solution 9
# Task 9: Binary File Operations with Name and Roll Number
import pickle

def write_data(file_name, data):


with open(file_name, 'wb') as file:
pickle.dump(data, file)

def read_data(file_name):
with open(file_name, 'rb') as file:
return pickle.load(file)

write_data('data.dat', {'roll': 101, 'name': 'John Doe'})


print(read_data('data.dat'))

You might also like