Toán tử được sử dụng để thực hiện các thao tác trên các giá trị và biến. Đây là những ký hiệu đặc biệt thực hiện các phép tính số học và logic. Giá trị mà toán tử thao tác được gọi là Toán hạng
Mục lục
Toán tử logic
Trong Python, các toán tử logic được sử dụng trên các câu điều kiện [Đúng hoặc Sai]. Chúng thực hiện các phép toán logic AND, logic OR và logic NOT
HOẠT ĐỘNG MÔ TẢCÚ pháp và logic VÀ. Đúng nếu cả hai toán hạng là truex và yorLogical OR. True nếu một trong hai toán hạng là truex hoặc ynotLogical NOT. True nếu toán hạng là falsenot xBảng chân lý cho tất cả các tổ hợp giá trị của X và Y
Bảng sự thật
Toán tử logic AND
Toán tử logic trả về True nếu cả hai toán hạng đều True, ngược lại nó trả về False.
Python3
# Python program to demonstrate
# logical and operator
a
=
Atleast one number has boolean value as False0
Atleast one number has boolean value as False1
=
Atleast one number has boolean value as False0
Atleast one number has boolean value as False4_______194_______
Atleast one number has boolean value as False6
Atleast one number has boolean value as False0
Atleast one number has boolean value as False8
Atleast one number has boolean value as False9_______8_______0
Either of the number is greater than 0 No number is greater than 01
Either of the number is greater than 0 No number is greater than 02
Either of the number is greater than 0 No number is greater than 00
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Either of the number is greater than 0 No number is greater than 08
Either of the number is greater than 0 No number is greater than 09
Atleast one number has boolean value as False8
Atleast one number has boolean value as False9_______8_______0
Either of the number is greater than 0 No number is greater than 01
Either of the number is greater than 0 No number is greater than 02
Either of the number is greater than 0 No number is greater than 00
Either of the number is greater than 0 No number is greater than 01
Atleast one number has boolean value as True7
Either of the number is greater than 0 No number is greater than 00
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Either of the number is greater than 0 No number is greater than 08
Either of the number is greater than 0 No number is greater than 09
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Method called for value: -1 Method called for value: 5 Atleast one of the number is positive0_______8_______9
Đầu ra
The numbers are greater than 0 Atleast one number is not greater than 0
Ví dụ #2.
Python3
# Python program to demonstrate
# logical and operator
a
=
Atleast one number has boolean value as False0
Atleast one number has boolean value as False1
=
Method called for value: -1 Method called for value: 5 Atleast one of the number is positive9
Atleast one number has boolean value as False4_______194_______
Either of the number is greater than 0 No number is greater than 00
Atleast one number has boolean value as False8
a
___Either of the number is greater than 0 No number is greater than 01
Atleast one number has boolean value as False1
Either of the number is greater than 0 No number is greater than 01
# Python program to demonstrate
8Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
# logical and operator
2Either of the number is greater than 0 No number is greater than 09
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
# logical and operator
9Either of the number is greater than 0 No number is greater than 09
Đầu ra
Atleast one number has boolean value as False
Ghi chú. Nếu biểu thức đầu tiên được đánh giá là sai trong khi sử dụng và toán tử, thì các biểu thức tiếp theo sẽ không được đánh giá
Toán tử logic OR
Logic hoặc toán tử trả về True nếu một trong hai toán hạng là True.
Python3
# Python program to demonstrate
a
2
a
=
Atleast one number has boolean value as False0
Atleast one number has boolean value as False1
=
Atleast one number has boolean value as False6
Atleast one number has boolean value as False0
Atleast one number has boolean value as False4_______194_______
Either of the number is greater than 0 No number is greater than 00
Atleast one number has boolean value as False8
Atleast one number has boolean value as False9_______8_______0
=
6 Either of the number is greater than 0 No number is greater than 02
Either of the number is greater than 0 No number is greater than 00
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False03_______8_______9
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False10
Either of the number is greater than 0 No number is greater than 09
Atleast one number has boolean value as False8
Either of the number is greater than 0 No number is greater than 02
Either of the number is greater than 0 No number is greater than 00
=
6 Atleast one number has boolean value as True7
Either of the number is greater than 0 No number is greater than 00
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False03_______8_______9
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False10
Either of the number is greater than 0 No number is greater than 09
Đầu ra
Either of the number is greater than 0 No number is greater than 0
Ví dụ #2.
Python3
# Python program to demonstrate
# logical and operator
a
=
Atleast one number has boolean value as False0
Atleast one number has boolean value as False1
=
Method called for value: -1 Method called for value: 5 Atleast one of the number is positive9
Atleast one number has boolean value as False4_______194_______
Either of the number is greater than 0 No number is greater than 00
Atleast one number has boolean value as False8
a
=
6 Atleast one number has boolean value as False1
=
6 # Python program to demonstrate
8Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False51
Either of the number is greater than 0 No number is greater than 09
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False58_______8_______9
Đầu ra
Atleast one number has boolean value as True
Ghi chú. Nếu biểu thức đầu tiên được đánh giá là True trong khi sử dụng toán tử hoặc, thì các biểu thức tiếp theo sẽ không được đánh giá
Toán tử logic không
Toán tử logic không hoạt động với một giá trị boolean duy nhất. Nếu giá trị boolean là True, nó trả về False và ngược lại.
Python3
# Python program to demonstrate
Atleast one number has boolean value as False61
a
=
Atleast one number has boolean value as False0
Atleast one number has boolean value as False8
Atleast one number has boolean value as False66
Atleast one number has boolean value as False67
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False71
Either of the number is greater than 0 No number is greater than 09
Atleast one number has boolean value as False8
Atleast one number has boolean value as False66
Atleast one number has boolean value as False75_______0_______76
Atleast one number has boolean value as False77
=
=
Either of the number is greater than 0 No number is greater than 00
=
6 a
___Atleast one number has boolean value as False76
Atleast one number has boolean value as False84
=
=
Either of the number is greater than 0 No number is greater than 00
Atleast one number has boolean value as False88
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False92
Either of the number is greater than 0 No number is greater than 09
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 04
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Atleast one number has boolean value as False99_______8_______9
Đầu ra
10 is divisible by either 3 or 5
Thứ tự đánh giá các toán tử logic
Trong trường hợp có nhiều toán tử, Python luôn đánh giá biểu thức từ trái sang phải. Điều này có thể được xác minh bằng ví dụ dưới đây. Ví dụ.
Python3
# Python program to demonstrate
Either of the number is greater than 0 No number is greater than 002
Either of the number is greater than 0 No number is greater than 003
Either of the number is greater than 0 No number is greater than 004
Either of the number is greater than 0 No number is greater than 005
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 06
Either of the number is greater than 0 No number is greater than 07
Either of the number is greater than 0 No number is greater than 009_______8_______10
Either of the number is greater than 0 No number is greater than 05
Either of the number is greater than 0 No number is greater than 012
Either of the number is greater than 0 No number is greater than 013
Atleast one number has boolean value as False8
Either of the number is greater than 0 No number is greater than 015
Either of the number is greater than 0 No number is greater than 00
10 is divisible by either 3 or 55
Either of the number is greater than 0 No number is greater than 018
Either of the number is greater than 0 No number is greater than 05
a
=
Either of the number is greater than 0 No number is greater than 022
Atleast one number has boolean value as False1
=
Either of the number is greater than 0 No number is greater than 022
Atleast one number has boolean value as False4_______194_______
Either of the number is greater than 0 No number is greater than 022
Atleast one number has boolean value as False8
Either of the number is greater than 0 No number is greater than 030_______0_______6
Either of the number is greater than 0 No number is greater than 032
Either of the number is greater than 0 No number is greater than 09
=
6 Either of the number is greater than 0 No number is greater than 035
Atleast one number has boolean value as False84_______8_______9
=
6 Either of the number is greater than 0 No number is greater than 039
Atleast one number has boolean value as False0
Atleast one number has boolean value as False88