❮ Chức năng tích hợp sẵn
Định nghĩa và cách sử dụng
Hàm
160 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ô đunThê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
160 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
162 đá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
163
164
165
166____67
160
165
162
163_______24
165
166
160
168
169
160
Enter the base number2
Enter the exponent4
Power is = 16.0
21Enter the base number2
Enter the exponent4
Power is = 16.0
22166______67
166
Enter the base number2
Enter the exponent4
Power is = 16.0
26Enter the base number2
Enter the exponent4
Power is = 16.0
27165
Enter the base number2
Enter the exponent4
Power is = 16.0
29 166
Enter the base number2
Enter the exponent4
Power is = 16.0
71167____173
Enter the base number2
Enter the exponent4
Power is = 16.0
74Enter the base number2
Enter the exponent4
Power is = 16.0
75Enter the base number2
Enter the exponent4
Power is = 16.0
76Enter the base number2
Enter the exponent4
Power is = 16.0
77Enter the base number2
Enter the exponent4
Power is = 16.0
71167____175
Enter the base number2
Enter the exponent4
Power is = 16.0
741662
Enter the base number2
Enter the exponent4
Power is = 16.0
76Enter the base number2
Enter the exponent4
Power is = 16.0
77đầu ra
169
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
163
1666
Enter the base number2
Enter the exponent4
Power is = 16.0
221668
1669____67
167
1632
1633
1634____129
160
Enter the base number2
Enter the exponent4
Power is = 16.0
22Enter the base number2
Enter the exponent4
Power is = 16.0
29 Enter the base number2
Enter the exponent4
Power is = 16.0
27Enter the base number2
Enter the exponent4
Power is = 16.0
26 1641
1642
160
1644
Enter the base number2
Enter the exponent4
Power is = 16.0
761646
1662
Enter the base number2
Enter the exponent4
Power is = 16.0
74Enter the base number2
Enter the exponent4
Power is = 16.0
731650
đầ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
76166______653____26
1655
Enter the base number2
Enter the exponent4
Power is = 16.0
74Enter the base number2
Enter the exponent4
Power is = 16.0
731650
đầu ra
Enter the base number2
Enter the exponent4
Power is = 16.0
2Phươ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
76166
1662
Enter the base number2
Enter the exponent4
Power is = 16.0
26Enter the base number2
Enter the exponent4
Power is = 16.0
26 Enter the base number2
Enter the exponent4
Power is = 16.0
751644
đầu ra
Enter the base number2
Enter the exponent4
Power is = 16.0
7Phươ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
166
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ó