Skip to content

Assignment 3: Module 4 - Functions & Modules in Python. Contains factorial calculator and math module calculations.

Notifications You must be signed in to change notification settings

Akshu1245/python-functions-modules-assignment3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Assignment 3: Module 4 - Functions & Modules in Python

Python Version

📚 Overview

This repository contains Python programs demonstrating functions and the use of modules in Python. These examples show how to create reusable code blocks and leverage Python's built-in math module.


📋 Assignment Tasks

Task 1: Calculate Factorial Using a Function

File: factorial_calculator.py

Problem Statement:

  1. Defines a function named factorial that takes a number as an argument
  2. Calculates the factorial using a loop
  3. Returns the calculated factorial
  4. Calls the function with a sample number and prints the output

Task 2: Using the Math Module for Calculations

File: math_calculations.py

Problem Statement:

  1. Asks the user for a number as input
  2. Uses the math module to calculate:
    • Square root of the number
    • Natural logarithm (log base e) of the number
    • Sine of the number (in radians)
  3. Displays the calculated results

🎯 Learning Objectives

After completing these exercises, you will understand:

  • ✅ How to define and call functions in Python
  • ✅ How to use parameters and return values
  • ✅ How to import and use Python's built-in modules
  • ✅ How to use the math module for mathematical operations

📁 Project Structure

python-functions-modules-assignment3/
│
├── factorial_calculator.py    # Task 1: Factorial calculation using function
├── math_calculations.py       # Task 2: Math module calculations
└── README.md                  # This documentation file

🚀 Getting Started

Prerequisites

  • Python 3.x installed on your computer
  • A terminal or command prompt
  • A text editor or IDE (VS Code recommended)

Running the Programs

Task 1: Factorial Calculator

python factorial_calculator.py

Sample Output:

Enter a number: 5
Factorial of 5 is: 120

Task 2: Math Calculations

python math_calculations.py

Sample Output:

Enter a number: 25
Square root: 5.0
Logarithm: 3.2188758248682006
Sine: -0.13235175009777303

📖 Concepts Explained

1. Functions

Functions are reusable blocks of code that perform a specific task.

def function_name(parameter):
    # Code to execute
    return result

Example from Task 1:

def factorial(number):
    result = 1
    for i in range(1, number + 1):
        result = result * i
    return result

2. Importing Modules

Modules are Python files containing functions and variables that can be reused.

import math  # Import the entire module

3. Math Module Functions

Function Description Example
math.sqrt(x) Square root of x math.sqrt(25)5.0
math.log(x) Natural logarithm of x math.log(25)3.218...
math.sin(x) Sine of x (in radians) math.sin(25)-0.132...

4. Factorial Explained

Factorial of a number n (written as n!) is the product of all positive integers from 1 to n:

5! = 5 × 4 × 3 × 2 × 1 = 120
4! = 4 × 3 × 2 × 1 = 24
0! = 1 (by definition)

✅ Features

  • Clean, beginner-friendly code with meaningful comments
  • Error handling for invalid user input
  • Clear output formatting matching expected results
  • Well-documented functions with docstrings

📝 Reference

Python Course - Module 4: Functions & Modules in Python


👤 Author

Student Assignment
Module 4: Functions & Modules in Python


Happy Coding! 🐍

About

Assignment 3: Module 4 - Functions & Modules in Python. Contains factorial calculator and math module calculations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages