Home >Backend Development >Python Tutorial >Python List and Tuple types

Python List and Tuple types

高洛峰
高洛峰Original
2017-02-17 11:18:451462browse


List and Tuple types in Python

1. Python creates list

L = ['Adam', 95.5, 'Lisa', 85, 'Bart', 59]
print L

2. Python accesses list

L = [95.5,85,59]
print L[0]
print L[1]
print L[2]
print L[3]

according to index 3. Access list in reverse order in Python

L = [95.5, 85, 59]
print L[-1]
print L[-2]
print L[-3]
print L[-4]

4. Add new elements in Python

L = ['Adam', 'Lisa', 'Bart']
L.insert(2, 'Paul')
print L

5. Delete elements from list in Python

L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(3)
L.pop(2)
print L

6. Replace elements in Python

L = ['Adam', 'Lisa', 'Bart']
L[0] = 'Bart'
L[-1] = 'Adam'
print L

7. Python’s creation of tuple

t = (0,1, 2, 3, 4, 5, 6, 7, 8, 9)
print t

8. Python’s creation of single-element tuple

t = ('Adam',)
print t

9. Python’s “variable” tuple

t = ('a', 'b', ('A', 'B'))
print t

For more articles related to Python’s List and Tuple types, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn