Điều gì có nghĩa và không có nghĩa trong Python?

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 x

Bả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.

Điều gì có nghĩa và không có nghĩa trong Python?
Ví dụ #1.

Python3




# Python program to demonstrate

# logical and operator

 

a=

Atleast one number has boolean value as False
0

Atleast one number has boolean value as False
1=
Atleast one number has boolean value as False
0

Atleast one number has boolean value as False
4_______194_______
Atleast one number has boolean value as False
6
Atleast one number has boolean value as False
0

 

Atleast one number has boolean value as False
8
Atleast one number has boolean value as False
9_______8_______0
Either of the number is greater than 0
No number is greater than 0
1
Either of the number is greater than 0
No number is greater than 0
2
Either of the number is greater than 0
No number is greater than 0
0
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Either of the number is greater than 0
No number is greater than 0
8
Either of the number is greater than 0
No number is greater than 0
9

 

Atleast one number has boolean value as False
8
Atleast one number has boolean value as False
9_______8_______0
Either of the number is greater than 0
No number is greater than 0
1
Either of the number is greater than 0
No number is greater than 0
2
Either of the number is greater than 0
No number is greater than 0
0
Either of the number is greater than 0
No number is greater than 0
1
Atleast one number has boolean value as True
7
Either of the number is greater than 0
No number is greater than 0
0
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Either of the number is greater than 0
No number is greater than 0
8
Either of the number is greater than 0
No number is greater than 0
9

10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Method called for value: -1
Method called for value: 5
Atleast one of the number is positive
0_______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 False
0

Atleast one number has boolean value as False
1=
Method called for value: -1
Method called for value: 5
Atleast one of the number is positive
9

Atleast one number has boolean value as False
4_______194_______
Either of the number is greater than 0
No number is greater than 0
0

 

Atleast one number has boolean value as False
8 a___
Either of the number is greater than 0
No number is greater than 0
1
Atleast one number has boolean value as False
1
Either of the number is greater than 0
No number is greater than 0
1 # Python program to demonstrate8

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7# logical and operator2
Either of the number is greater than 0
No number is greater than 0
9

10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7# logical and operator9
Either of the number is greater than 0
No number is greater than 0
9

Đầ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.

Điều gì có nghĩa và không có nghĩa trong Python?
Ví dụ #1.

Python3




# Python program to demonstrate

a2

 

a=

Atleast one number has boolean value as False
0

Atleast one number has boolean value as False
1=
Atleast one number has boolean value as False
6
Atleast one number has boolean value as False
0

Atleast one number has boolean value as False
4_______194_______
Either of the number is greater than 0
No number is greater than 0
0

 

Atleast one number has boolean value as False
8
Atleast one number has boolean value as False
9_______8_______0 =6
Either of the number is greater than 0
No number is greater than 0
2
Either of the number is greater than 0
No number is greater than 0
0
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
03_______8_______9

10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
10
Either of the number is greater than 0
No number is greater than 0
9

 

Atleast one number has boolean value as False
8
Either of the number is greater than 0
No number is greater than 0
2
Either of the number is greater than 0
No number is greater than 0
0 =6
Atleast one number has boolean value as True
7
Either of the number is greater than 0
No number is greater than 0
0
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
03_______8_______9

10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
10
Either of the number is greater than 0
No number is greater than 0
9

Đầ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 False
0

Atleast one number has boolean value as False
1=
Method called for value: -1
Method called for value: 5
Atleast one of the number is positive
9

Atleast one number has boolean value as False
4_______194_______
Either of the number is greater than 0
No number is greater than 0
0

 

Atleast one number has boolean value as False
8 a=6
Atleast one number has boolean value as False
1=6 # Python program to demonstrate8

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
51
Either of the number is greater than 0
No number is greater than 0
9

10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
58_______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.

Điều gì có nghĩa và không có nghĩa trong Python?
Ví dụ.

Python3




# Python program to demonstrate

Atleast one number has boolean value as False
61

 

a=

Atleast one number has boolean value as False
0

 

Atleast one number has boolean value as False
8
Atleast one number has boolean value as False
66
Atleast one number has boolean value as False
67

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
71
Either of the number is greater than 0
No number is greater than 0
9

 

Atleast one number has boolean value as False
8
Atleast one number has boolean value as False
66
Atleast one number has boolean value as False
75_______0_______76
Atleast one number has boolean value as False
77 ==
Either of the number is greater than 0
No number is greater than 0
0 =6 a___
Atleast one number has boolean value as False
76
Atleast one number has boolean value as False
84 ==
Either of the number is greater than 0
No number is greater than 0
0
Atleast one number has boolean value as False
88

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
92
Either of the number is greater than 0
No number is greater than 0
9

10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
4

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Atleast one number has boolean value as False
99_______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 0
02

Either of the number is greater than 0
No number is greater than 0
03

 

Either of the number is greater than 0
No number is greater than 0
04
Either of the number is greater than 0
No number is greater than 0
05

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
6
Either of the number is greater than 0
No number is greater than 0
7
Either of the number is greater than 0
No number is greater than 0
09_______8_______10

Either of the number is greater than 0
No number is greater than 0
5
Either of the number is greater than 0
No number is greater than 0
12
Either of the number is greater than 0
No number is greater than 0
13
Atleast one number has boolean value as False
8
Either of the number is greater than 0
No number is greater than 0
15
Either of the number is greater than 0
No number is greater than 0
0
10 is divisible by either 3 or 5
5
Either of the number is greater than 0
No number is greater than 0
18

Either of the number is greater than 0
No number is greater than 0
5

a=

Either of the number is greater than 0
No number is greater than 0
22

Atleast one number has boolean value as False
1=
Either of the number is greater than 0
No number is greater than 0
22

Atleast one number has boolean value as False
4_______194_______
Either of the number is greater than 0
No number is greater than 0
22

 

Atleast one number has boolean value as False
8
Either of the number is greater than 0
No number is greater than 0
30_______0_______6
Either of the number is greater than 0
No number is greater than 0
32
Either of the number is greater than 0
No number is greater than 0
9=6
Either of the number is greater than 0
No number is greater than 0
35
Atleast one number has boolean value as False
84_______8_______9=6
Either of the number is greater than 0
No number is greater than 0
39
Atleast one number has boolean value as False
0
Atleast one number has boolean value as False
88

Cái gì có và không có trong Python?

Trong Python, and và or (cùng với not) được định nghĩa là toán tử logic . Cả hai đều yêu cầu hai toán hạng có thể đánh giá là đúng hoặc sai.

làm gì. == Có nghĩa là gì trong Python?

Trong bài viết này, chúng ta sẽ xem. = (Không bằng) toán tử. Trong Python. = được định nghĩa là không bằng toán tử . Nó trả về True nếu toán hạng ở hai bên không bằng nhau và trả về False nếu chúng bằng nhau.

&= trong Python là gì?

Có nghĩa là hoạt động AND theo bit . Ví dụ. x = 5 x &= 3 #tương tự như x = x & 3 print(x)

Toán tử là gì và không phải là gì?

Toán tử logic *NOT (hoặc ¬) được dùng để phủ định các biến hoặc hằng logic .