Python for Mathematical Thinking: Basic Examples

Hello all... 🖐 Today's learning - Python for Mathematical Thinking - 1 Today we begin with the basics. This assumes that the basic knowledge of Python is already known. These examples may not be very useful for seniors or advanced learners, but they will definitely help those who want to revise their Python knowledge, revisit mathematical concepts, or start learning from scratch. 1. Finding the Square of a Number num = int(input("Enter a number: ")) square = num ** 2 print(f"The square of {num} is {square}")   This simple program takes a number as input and calculates its square using the exponent operator (**). 2. Finding the Cube of a Number Using a Function The code below is slightly different from the previous one. Here, we define a function first and then use it to calculate the cube of the given number. def cube(num):  """Calculates the cube of a number."""  return num ** 3 number = float(input("Please Enter any numeric Value: ")) cubed_number = cube(number) print(f"The Cube of the Given Number {number} = {cubed_number}")   Using functions makes the code more reusable and organized. 3. Finding the Factorial of a Number Using the Math Library In this example, we import the built-in math module in Python. This module contains many useful mathematical functions that allow us to perform calculations easily. import math num = int(input("Enter a number:")) if num < 0:   print("Sorry, factorial does not exist for negative numbers") else:   result = math.factorial(num)   print(f"The factorial of {num} is {result}")   These small exercises help build the foundation for solving larger mathematical and computational problems using Python. Thank you for reading... Have a nice day. 😊 Want to try these programs in Visual Studio Code? Follow the steps below: 1. Install Visual Studio Code. 2. Install the Python extension by pressing Ctrl + Shift + X and searching for Python. 3. Copy and paste the code from above. 4. Save the file with any name you like, but make sure it ends with .py (for example: program.py). 5. Open the Terminal in VS Code. 6. Type the command python your_filename.py and press Enter. 7. Now you can run the program and enjoy trying it with different numbers! #mathusingpython

To view or add a comment, sign in

Explore content categories