Khi xử lý các toán tử trong Python, chúng ta phải biết về khái niệm về mức độ ưu tiên và tính kết hợp của toán tử Python vì chúng xác định mức độ ưu tiên của toán tử, nếu không, chúng ta sẽ thấy kết quả không mong muốn
ưu tiên điều hành. Điều này được sử dụng trong một biểu thức có nhiều toán tử với mức độ ưu tiên khác nhau để xác định thao tác nào sẽ thực hiện trước
Thí dụ. Giải quyết
10 + 20 * 30
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 30
Mã số
Python3
# Precedence of '+' & '*'
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 300
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 302
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 303
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 304
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 306
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 307
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 309
đầu ra
610
Thí dụ. Bây giờ, hãy xem một ví dụ về toán tử logic 'hoặc' & logic 'và' . khối 'if' được thực thi ngay cả khi tuổi bằng 0. Bởi vì độ ưu tiên của logic 'và' lớn hơn logic 'hoặc'
Python3
6100
6101
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
6103
6104
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
6106
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 307
6108
6101
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301_______1_______1
6103
Hello! Welcome.3
6101
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
Hello! Welcome.7
Hello! Welcome.8
Hello! Welcome.9
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
Good Bye!!1
Good Bye!!2
Good Bye!!3
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
Good Bye!!5
Good Bye!!6
Good Bye!!7
Good Bye!!8
Good Bye!!2
Good Bye!!3
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
Good Bye!!5
100 6 0 5123
Good Bye!!7
đầu ra
Hello! Welcome.
Do đó, để chạy khối 'else', chúng ta có thể sử dụng dấu ngoặc đơn [ ] vì mức độ ưu tiên của chúng là cao nhất trong số tất cả các toán tử
Python3
6100
6101
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
6103
6104
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
6106
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 307
6108
100 + 200 / 10 - 3 * 104
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301_______1_______1
6103
Hello! Welcome.3
6101
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
Hello! Welcome.7
Good Bye!!7
Hello! Welcome.8
Hello! Welcome.9
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 301
Good Bye!!1
Good Bye!!2
100 + 200 / 10 - 3 * 10 is calculated as 100 + [200 / 10] - [3 * 10] and not as [100 + 200] / [10 - 3] * 109_______1_______8
Good Bye!!5
Good Bye!!6
Good Bye!!7
Good Bye!!8
Good Bye!!2
100 + 200 / 10 - 3 * 10 is calculated as 100 + [200 / 10] - [3 * 10] and not as [100 + 200] / [10 - 3] * 109_______1_______8
Good Bye!!5
100 6 0 5123
Good Bye!!7
đầu ra
Good Bye!!
Hiệp hội nhà điều hành. Nếu một biểu thức chứa hai hoặc nhiều toán tử có cùng mức độ ưu tiên thì Toán tử kết hợp được sử dụng để xác định. Nó có thể là từ trái sang phải hoặc từ phải sang trái
Thí dụ. '*' và '/' có cùng mức độ ưu tiên và tính kết hợp của chúng là Từ trái sang phải, vì vậy biểu thức “100/10 * 10” được coi là “[100/10] * 10”
Mã số
Python3
# Precedence of '+' & '*'
1
# Precedence of '+' & '*'
2
# Precedence of '+' & '*'
3
# Precedence of '+' & '*'
4
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
Good Bye!!5
# Precedence of '+' & '*'
7 # Precedence of '+' & '*'
8 10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 302
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 302
Good Bye!!7
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 307
# Precedence of '+' & '*'
1
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3005
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3006
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3007
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
Good Bye!!5
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3010
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3011
Good Bye!!1
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 303
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3014
Good Bye!!7
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 307
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3017
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
Good Bye!!5
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3010
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3011
Good Bye!!5
Good Bye!!1
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 303
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3014
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3026
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 307
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3028
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3029
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3030
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3031
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
Good Bye!!5
Good Bye!!1
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3014
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
Good Bye!!1
Good Bye!!7
đầu ra
100 6 0 512
Toán tử ưu tiên và tính kết hợp là hai đặc điểm chính của toán tử xác định thứ tự đánh giá của các biểu thức con khi không có dấu ngoặc
Thí dụ. Giải quyết
100 + 200 / 10 - 3 * 10
100 + 200 / 10 - 3 * 10 is calculated as 100 + [200 / 10] - [3 * 10] and not as [100 + 200] / [10 - 3] * 10
Mã số
Python3
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3042_______1_______1
# Precedence of '+' & '*'
7 10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 303
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3046
# Precedence of '+' & '*'
8 10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 302
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3011
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3014
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 305
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 302
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 308
10 + 20 * 30 is calculated as 10 + [20 * 30] and not as [10 + 20] * 3054
đầu ra
90.0
Vui lòng xem bảng ưu tiên và kết hợp sau đây để tham khảo. Bảng này liệt kê tất cả các toán tử từ mức ưu tiên cao nhất đến mức ưu tiên thấp nhất