, .
, , if:
>>> def is_it_true(anything):
... if anything:
... print(", ")
... else:
... print(", ")
...
>>> is_it_true([]) ①
,
>>> is_it_true(['a']) ②
,
>>> is_it_true([False]) ③
,
① | . |
② | , , . |
③ | , , . . |
. .
>>> a_tuple = ("a", "b", "mpilgrim", "z", "example") ①
>>> a_tuple
('a', 'b', 'mpilgrim', 'z', 'example')
>>> a_tuple[0] ②
'a'
>>> a_tuple[-1] ③
'example'
>>> a_tuple[1:3] ④
('b', 'mpilgrim')
① | , , , , . |
② | , . , , a_tuple[0]. |
③ | , . |
④ | (slicing) . , ; , . |
, . , . , , . , append(), extend(), insert(), remove(), pop(). . ( ), , ( ), ... , .
#
>>> a_tuple
('a', 'b', 'mpilgrim', 'z', 'example')
>>> a_tuple.append("new") ①
Traceback (innermost last):
File "<interactive input>", line 1, in?
AttributeError: 'tuple' object has no attribute 'append'
>>> a_tuple.remove("z") ②
Traceback (innermost last):
File "<interactive input>", line 1, in?
AttributeError: 'tuple' object has no attribute 'remove'
>>> a_tuple.index("example") ③
4
>>> "z" in a_tuple ④
True
:
( ):
"< >", 1, ?
|
|
AttributeError: 'tuple' '<>'
① | . append() extend(). |
② | . remove() pop(). |
③ | , . |
④ | in, . |
?
- , . , , .
- , , . assert, , , ( ), .
- (, , , , , ). , .
. tuple() , list() . , tuple() , list() . |