Hướng dẫn how do you create a new boolean in python? - làm thế nào để bạn tạo một boolean mới trong python?

Kiểu dữ liệu Boolean

Kiểu dữ liệu Boolean là đúng hoặc sai. Trong Python, các biến Boolean được xác định bởi các từ khóa

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3 và
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4.

>>> a = True
>>> type(a)


>>> b = False
>>> type(b)

Đầu ra

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
5 chỉ ra biến là kiểu dữ liệu boolean.

Lưu ý các từ khóa

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3 và
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4 phải có chữ cái đầu tiên trên. Sử dụng chữ thường
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
8 trả về lỗi.

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined

Số nguyên và phao như booleans

Các số nguyên và số điểm nổi có thể được chuyển đổi thành kiểu dữ liệu Boolean bằng hàm

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
9 của Python. Một số int, float hoặc số phức được đặt thành không trả về
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4. Một số nguyên, phao hoặc số phức được đặt thành bất kỳ số nào khác, dương hoặc âm, trả về
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3.

>>> zero_int = 0
>>> bool(zero_int)
False
>>> pos_int = 1
>>> bool(pos_int)
True
>>> neg_flt = -5.1
>>> bool(neg_flt)
True

Số học boolean

Số học Boolean là số học của logic đúng và sai. Một giá trị boolean hoặc logic có thể là

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3 hoặc
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4. Các giá trị Boolean có thể được thao tác và kết hợp với các toán tử Boolean. Các nhà khai thác Boolean trong Python bao gồm
>>> zero_int = 0
>>> bool(zero_int)
False
4,
>>> zero_int = 0
>>> bool(zero_int)
False
5 và
>>> zero_int = 0
>>> bool(zero_int)
False
6.

Các nhà khai thác boolean phổ biến trong Python dưới đây:

  • >>> zero_int = 0
    >>> bool(zero_int)
    False
    
    5
  • >>> zero_int = 0
    >>> bool(zero_int)
    False
    
    4
  • >>> zero_int = 0
    >>> bool(zero_int)
    False
    
    6
  • >>> pos_int = 1
    >>> bool(pos_int)
    True
    
    0 (tương đương)
  • >>> pos_int = 1
    >>> bool(pos_int)
    True
    
    1 (không tương đương)

Trong phần mã bên dưới, hai biến được gán các giá trị boolean

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3 và
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4. Sau đó, các giá trị Boolean này được kết hợp và thao tác với các toán tử Boolean.

>>> A = True
>>> B = False
>>> A or B
True
>>> A and B
False
>>> not A
False
>>> not B
True
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
0
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
1

Các toán tử Boolean như

>>> zero_int = 0
>>> bool(zero_int)
False
4,
>>> zero_int = 0
>>> bool(zero_int)
False
5 và
>>> zero_int = 0
>>> bool(zero_int)
False
6 có thể được kết hợp với ngoặc đơn để tạo ra các biểu thức boolean hợp chất.

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
2

Một bản tóm tắt các toán tử Boolean và các toán tử Boolean được hiển thị trong bảng dưới đây:

MộtBkhông phải akhông bA == bA =! BA hoặc bA và B
TFFTFTTF
FTTFFTTF
TTFFTFTT
FFTTTFFF


In "Có!" Nếu hàm trả về đúng, nếu không thì in "Không!":


Nếu myfunction (): & nbsp; In ("Có!") Khác: & nbsp; in ("Không!")

Python cũng có nhiều hàm tích hợp trả về giá trị boolean, như hàm

>>> A or B
True
8, có thể được sử dụng để xác định xem một đối tượng có thuộc loại dữ liệu nhất định không:

Kiểm tra xem một đối tượng có phải là số nguyên hay không:

x = 200 dấu (isinstance (x, int))

Kiểm tra bản thân với các bài tập

Thí dụ

Booleans đại diện cho một trong hai giá trị:

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3 hoặc
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4.

Giá trị boolean
b = 33

Trong lập trình, bạn thường cần biết một biểu thức là

>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
3 hay
>>> c = true
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'true' is not defined

>>> d = false
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'false' is not defined
4.
  print("b is greater than a")
else:
  print("b is not greater than a")

Hãy tự mình thử »


Bạn có thể thực thi mã dựa trên câu trả lời boolean của một hàm:

In "Có!" Nếu hàm trả về đúng, nếu không thì in "Không!":

Thí dụ

Nếu myfunction (): & nbsp; In ("Có!") Khác: & nbsp; in ("Không!")

Python cũng có nhiều hàm tích hợp trả về giá trị boolean, như hàm

>>> A or B
True
8, có thể được sử dụng để xác định xem một đối tượng có thuộc loại dữ liệu nhất định không:
print(bool(15))

Hãy tự mình thử »

Thí dụ

Bạn có thể thực thi mã dựa trên câu trả lời boolean của một hàm:

In "Có!" Nếu hàm trả về đúng, nếu không thì in "Không!":
y = 15

print(bool(x))print(bool(y))
print(bool(y))

Hãy tự mình thử »



Bạn có thể thực thi mã dựa trên câu trả lời boolean của một hàm:

In "Có!" Nếu hàm trả về đúng, nếu không thì in "Không!":

Nếu myfunction (): & nbsp; In ("Có!") Khác: & nbsp; in ("Không!")

Python cũng có nhiều hàm tích hợp trả về giá trị boolean, như hàm

>>> A or B
True
8, có thể được sử dụng để xác định xem một đối tượng có thuộc loại dữ liệu nhất định không:

Kiểm tra xem một đối tượng có phải là số nguyên hay không:

Thí dụ

x = 200 dấu (isinstance (x, int))

Kiểm tra bản thân với các bài tập
bool(123)
bool(["apple", "cherry", "banana"])

Hãy tự mình thử »


Các chức năng có thể trả lại một boolean

Bạn có thể tạo các chức năng trả về giá trị Boolean:

Thí dụ

In câu trả lời của một chức năng:

def myfunction (): & nbsp; trả về đúng
bool(None)
bool(0)
bool("")
bool(())
bool([])
bool({})

Hãy tự mình thử »

Các chức năng có thể trả lại một boolean

Thí dụ

lớp myClass (): & nbsp; def __len __ (tự): & nbsp; & nbsp; & nbsp; trả lại 0
  def __len__(self):
    return 0

myObj = myClass () in (bool (myobj))
print(bool(myobj))

Hãy tự mình thử »


Các chức năng có thể trả lại một boolean

Bạn có thể tạo các chức năng trả về giá trị Boolean:

Thí dụ

In câu trả lời của một chức năng:

def myfunction (): & nbsp; trả về đúng
  return True

print(myFunction())

Hãy tự mình thử »

Bạn có thể thực thi mã dựa trên câu trả lời boolean của một hàm:

Thí dụ

In "Có!" Nếu hàm trả về đúng, nếu không thì in "Không!":

def myfunction (): & nbsp; trả về đúng
  return True

Hãy tự mình thử »
  print("YES!")
else:
  print("NO!")

Hãy tự mình thử »

Bạn có thể thực thi mã dựa trên câu trả lời boolean của một hàm:

Thí dụ

In "Có!" Nếu hàm trả về đúng, nếu không thì in "Không!":

Nếu myfunction (): & nbsp; In ("Có!") Khác: & nbsp; in ("Không!")
print(isinstance(x, int))

Hãy tự mình thử »


Bạn có thể thực thi mã dựa trên câu trả lời boolean của một hàm: