Python Coding Day 4 | Operations and Practical Calculations

Mastering Operations and Practical Calculations

Watch the lesson tutorial  ðŸ”»


Python is a versatile language, but at its heart, it is a powerful calculator. To use it effectively, we must understand the fundamental rules governing how mathematical expressions are evaluated, known as the order of operations, and how to apply them to solve real-world problems like calculating Body Mass Index (BMI).

1. PEMDAS: The Order of Operations in Python

PEMDAS is an acronym used to remember the standard order in which mathematical operations must be performed. Python strictly adheres to these rules, ensuring that complex calculations always yield the correct result.

The PEMDAS Rule Breakdown

Letter

Operation

Description

P

Parentheses ()

Operations inside parentheses are performed first.

E

Exponents **

Calculations involving powers (like $x^2$) are done next.

M & D

Multiplication * and Division /

These operations have equal precedence and are performed from left to right.

A & S

Addition + and Subtraction -

These operations have equal precedence and are performed last, also from left to right.

In Python, the operators are:

  • Parentheses: ()

  • Exponent: ** (e.g., 2**3 is $2^3$)

  • Multiplication: *

  • Division (Floating-point): /

  • Addition: +

  • Subtraction: -

Practical PEMDAS Example in Python

Let's look at the specific expression you provided: 3*(3+3)/3-3.

  1. P (Parentheses): (3+3) is calculated first, resulting in 6.

    The expression becomes: 3 * 6 / 3 - 3

  2. M & D (Multiplication and Division): These are processed left to right.

    • 3 * 6 is $18$.

    • The expression becomes: 18 / 3 - 3

    • 18 / 3 is $6.0$. (Note: Division / always returns a float).

    • The expression becomes: 6.0 - 3

  3. A & S (Addition and Subtraction):

    • 6.0 - 3 is $3.0$.

The following code demonstrates this and other examples:

# See pemdas_examples.py for runnable code

2. Creating a BMI Calculator in Python

The Body Mass Index (BMI) is a measure used to estimate body fat based on a person's weight and height. It's a great simple project to practice input handling and mathematical operations in Python.

The BMI Formula

The standard formula for BMI is bmi_formula=weight/(height**2)

Here’s a short and simple article about BMI with a Python code example 👇


🧮 BMI Calculator in Python

Here’s a simple Python program to calculate your BMI:

# BMI Calculator

# Get user input
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: "))

# Calculate BMI
bmi = weight / (height ** 2)

# Show result
print("Your BMI is:", int(bmi))

⚖️ BMI Categories (for reference)

Category BMI Range
Underweight Below 18.5
Normal weight 18.5 – 24.9
Overweight 25 – 29.9
Obese 30 and above


- by Chirana Nimnaka



Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python