0% found this document useful (0 votes)
10 views1 page

Python Functions and File Handling Examples

The document outlines several programming tasks in Python, including creating a function to compute the average of a list, reading numbers from a file and categorizing them as odd or even, demonstrating tuple unpacking and string operations, defining a function to check for perfect squares, and writing a program to reverse characters in a file. Each task requires specific coding implementations and error handling. The tasks cover basic file operations, functions, and data manipulation in Python.

Uploaded by

lkbprakhar27
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)
10 views1 page

Python Functions and File Handling Examples

The document outlines several programming tasks in Python, including creating a function to compute the average of a list, reading numbers from a file and categorizing them as odd or even, demonstrating tuple unpacking and string operations, defining a function to check for perfect squares, and writing a program to reverse characters in a file. Each task requires specific coding implementations and error handling. The tasks cover basic file operations, functions, and data manipulation in Python.

Uploaded by

lkbprakhar27
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

LONG ANSWER QUESTIONS

1)Write a python function named ComputeAverage to find average of a list of


numbers. It should handle the exception if the list is empty and return 0 in that
case.
2) There is a file named [Link]. Enter some positive numbers into the file
named [Link]. Read the contents of the file and if it is an odd number write
it to [Link] and if the number is even, write it to [Link]
3) Illustrate Unpacking tuples, mutable sequences, and string concatenation
with examples
4) Construct a function perfect_square(number) that returns a number if it is a
perfect square otherwise it returns -1. For example:
perfect_square(1) returns 1
perfect_square (2) returns -1
5) Construct a program to change the contents of the file by reversing each
character separated by comma:
Hello!!
Output - H,e,l,l,o,!,!

Common questions

Powered by AI

To reverse the contents of a file character-by-character with each character separated by a comma, open the target file for reading, read its contents into a string, then create a new string where each character is separated by a comma. Use the ''.join() method to insert commas between characters, and apply the reversed() function to reverse the resulting iteration of the string. Finally, write back the processed string into the file or another output file . For example, for the input 'Hello!', the output would be 'H,e,l,l,o,!,!' .

To construct a Python function named ComputeAverage that calculates the average of a list of numbers, define the function to take a list as an input. Use a try-except block to handle the case where the list is empty: in the try block, compute the average if the list is non-empty; in the except block, catch the ZeroDivisionError to return 0 when the list is empty . A simple implementation can look like: def ComputeAverage(numbers): try: return sum(numbers) / len(numbers) except ZeroDivisionError: return 0 .

To implement a function perfect_square in Python that identifies perfect squares, define a function that takes a number as input and calculates its square root. If the square of the integer value of this root equals the original number, return the number; otherwise, return -1. For example, perfect_square(1) should return 1, while perfect_square(2) should return -1 because 2 is not a perfect square . The function checks the condition using integer arithmetic to verify the square property.

To sort numbers from a file into separate files based on parity in Python, first read the numbers from the file Input.Txt. Use a loop to iterate through each number and apply a conditional statement to check if the number is odd or even. If the number is odd, write it to ODD.TXT; if even, write it to EVEN.TXT . This involves opening Input.Txt in read mode and the other two files in write mode to store the respective outputs .

Unpacking tuples in Python involves assigning the elements of a tuple to variables using a single assignment statement, e.g., tuple_values = (1, 2, 3); a, b, c = tuple_values . Handling mutable sequences can involve changing elements within a list by index, such as my_list = [1, 2, 3]; my_list[0] = 9, changing the list to [9, 2, 3]. String concatenation can be done using the '+' operator, e.g., str1 = 'Hello'; str2 = 'World'; result = str1 + ' ' + str2 results in 'Hello World' .

You might also like