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

Python_Programming_Solutions

Python

Uploaded by

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

Python_Programming_Solutions

Python

Uploaded by

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

Solutions to Programming Questions:

1. Area of a Circle:
def area_of_circle(radius):
return 3.14 * radius * radius

2. Positive, Negative, or Zero:


def check_number(num):
if num > 0:
return "Positive"
elif num < 0:
return "Negative"
else:
return "Zero"

3. Find Maximum in a List:


def find_max(numbers):
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
return max_val

4. Common Element in Two Lists:


def common_elements(list1, list2):
return any(x in list2 for x in list1)

5. Multiplication Table:
def multiplication_table(n):
return [n * i for i in range(1, 11)]

6. Factorial Using Recursion:


def factorial(n):
if n == 1 or n == 0:
return 1
return n * factorial(n - 1)

7. File Line Count:


def count_lines_in_file(filename):
with open(filename, 'r') as file:
return len(file.readlines())

8. Pattern (Pyramid of Stars):


def pyramid_pattern(n):
pattern = ""
for i in range(1, n + 1):
pattern += " " * (n - i) + "*" * (2 * i - 1) + "\n"
return pattern.strip()

9. Palindrome Check:
def is_palindrome(s):
return s == s[::-1]

10. File I/O Operations (Copy Content):


def copy_file_content(source, destination):
with open(source, 'r') as src, open(destination, 'w') as dest:
dest.write(src.read())

You might also like