Kiểm tra bảng điều khiển đơn giản:
>>> if len[b] == 0: print "Ups!"
...
Traceback [most recent call last]:
File "", line 1, in
NameError: name 'b' is not defined
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
Ví dụ cho thấy cách kiểm tra xem danh sách có các yếu tố không:
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
Otherwise:
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
Nếu bạn cần kiểm tra sự tồn tại/không tồn tại của danh sách/tuple, có thể điều này có thể giúp đỡ:
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
Nếu chúng ta cần tránh trong Globals [] có lẽ chúng ta có thể sử dụng điều này:in globals[] maybe we can use this:
from types import ListType, TupleType
try:
# for an existing/nonexisting list
if type[ima_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if type[ima_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
print "%s" % e
Output:
name 'ima_list' is not defined
---
name 'ima_tuple' is not defined
Tài liệu tham khảo: 8.15. Các loại-Tên cho các loại tích hợp-Python v2.7.3 Tài liệu //docs.python.org/3/l Library/types.html 8.15. types — Names for built-in types — Python v2.7.3 documentation //docs.python.org/3/library/types.html
Danh sách này là một thùng chứa quan trọng trong Python vì nó lưu trữ các yếu tố của tất cả các loại dữ liệu dưới dạng bộ sưu tập. Kiến thức về các hoạt động danh sách nhất định là cần thiết cho lập trình hàng ngày. Bài viết này thảo luận về cách nhanh nhất để kiểm tra xem giá trị có tồn tại trong danh sách hay không sử dụng Python.
Example:
list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True
Input: 7 # Check if 7 exist or not. Output: False
Phương pháp 1: Phương pháp ngây thơ
Trong phương pháp ngây thơ, người ta dễ dàng sử dụng một vòng lặp lặp qua tất cả các yếu tố để kiểm tra sự tồn tại của phần tử đích. Đây là cách đơn giản nhất để kiểm tra sự tồn tại của phần tử trong danh sách. Python là cách thông thường nhất để kiểm tra xem một yếu tố có tồn tại trong danh sách hay không. Cách cụ thể này trả về đúng nếu một phần tử tồn tại trong danh sách và sai nếu phần tử không tồn tại trong danh sách. Danh sách không cần phải được sắp xếp để thực hành phương pháp kiểm tra này.
Ví dụ 1: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng câu lệnh if-else không Check if an element exists in the list using the if-else statement
Python3
Các
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
0>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
2alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
4alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
6alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
0from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
2from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
3alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
7from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1Ví dụ 2: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng Loop & NBSP hay không;Check if an element exists in the list using a loop
Python3
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 from types import ListType, TupleType
try:
# for an existing/nonexisting list
if type[ima_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if type[ima_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
print "%s" % e
Output:
name 'ima_list' is not defined
---
name 'ima_tuple' is not defined
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
8>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
0>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9__22222219list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True4
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
4alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5 list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3Input: 7 # Check if 7 exist or not. Output: False0
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8Input: 7 # Check if 7 exist or not. Output: False4
‘
Output:
Element Exists
Ví dụ 3: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng trên mạng trong & NBSP hay không;Check if an element exists in the list using “in”
Python3
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 from types import ListType, TupleType
try:
# for an existing/nonexisting list
if type[ima_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if type[ima_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
print "%s" % e
Output:
name 'ima_list' is not defined
---
name 'ima_tuple' is not defined
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
8>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
0>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9__22222219list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True4
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
4alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5 list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3Input: 7 # Check if 7 exist or not. Output: False0
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8Input: 7 # Check if 7 exist or not. Output: False4
Output:
Element Exists
‘Check if an element exists in the list using any[] function
Python3
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 from types import ListType, TupleType
try:
# for an existing/nonexisting list
if type[ima_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if type[ima_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
print "%s" % e
Output:
name 'ima_list' is not defined
---
name 'ima_tuple' is not defined
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
8>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
0>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9__22222219list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True4
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
4alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5 list = test_list = [1, 6, 3, 5, 3, 4] Input: 3 # Check if 3 exist or not. Output: True7
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3Input: 7 # Check if 7 exist or not. Output: False0
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8Input: 7 # Check if 7 exist or not. Output: False4
Output:
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
0‘count[]
Ví dụ 3: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng trên mạng trong & NBSP hay không;
Python3
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5 Element Exists9
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
55from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9Input: 7 # Check if 7 exist or not. Output: False8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1Ví dụ 4: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng hàm bất kỳ [] nào không
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
69from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
2from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
3alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
76from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1Output:
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
20>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 >>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
222>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
23alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
32 >>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
33>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
34alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
36>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
37Phương pháp 2: Kiểm tra xem một phần tử có tồn tại trong danh sách bằng cách sử dụng Count []The bisect function will only state the position of where to insert the element but not the details about if the element is present or not.
Chúng ta có thể sử dụng phương thức danh sách Python được xây dựng, Count [], để kiểm tra xem phần tử được truyền có tồn tại trong danh sách không. Nếu phần tử được truyền tồn tại trong danh sách, phương thức đếm [] sẽ hiển thị số lần nó xảy ra trong toàn bộ danh sách. Nếu đó là một số dương khác không, nó có nghĩa là một yếu tố tồn tại trong danh sách. Thể hiện để kiểm tra sự tồn tại của các yếu tố trong danh sách bằng cách sử dụng Count [].
Python3
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 from types import ListType, TupleType
try:
# for an existing/nonexisting list
if type[ima_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if type[ima_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
print "%s" % e
Output:
name 'ima_list' is not defined
---
name 'ima_tuple' is not defined
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
41>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
43__>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
57>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 >>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
59>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
43from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
97>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 >>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
7>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
8>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
0>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
222alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
14from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
82>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
18alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
19alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
5 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
23alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9Input: 7 # Check if 7 exist or not. Output: False8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
31from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
33alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
35alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
37>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
39alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8Input: 7 # Check if 7 exist or not. Output: False4
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8 alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9Input: 7 # Check if 7 exist or not. Output: False8
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
2from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
3alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
52from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1Output:
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
2alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
31from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
Python3
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
35alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
37>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
39alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8Input: 7 # Check if 7 exist or not. Output: False4
Phương pháp 4: Sử dụng phương thức Find []
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
73>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
75alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
77alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
34alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
80alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
81>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
83alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
84from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6 from types import ListType, TupleType
try:
# for an existing/nonexisting list
if type[ima_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if type[ima_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
except Exception, e:
print "%s" % e
Output:
name 'ima_list' is not defined
---
name 'ima_tuple' is not defined
1>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
41>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
43__alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
69from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
2from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
3alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
76from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
55>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
1alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
86alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
87alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
88>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
90>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
8from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
3
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
Đầu ra
Python3
Phương pháp 5: Sử dụng hàm bộ đếm []
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3 alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
35alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
37>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
6alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
39alist = [1,2,3]
if alist: print "I'm here!"
Output: I'm here!
8Input: 7 # Check if 7 exist or not. Output: False4
Phương pháp 4: Sử dụng phương thức Find []
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
3alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
28>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
43alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
30>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
64Input: 7 # Check if 7 exist or not. Output: False4
alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
69from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
2from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
3alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
7alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
76from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
1
8alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
9alist = []
if not alist: print "Somebody here?"
Output: Somebody here?
55>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
1from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = [1,2,3,4]
# for an existing/nonexisting list
# "a_list" in globals[] check if "a_list" is defined [not undefined :p]
if "a_list" in globals[] and type[a_list] is ListType:
print "I'm a list, therefore I am an existing list! :]"
# for an existing/nonexisting tuple
if "a_tuple" in globals[] and type[a_tuple] is TupleType:
print "I'm a tuple, therefore I am an existing tuple! :]"
>>> try:
... len[b]
... except Exception as e:
... print e
...
name 'b' is not defined
4