Q1: Write a Python code (module) that contains a public variable called counter, which is initialized to 0. Implement an if-else statement that checks if the code is being run as the main program or if it is being imported as a module into another program. If it is the main program, the code should print "I prefer to be a module." If it is being imported as a module, it should print "I like to be a module."


# module_example.py

counter = 0

if __name__ == "__main__":
    print("I prefer to be a module.")
else:
    print("I like to be a module.")

Q2: Write a Python code snippet that imports the necessary module and iterates through the elements in the system path variable (sys.path). Inside the loop, print each element on a new line.
Please provide the code that accomplishes this task.


import sys
for p in sys.path:
  print(p)

Q3: You have two folders in your project directory: myproject and mymod. The mymod folder contains a Python module named module.py, and the myproject folder contains the main Python script main.py. Your task is to write the code for main.py, which will import the module.py module from the mymod folder. Additionally, you need to include a command to switch the working directory to the mymod folder before importing the module. Assume that both folders are located in the same parent directory.

#!/usr/bin/env python3

"""module.py - an example of a Python module"""

__counter = 0

def suml(the_list):
  global __counter
  __counter += 1
  the_sum = 0
  for element in the_list:
    the_sum += element
  return the_sum

def prodl(the_list):
  global __counter
  __counter += 1
  prod = 1
  for element in the_list:
    prod *= element
  return prod

if __name__ == "__main__":
  print("I prefer to be a module, but I can do some tests for you.")
  my_list = [i+1 for i in range(5)]
  print(my_list)
  print(suml(my_list) == 15)
  print(prodl(my_list) == 120)


# main.py

import os
import sys

# Step 1: Get the path to the 'mymod' folder (one level up from the current directory)
mymod_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "mymod"))

# Step 2: Append the 'mymod' folder path to the system path
sys.path.append(mymod_path)

# Step 3: Import the module
import module

# Sample lists
zeroes = [0 for i in range(5)]
ones = [1 for i in range(5)]

# Step 4: Use the functions from the imported module
print(module.suml(zeroes))
print(module.prodl(ones))

Q4: Create a Python module named math_operations.py in the same directory as your main.py script. In the math_operations.py module, define functions add, subtract, multiply, and divide, each taking two arguments and performing the respective operation. Now, in the main.py script, import all the functions from math_operations.py and use them to perform arithmetic operations on two numbers provided by the user.


# main.py
import math_operations

x = 40
y = 5

print(math_operations.add_math(x,y))
print(math_operations.subtract_math(x,y))
print(math_operations.multiply_math(x,y))
print(math_operations.divide_math(x,y))
#! /usr/bin/env python3

"""math_operations.py - basic math functions"""


def add_math(num1, num2):
    x = num1+num2
    return x

def subtract_math(num1, num2):
    x=num1-num2
    return x

def multiply_math(num1,num2):
    x=num1*num2
    return x

def divide_math(num1,num2):
    x=num1/num2
    return x

if __name__ == "__main__":
    num1 = 5
    num2 = 4
    print("I prefer to be a module, but I can do some tests for you.")
    print(add_math(num1,num2))
    print(subtract_math(num1,num2))
    print(multiply_math(num1,num2))
    print(divide_math(num1,num2))