0% found this document useful (0 votes)
7 views3 pages

Python File Handling Exercises

The document contains a series of Python file handling practice questions that involve reading integers from an input file, performing various calculations (like counting even numbers, summing positive numbers, and checking for palindromes), and writing the results to an output file. Each question includes sample code, sample input, and expected output. The questions cover a range of topics including counting, summation, product of digits, and identifying palindromes.
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)
7 views3 pages

Python File Handling Exercises

The document contains a series of Python file handling practice questions that involve reading integers from an input file, performing various calculations (like counting even numbers, summing positive numbers, and checking for palindromes), and writing the results to an output file. Each question includes sample code, sample input, and expected output. The questions cover a range of topics including counting, summation, product of digits, and identifying palindromes.
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

Python File Handling Practice Questions with Code, Input, and Output

Question 1: Count Even Numbers


Write a program that reads integers from a file named [Link], counts how many of them
are even, and writes the count to [Link].
Code:
with open("[Link]", "r") as f:
numbers = [int([Link]()) for line in f if [Link]()]
count = sum(1 for n in numbers if n % 2 == 0)
with open("[Link]", "w") as f:
[Link](str(count) + "\n")
Sample Input: 12, 7, 8, 15, 10
Expected Output: 3

Question 2: Sum of Positive Numbers


Read integers from [Link]. Calculate the sum of all positive numbers and write it into
[Link].
Code:
with open("[Link]", "r") as f:
numbers = [int([Link]()) for line in f if [Link]()]
total = sum(n for n in numbers if n > 0)
with open("[Link]", "w") as f:
[Link](str(total) + "\n")
Sample Input: -5, 3, 10, -2, 7
Expected Output: 20

Question 3: Count Numbers Divisible by 3


Read integers from [Link] and count how many numbers are divisible by 3. Write the
count to [Link].
Code:
with open("[Link]", "r") as f:
numbers = [int([Link]()) for line in f if [Link]()]
count = sum(1 for n in numbers if n % 3 == 0)
with open("[Link]", "w") as f:
[Link](str(count) + "\n")
Sample Input: 9, 12, 7, 18, 25
Expected Output: 3

Question 4: Product of Digits


Each line in the input file contains a single integer. For each number, calculate the product
of its digits and write all results to [Link] (one per line).
Code:
def product_of_digits(n):
product = 1
for digit in str(abs(n)):
product *= int(digit)
return product
with open("[Link]", "r") as f:
numbers = [int([Link]()) for line in f if [Link]()]
results = [str(product_of_digits(n)) for n in numbers]
with open("[Link]", "w") as f:
[Link]("\n".join(results))
Sample Input: 123, 405, 92
Expected Output: 6, 0, 18

Question 5: Check Palindrome Numbers


From [Link], identify all numbers that are palindromes (same forward and backward) and
write them to [Link].
Code:
with open("[Link]", "r") as f:
numbers = [[Link]() for line in f if [Link]()]
palindromes = [n for n in numbers if n == n[::-1]]
with open("[Link]", "w") as f:
[Link]("\n".join(palindromes))
Sample Input: 121, 345, 787, 9009, 123
Expected Output: 121, 787, 9009

Question 6: Count Numbers Ending with Odd Digit


Read numbers from a file and count how many end with an odd digit (1, 3, 5, 7, 9). Write the
result to [Link].
Code:
with open("[Link]", "r") as f:
numbers = [int([Link]()) for line in f if [Link]()]
count = sum(1 for n in numbers if abs(n) % 10 in [1, 3, 5, 7, 9])
with open("[Link]", "w") as f:
[Link](str(count) + "\n")
Sample Input: 23, 44, 17, 80, 95
Expected Output: 3

Question 7: Write Squares of Numbers


Read integers from [Link]. For each integer, calculate its square and write all results to
[Link], one per line.
Code:
with open("[Link]", "r") as f:
numbers = [int([Link]()) for line in f if [Link]()]
squares = [str(n ** 2) for n in numbers]
with open("[Link]", "w") as f:
[Link]("\n".join(squares))
Sample Input: 2, 3, 4, 5
Expected Output: 4, 9, 16, 25

You might also like