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

Python List and Tuple types

高洛峰
Release: 2017-02-17 11:18:45
Original
1472 people have browsed it


List and Tuple types in Python

1. Python creates list

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

2. Python accesses list

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

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]
Copy after login

4. Add new elements in Python

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

5. Delete elements from list in Python

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

6. Replace elements in Python

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

7. Python’s creation of tuple

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

8. Python’s creation of single-element tuple

t = ('Adam',)
print t
Copy after login

9. Python’s “variable” tuple

t = ('a', 'b', ('A', 'B'))
print t
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template