Hướng dẫn is any () in python? - là bất kỳ () trong python?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về chức năng Python bất kỳ () với sự trợ giúp của các ví dụ.

Show

Hàm

any(iterable)
6 trả về
any(iterable)
7 nếu bất kỳ yếu tố nào của một điều khác là
any(iterable)
7. Nếu không, nó trả về
any(iterable)
9.

Thí dụ

boolean_list = ['True', 'False', 'True']

# check if any element is true result = any(boolean_list)

print(result) # Output: True


bất kỳ () cú pháp

Cú pháp của

any(iterable)
6 là:

any(iterable)

bất kỳ () tham số

Hàm

any(iterable)
6 có một số lượng (danh sách, chuỗi, từ điển, v.v.) trong Python.


Bất kỳ () giá trị trả về

Hàm

any(iterable)
6 trả về giá trị boolean:

  • any(iterable)
    7 Nếu ít nhất một yếu tố của một điều không thể thực hiện được
  • any(iterable)
    9 Nếu tất cả các yếu tố là sai hoặc nếu một thứ có thể trống

Tình trạngGiá trị trả về
Tất cả các giá trị là đúngĐÚNG VẬY
Tất cả các giá trị đều saiSai
Một giá trị là đúng (những người khác là sai)ĐÚNG VẬY
Tất cả các giá trị đều saiĐÚNG VẬY
Tất cả các giá trị đều saiSai


Một giá trị là đúng (những người khác là sai)

# True since 1,3 and 4 (at least one) is true
l = [1, 3, 4, 0]

print(any(l))

# False since both are False l = [0, False]

print(any(l))

# True since 5 is true l = [0, False, 5]

print(any(l))

# False since iterable is empty l = []

print(any(l))

Một giá trị là sai (những người khác là đúng)

True
False
True
False

Trống rỗng


Ví dụ 1: Sử dụng bất kỳ () trên danh sách Python

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

Một giá trị là sai (những người khác là đúng)

True
True
False

Trống rỗng

Ví dụ 1: Sử dụng bất kỳ () trên danh sách Python

# 0 is False
d = {0: 'False'}
print(any(d))

# 1 is True
d = {0: 'False', 1: 'True'}

print(any(d))

# 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {}

print(any(d))

# 0 is False # '0' is True d = {'0': 'False'} print(any(d))

Một giá trị là sai (những người khác là đúng)

False
True
False
False
True

Thực hiện bất kỳ () chức năng sử dụng hàm python và vòng lặp. hàm my_any () trả về true nếu bất kỳ yếu tố nào của itable là đúng, nếu không trả về sai. returns True if any of the elements of a given iterable( List, Dictionary, Tuple, set, etc) are True else it returns False.

Python bất kỳ () chức năng cú pháp

Cú pháp: Bất kỳ (có thể lặp lại)any(iterable)

  • Có thể sử dụng được: Đó là một đối tượng có thể lặp lại như từ điển, tuple, danh sách, bộ, vv & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; It is an iterable object such as a dictionary, tuple, list, set, etc.                 

Trả về: Trả về đúng nếu bất kỳ mục nào là đúng.Returns True if any of the items is True.

Ví dụ về chức năng Python bất kỳ ()

Python bất kỳ () chức năng trong danh sách trong Python. Ví dụ dưới đây trả về đúng vì ít nhất một phần tử trong danh sách (phần tử thứ 3) là đúng.

Python3

Các

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

6

Output:

True

Ví dụ 1: Python bất kỳ () & nbsp; danh sách chức năng

Sử dụng bất kỳ () trên danh sách Python.

Python3

True
False
True
False
0
True
False
True
False
1
True
False
True
False
2
True
True
False
0
True
False
True
False
4
True
True
False
25____34
True
True
False
4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

2

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

6

Các

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

6

Ví dụ 1: Python bất kỳ () & nbsp; danh sách chức năng

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

6

Output:

True
False
True
False

Sử dụng bất kỳ () trên danh sách Python.

True
False
True
False
0
True
False
True
False
1
True
False
True
False
2
True
True
False
0
True
False
True
False
4
True
True
False
25____34
True
True
False
4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

2

Python3

Các

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
True
False
True
False
2

True
False
True
False
0
True
False
True
False
1
False
True
False
False
True
5

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
True
False
True
False
2

Ví dụ 2: Hoạt động của bất kỳ () hàm nào với Tuples

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
True
False
True
False
2

Sử dụng bất kỳ () chức năng trên các bộ dữ liệu python.

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
True
False
True
False
2

Output:

True
False
True
False

True0True False True False1 # At east one (in fact all) elements are True s = "This is good" print(any(s)) # 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = '' print(any(s))4True3True False True False4True True False0True False True False4True7True8

True
0
True
False
True
False
1
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# 0 is False
d = {0: 'False'}
print(any(d))

# 1 is True
d = {0: 'False', 1: 'True'}

print(any(d))

# 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {}

print(any(d))

# 0 is False # '0' is True d = {'0': 'False'} print(any(d))
3
True
False
True
False
4
any(iterable)
9
True
False
True
False
4
any(iterable)
9
True
8

Python3

True
0
True
False
True
False
1
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
True
True
False
2
True
False
True
False
4
# 0 is False
d = {0: 'False'}
print(any(d))

# 1 is True
d = {0: 'False', 1: 'True'}

print(any(d))

# 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {}

print(any(d))

# 0 is False # '0' is True d = {'0': 'False'} print(any(d))
3
True
False
True
False
4
any(iterable)
13
True
False
True
False
4
True
True
False
4
True
False
True
False
4____________
True
8

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
42

True
0
True
False
True
False
1
any(iterable)
25

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
42

Ví dụ 3: Hoạt động của bất kỳ () hàm với các bộ

Sử dụng bất kỳ () chức năng trên các bộ python.

any(iterable)
30
True
False
True
False
1
any(iterable)
32
True
True
False
4
True
False
True
False
4
True
True
False
4
True
False
True
False
4
any(iterable)
13
any(iterable)
38

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
42

Output:

True
False
True
False

any(iterable)30True False True False1 any(iterable)32# 0 is False d = {0: 'False'} print(any(d)) # 1 is True d = {0: 'False', 1: 'True'} print(any(d)) # 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {} print(any(d)) # 0 is False # '0' is True d = {'0': 'False'} print(any(d))3True False True False4# 0 is False d = {0: 'False'} print(any(d)) # 1 is True d = {0: 'False', 1: 'True'} print(any(d)) # 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {} print(any(d)) # 0 is False # '0' is True d = {'0': 'False'} print(any(d))3True False True False4any(iterable)9any(iterable)38

Is In the case of a dictionary if all the keys of the dictionary are false or the dictionary is empty the any() returns False. If at least one key is True any() returns True.

# At east one (in fact all) elements are True s = "This is good" print(any(s)) # 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = '' print(any(s))3# At east one (in fact all) elements are True s = "This is good" print(any(s)) # 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = '' print(any(s))4# At east one (in fact all) elements are True s = "This is good" print(any(s)) # 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = '' print(any(s))5any(iterable)42

any(iterable)
30
True
False
True
False
1
any(iterable)
75

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
94

Ví dụ 4: Hoạt động của bất kỳ () chức năng nào với từ điển

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
94

Lưu ý: Trong trường hợp từ điển nếu tất cả các khóa của từ điển là sai hoặc từ điển trống, bất kỳ () trả về sai. Nếu ít nhất một khóa là đúng, bất kỳ () trả về đúng.

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
94

Python3

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
94

Output:

True
False
True
False

any(iterable)80True False True False1 any(iterable)82True True False4any(iterable)84any(iterable)85True False True False4True3any(iterable)84any(iterable)89any(iterable)38Working of any() function with Strings

any(iterable)
80
True
False
True
False
1
any(iterable)
82
# 0 is False
d = {0: 'False'}
print(any(d))

# 1 is True
d = {0: 'False', 1: 'True'}

print(any(d))

# 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {}

print(any(d))

# 0 is False # '0' is True d = {'0': 'False'} print(any(d))
3
any(iterable)
84
any(iterable)
85
True
False
True
False
4
any(iterable)
9
any(iterable)
84
any(iterable)
89
any(iterable)
38

Python3

Các

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
42

any(iterable)
80
True
False
True
False
1
any(iterable)
75

Sử dụng bất kỳ () chức năng trên các bộ python.

any(iterable)
30
True
False
True
False
1
any(iterable)
32
True
True
False
4
True
False
True
False
4
True
True
False
4
True
False
True
False
4
any(iterable)
13
any(iterable)
38

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
42

Output:

True
True
False

any(iterable)30True False True False1 any(iterable)32# 0 is False d = {0: 'False'} print(any(d)) # 1 is True d = {0: 'False', 1: 'True'} print(any(d)) # 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {} print(any(d)) # 0 is False # '0' is True d = {'0': 'False'} print(any(d))3True False True False4# 0 is False d = {0: 'False'} print(any(d)) # 1 is True d = {0: 'False', 1: 'True'} print(any(d)) # 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {} print(any(d)) # 0 is False # '0' is True d = {'0': 'False'} print(any(d))3True False True False4any(iterable)9any(iterable)38

Is

Python3

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

5
any(iterable)
42

any(iterable)
30
True
False
True
False
1
any(iterable)
75

Ví dụ 4: Hoạt động của bất kỳ () chức năng nào với từ điển

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

4
# True since 1,3 and 4 (at least one) is true
l = [1, 3, 4, 0]

print(any(l))

# False since both are False l = [0, False]

print(any(l))

# True since 5 is true l = [0, False, 5]

print(any(l))

# False since iterable is empty l = []

print(any(l))

87
# True since 1,3 and 4 (at least one) is true
l = [1, 3, 4, 0]

print(any(l))

# False since both are False l = [0, False]

print(any(l))

# True since 5 is true l = [0, False, 5]

print(any(l))

# False since iterable is empty l = []

print(any(l))

88

Output:

any(iterable)
4

Lưu ý: Trong trường hợp từ điển nếu tất cả các khóa của từ điển là sai hoặc từ điển trống, bất kỳ () trả về sai. Nếu ít nhất một khóa là đúng, bất kỳ () trả về đúng.ython any() function with for loop

Python3

Python3

any(iterable)
80
True
False
True
False
1
any(iterable)
82
True
True
False
4
any(iterable)
84
any(iterable)
85
True
False
True
False
4
True
3
any(iterable)
84
any(iterable)
89
any(iterable)
38

any(iterable)
80
True
False
True
False
1
any(iterable)
82
# 0 is False
d = {0: 'False'}
print(any(d))

# 1 is True
d = {0: 'False', 1: 'True'}

print(any(d))

# 0 and False are false d = {0: 'False', False: 0} print(any(d)) # iterable is empty d = {}

print(any(d))

# 0 is False # '0' is True d = {'0': 'False'} print(any(d))
3
any(iterable)
84
any(iterable)
85
True
False
True
False
4
any(iterable)
9
any(iterable)
84
any(iterable)
89
any(iterable)
38

Các

any(iterable)
80
True
False
True
False
1
any(iterable)
75

Ví dụ 5: Hoạt động của bất kỳ () hàm nào với chuỗi

Python bất kỳ () trả về đúng, nếu có ít nhất 1 ký tự trong chuỗi.

# At east one (in fact all) elements are True
s = "This is good"

print(any(s))

# 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = ''

print(any(s))

3
True
False
True
False
21

Output:

True

Bất kỳ () và tất cả () trong Python là gì?

Các chức năng Python bất kỳ () và tất cả () () đánh giá các mục trong một danh sách để xem cái nào là đúng.Phương thức bất kỳ () trả về đúng nếu bất kỳ mục nào trong danh sách là đúng và hàm tất cả () trả về đúng nếu tất cả các mục danh sách là đúng.evaluate the items in a list to see which are true. The any() method returns true if any of the list items are true, and the all() function returns true if all the list items are true.

Làm thế nào để bạn sử dụng bất kỳ () hoặc tất cả () trong Python?

Sử dụng tất cả () khi bạn cần kiểm tra một chuỗi dài và điều kiện. Sử dụng bất kỳ () khi bạn cần kiểm tra một loạt hoặc điều kiện dài. Use any() when you need to check a long series of or conditions.

Danh sách có gì trong Python?

Python bất kỳ () hàm trả về true nếu bất kỳ phần tử nào của một điều đáng tin cậy nhất định (danh sách, từ điển, tuple, set, v.v.) là đúng thì nó trả về sai.returns True if any of the elements of a given iterable( List, Dictionary, Tuple, set, etc) are True else it returns False.

Tất cả () được sử dụng trong Python ở đâu?

Chức năng python - tất cả () hàm python tất cả () trả về đúng nếu tất cả các phần tử của một số không thể sử dụng được (danh sách, từ điển, tuple, set, v.v.) là đúng nếu không nó sẽ trả về sai.Nó cũng trả về đúng nếu đối tượng có thể trống.returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.) are True otherwise it returns False. It also returns True if the iterable object is empty.