Wednesday, 15 April 2026

πŸš€ Day 20/150 – Area of a Triangle in Python

 

πŸš€ Day 20/150 – Area of a Triangle in Python

Calculating the area of a triangle is a classic beginner-friendly problem that helps you understand formulas, user input, and different coding styles in Python.

The most common formula is:

Area=12×base×height\text{Area} = \frac{1}{2} \times \text{base} \times \text{height}

Let’s explore multiple ways to implement this πŸ‘‡

πŸ”Ή Method 1 – Basic Method (Direct Calculation)

base = 10 height = 5 area = 0.5 * base * height print("Area of triangle:", area)







🧠 Explanation:
  • We directly assign values to base and height.
  • 0.5 * base * height calculates the area.
  • Simple and easy to understand.

πŸ‘‰ Best for: Quick calculations and beginners.

πŸ”Ή Method 2 – Taking User Input

base = float(input("Enter base: ")) height = float(input("Enter height: ")) area = 0.5 * base * height print("Area of triangle:", area)






🧠 Explanation:
  • input() allows user interaction.
  • float() converts input into numbers.
  • Same formula is applied afterward.

πŸ‘‰ Best for: Dynamic, real-world scenarios.

πŸ”Ή Method 3 – Using a Function

def triangle_area(b, h): return 0.5 * b * h print(triangle_area(10, 5))





🧠 Explanation:
  • Function triangle_area() takes b and h as parameters.
  • return gives back the computed value.
  • Makes code reusable and clean.

πŸ‘‰ Best for: Structured and reusable programs.

πŸ”Ή Method 4 – Using Lambda Function

area = lambda b, h: 0.5 * b * h print(area(10, 5))




🧠 Explanation:
  • lambda is a short, one-line function.
  • Useful for simple operations without defining a full function.

πŸ‘‰ Best for: Short and quick logic.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (243) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (29) Azure (10) BI (10) Books (262) Bootcamp (6) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (31) Data Analytics (22) data management (15) Data Science (343) Data Strucures (17) Deep Learning (150) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (69) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (283) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (32) pytho (1) Python (1300) Python Coding Challenge (1128) Python Mistakes (51) Python Quiz (472) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)