Hướng dẫn how do you find the hypotenuse of a right angled triangle in python? - Làm thế nào để bạn tìm thấy cạnh huyền của một tam giác vuông trong python?

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:49 (UTC/GMT +8 giờ)

Python Basic: Tập thể dục-60 với giải pháp

Viết một chương trình Python để tính toán hạ huyết áp của một tam giác góc phải.

Từ Wikipedia, một tam giác bên phải hoặc hình tam giác góc phải, hoặc chính thức hơn là một tam giác trực giao, là một tam giác trong đó một góc là một góc vuông. Mối quan hệ giữa các cạnh và góc của một tam giác vuông là cơ sở cho lượng giác. Phía đối diện góc bên phải được gọi là hypotenuse. Nếu chiều dài của cả ba cạnh của một tam giác vuông là số nguyên, thì tam giác được cho là một tam giác Pythagore và độ dài bên của nó được gọi chung là một bộ ba Pythagore.
A right triangle or right-angled triangle, or more formally an orthogonal triangle, is a triangle in which one angle is a right angle. The relation between the sides and angles of a right triangle is the basis for trigonometry. The side opposite the right angle is called the hypotenuse. If the lengths of all three sides of a right triangle are integers, the triangle is said to be a Pythagorean triangle and its side lengths are collectively known as a Pythagorean triple.

Trình bày bằng hình ảnh:

Hướng dẫn how do you find the hypotenuse of a right angled triangle in python? - Làm thế nào để bạn tìm thấy cạnh huyền của một tam giác vuông trong python?

Giải pháp mẫu-1:

Mã Python:

from math import sqrt
print("Input lengths of shorter triangle sides:")
a = float(input("a: "))
b = float(input("b: "))
c = sqrt(a**2 + b**2)
print("The length of the hypotenuse is:", c )

Đầu ra mẫu:

Input lengths of shorter triangle sides:
a:  3
b:  4
The length of the hypotenuse is: 5.0

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Giải pháp mẫu-2:

Mã Python:

def test(x, y):
   h = (x**2 + y**2)**0.5
   return h
print(test(3,4))
print(test(3.5,4.4))

Đầu ra mẫu:

5.0
5.622277118748239

Flowchart:

Hướng dẫn how do you find the hypotenuse of a right angled triangle in python? - Làm thế nào để bạn tìm thấy cạnh huyền của một tam giác vuông trong python?

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Giải pháp mẫu-2:

Mã Python:

Trình chỉnh sửa mã Python: Write a Python program to convert height (in feet and inches) to centimeters.
Next: Write a Python program to convert the distance (in feet) to inches, yards, and miles.

Python: Lời khuyên trong ngày

Giải nén các biến bằng toán tử splat:

>>> def test(x, y, z):
>>> 	print(x, y, z)  
>>> res = test(*[10, 20, 30]) 
10 20 30
>>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
10 20 30

Phương pháp toán học


Thí dụ

Tìm hypotenuse của một tam giác góc phải, nơi vuông góc và cơ sở được biết đến:

#Trình thư viện toán học Mathimport Toán học
import math

#Set vuông góc và cơ sở parendicular = 10base = 5
parendicular = 10
base = 5

#in hypotenuse của một hình tam giác góc phải (math.hypot (parendicular, cơ sở)))
print(math.hypot(parendicular, base))

Hãy tự mình thử »


Định nghĩa và cách sử dụng

Phương pháp math.hypot() trả về định mức Euclide. Định mức Euclidian là khoảng cách từ nguồn gốc đến tọa độ được đưa ra.

Python 3.8 trước đó, phương pháp này chỉ được sử dụng để tìm ra hypotenuse của một tam giác góc phải: SQRT (x*x + y*y).

Từ Python 3.8, phương pháp này cũng được sử dụng để tính định mức Euclide. Đối với các trường hợp N chiều, các tọa độ được thông qua được coi là giống như (x1, x2, x3, ..., xn). Vì vậy, chiều dài Euclide từ gốc được tính bằng sqrt (x1*x1 + x2*x2 + x3*x3 .... xn*xn).


Cú pháp

math.hypot (x1, x2, x3, ..., xn)

Giá trị tham số

Tham sốSự mô tả
x1, x2, x3, ..., xnYêu cầu. Hai hoặc nhiều điểm đại diện cho tọa độ

Chi tiết kỹ thuật

Giá trị trở lại:Giá trị float, đại diện cho khoảng cách Euclide từ nguồn gốc cho đầu vào n hoặc hạ huyết áp của một tam giác góc phải cho hai đầu vào
Thay đổi nhật ký:Từ 3.8: Cũng hỗ trợ các điểm N chiều. Các phiên bản trước chỉ hỗ trợ các điểm hai chiều

Nhiều ví dụ hơn

Thí dụ

Tìm hypotenuse của một tam giác góc phải, nơi vuông góc và cơ sở được biết đến:

#Trình thư viện toán học Mathimport Toán học
import math

#Set vuông góc và cơ sở parendicular = 10base = 5
print(math.hypot(10, 2, 4, 13))
print(math.hypot(4, 7, 8))
print(math.hypot(12, 14))


Phương pháp toán học


Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Examples: 
     

    Bàn luận side1 = 3, side2 = 4 
    Output: 5.00 
    32 + 42 = 52
    Input: side1 = 12, side2 = 15 
    Output: 19.21 
     

    Với hai mặt khác của một tam giác góc phải, nhiệm vụ là tìm thấy nó hạ huyết áp. Pythagoras theorem states that the square of hypotenuse of a right angled triangle is equal to the sum of squares of the other two sides.
    Below is the implementation of the above approach:
     

    C++

    #include

    #include

    #include

    Đầu vào: Side1 = 3, Side2 = 4 & nbsp; đầu ra: 5,00 & nbsp; 32 + 42 = 52Input: side1 = 12, side2 = 15 & nbsp; đầu ra: 19,21 & nbsp; & nbsp;

    Cách tiếp cận: Định lý Pythagoras nói rằng hình vuông của các hình tam giác góc phải bằng với tổng hình vuông của hai mặt khác .Below là việc thực hiện phương pháp trên: & nbsp;

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    1
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    2

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    4
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    6
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    2
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    3
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    4

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    6
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    7

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    9
    5.0
    5.622277118748239
    
    0

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    5.0
    5.622277118748239
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    0

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    def test(x, y): h = (x**2 + y**2)**0.5 return h print(test(3,4)) print(test(3.5,4.4)) 0def test(x, y): h = (x**2 + y**2)**0.5 return h print(test(3,4)) print(test(3.5,4.4)) 9 5.0 5.622277118748239 4

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    5.0
    5.622277118748239
    
    6

    Java

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    2
    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    3

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    5
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    4
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    6
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    math.hypot()4

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3 math.hypot()6

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    math.hypot()4

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    6
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    7

    math.hypot()4#include7#include8#include9

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0float3
    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    5 float5 float6

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    Python3

    math.hypot()4

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    9 #include1#include2#include3#include4#include5

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    6
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    7

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    05#include 7 #include2#include5

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    09#include 7 #include4#include5

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    13
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    14

    C#

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    16

    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    2
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    18

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    5
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    4
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    25

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    26
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    math.hypot()4

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    3
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    33

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    34
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    35

    math.hypot()4

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    6
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    7

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0float3
    >>> def test(x, y, z):
    >>> 	print(x, y, z)  
    >>> res = test(*[10, 20, 30]) 
    10 20 30
    >>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
    10 20 30
    
    5 float5
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    45

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    9

    math.hypot()4

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    9
    5.0
    5.622277118748239
    
    4

    math.hypot()4

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    52
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    53
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    54

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    55
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    56

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    JavaScript

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    60

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    61
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    62

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    64

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    0
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    6
    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    7

    def test(x, y):
       h = (x**2 + y**2)**0.5
       return h
    print(test(3,4))
    print(test(3.5,4.4))
    
    
    8

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    69

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    70

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    71

    Input lengths of shorter triangle sides:
    a:  3
    b:  4
    The length of the hypotenuse is: 5.0
    
    72

    Độ phức tạp về thời gian: O (log (2*(s2)) trong đó s là mặt của hình chữ nhật. Vì độ phức tạp của thời gian của hàm SQRT sẵn có là o (log (n)): O(log(2*(s2)) where s is the side of the rectangle. because time complexity of inbuilt sqrt function is O(log(n))

    Không gian phụ trợ: O (1) O(1)


    Làm thế nào để bạn tìm thấy hạ huyết áp của một quyền

    Hymotenuse nằm đối diện góc vuông và có thể được giải quyết bằng cách sử dụng định lý Pythagore.Trong một tam giác bên phải với ống thông A và B và với hypotenuse C, định lý của Pythagoras nói rằng: a² + b² = c².Để giải quyết cho C, lấy căn bậc hai của cả hai bên để nhận C = √ (B²+A²).a² + b² = c² . To solve for c , take the square root of both sides to get c = √(b²+a²) .

    Công thức cho tam giác trong Python là gì?

    s = (a + b + c) / 2. # Tính diện tích.diện tích = (s*(s-a)*(s-b)*(s-c)) ** 0,5.in ('diện tích của tam giác là %0,2f' %diện tích). # calculate the area. area = (s*(s-a)*(s-b)*(s-c)) ** 0.5. print('The area of the triangle is %0.2f' %area)

    Làm thế nào để bạn tìm thấy góc của một tam giác trong Python?

    Khoa học dữ liệu thực tế sử dụng Python..
    ANS: = ARC-tan (AB/BC).
    trả lại ANS theo độ ..