Làm thế nào để bạn tính toán sức mạnh của python?

❮ Chức năng tích hợp sẵn



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

Hàm

16
0 trả về giá trị của x theo lũy thừa của y(xy)

Nếu có tham số thứ ba, nó trả về x lũy thừa của y, mô đun z


cú pháp

Giá trị tham số

Tham sốMô tảxA, số baseyA, số mũzTùy chọn. Một số, mô đun

Thêm ví dụ

Ví dụ

Trả về giá trị của 4 lũy thừa 3, mô đun 5 (giống như (4 * 4 * 4) % 5)

Tự mình thử »

❮ Chức năng tích hợp sẵn


Tạo chương trình Python để lấy hai số từ người dùng, một số là số cơ số, số khác là số mũ, sau đó tính lũy thừa

import math

base_number = float(input("Enter the base number"))
exponent = float(input("Enter the exponent"))

power = math.pow(base_number,exponent)
print("Power is =",power)

đầu ra

Enter the base number2
Enter the exponent4
Power is = 16.0

Mô-đun toán học tích hợp cung cấp một số chức năng cho các hoạt động toán học. Phương thức

16
0 lấy một cơ số và số mũ làm tham số và trả về lũy thừa

Vì trong Python, luôn có nhiều hơn một cách để đạt được những thứ tính toán sức mạnh với toán tử lũy thừa cũng có thể. Toán tử lũy thừa

16
2 đánh giá thành lũy thừa

Cho một số N và lũy thừa P. Nhiệm vụ là viết chương trình Python để tìm lũy thừa của một số bằng cách sử dụng đệ quy

Sự định nghĩa. Luỹ thừa của một số có thể được định nghĩa là phép nhân của số đó lặp đi lặp lại số lần lũy thừa của nó

Ví dụ

Đầu vào. Số = 2 Sức mạnh = 3

đầu ra. 2 luỹ thừa 3 = 8

Phương pháp 1. cách tiếp cận lặp đi lặp lại

Ở đây, chúng tôi đã sử dụng vòng lặp for để tính lũy thừa, bằng cách lặp lại phép nhân số cho một số lần nhất định

Python3




16
3
16
4

16
5
16
6____67
16
0

16
5
16
2
16
3_______24
16
5
16
6
16
0
16
8
16
9
16
0
Enter the base number2
Enter the exponent4
Power is = 16.0
21

Enter the base number2
Enter the exponent4
Power is = 16.0
22
16
6______67
16
6
Enter the base number2
Enter the exponent4
Power is = 16.0
26
Enter the base number2
Enter the exponent4
Power is = 16.0
27

16
5
Enter the base number2
Enter the exponent4
Power is = 16.0
29
16
6

 

Enter the base number2
Enter the exponent4
Power is = 16.0
71
16
7____173
Enter the base number2
Enter the exponent4
Power is = 16.0
74
Enter the base number2
Enter the exponent4
Power is = 16.0
75

Enter the base number2
Enter the exponent4
Power is = 16.0
76
Enter the base number2
Enter the exponent4
Power is = 16.0
77

Enter the base number2
Enter the exponent4
Power is = 16.0
71
16
7____175
Enter the base number2
Enter the exponent4
Power is = 16.0
74
16
62

Enter the base number2
Enter the exponent4
Power is = 16.0
76
Enter the base number2
Enter the exponent4
Power is = 16.0
77

đầu ra

16
9

Phương pháp 2. Cách tiếp cận đệ quy

Ở đây, chúng tôi đã sử dụng hàm đệ quy calc_power() để tính lũy thừa của một số đã cho bằng cách sử dụng đệ quy. Hàm này sẽ trả về 1 nếu công suất bằng 0, đây là điều kiện cơ bản cho hàm

Python3




16
3
16
66

Enter the base number2
Enter the exponent4
Power is = 16.0
22
16
68
16
69____67
16
7
16
32
16
33

16
34____129
16
0

Enter the base number2
Enter the exponent4
Power is = 16.0
22
Enter the base number2
Enter the exponent4
Power is = 16.0
29
Enter the base number2
Enter the exponent4
Power is = 16.0
27
Enter the base number2
Enter the exponent4
Power is = 16.0
26
16
41
16
42
16
0
16
44

 

Enter the base number2
Enter the exponent4
Power is = 16.0
76
16
46
16
62
Enter the base number2
Enter the exponent4
Power is = 16.0
74
Enter the base number2
Enter the exponent4
Power is = 16.0
73
16
50

đầu ra

16

Phương pháp 3. Sử dụng hàm pow()

Ở đây, chúng tôi đã sử dụng hàm pow() tích hợp để tính lũy thừa của bất kỳ số nguyên dương nào

Python3




Enter the base number2
Enter the exponent4
Power is = 16.0
76
16
6______653____26
16
55
Enter the base number2
Enter the exponent4
Power is = 16.0
74
Enter the base number2
Enter the exponent4
Power is = 16.0
73
16
50

đầu ra

Enter the base number2
Enter the exponent4
Power is = 16.0
2

Phương pháp 4. Sử dụng toán tử **

Ở đây, chúng ta đã sử dụng phép lũy thừa để tính lũy thừa của một số. Điều này thuận tiện nhất để sử dụng vì không cần gọi hàm hoặc nhập mô-đun

Python3




Enter the base number2
Enter the exponent4
Power is = 16.0
76
16
6
16
62
Enter the base number2
Enter the exponent4
Power is = 16.0
26
Enter the base number2
Enter the exponent4
Power is = 16.0
26
Enter the base number2
Enter the exponent4
Power is = 16.0
75
16
44

đầu ra

Enter the base number2
Enter the exponent4
Power is = 16.0
7

Phương pháp 5. sử dụng numpy

Bạn có thể cài đặt NumPy bằng cách chạy lệnh sau

16
6

sử dụng chức năng "power" của thư viện NumPy. Hàm này cho phép bạn tính lũy thừa của một số bằng một dòng mã. Đây là một ví dụ về cách sử dụng nó