Python basic data type--list list

Source: Internet
Author: User

List:

The list is one of the underlying data types in Python, and other languages have data types similar to lists, such as JS called arrays, which are enclosed in [] and each element is separated by commas, and he can store various data types such as:

Li = [' Alex ', 123,ture, (Wusir), [All-in-all, ' xiaoming ',],{' name ': ' Alex '}]

Compared to strings, lists can store not only different data types, but also large amounts of data, 32-bit Python is limited to 536,870,912 elements, and 64-bit Python is limited to 1.,152,921,504,606,85e,+18 elements. And the list is ordered, there are index values, can be sliced, convenient to take value.

#列表能放字符串, numbers, lists, dictionaries, when none of the properties of the list are changed
Li = ['Alex', 100,true,[1,2,3],{'name': ' Taibai '}]
1. Index, slice, slice + step

Index

Li = ['Alex', 100,true,[1,2,3],{'name': ' Taibai '}]
Print (Li[0],type (li[0))
#运行结果 Alex <class ' str ' >

Slice + Step

Li = ['Alex', 100,true,[1,2,3],{'name':' Taibai ' }]print(Li[0:2])
Print (Li[0:3:2])
Print (Li[-1:1:-1])
2. Increase, delete, change, check

L1 = [' Alex ', ' Wusir ', ' Taibai ', ' Egon ', ' King Girl ', ' Wen Zhou Teacher ', ' Day of the day ']

Increase:

Append appended.

L1 = ['Alex','Wusir','Taibai','Egon','King Girl','Mr. Wen Zhou','Day Day']l1.append ('teacher Xiao Wen')Print(L1) #[' Alex ', ' Wusir ', ' Taibai ', ' Egon ', ' King Girl ', ' Wen Zhou Teacher ', ' Day of the Week ', ' Little Miss Wen ']

#循坏追加姓名, enter Q or Q to exit and print the list.
Name_list = [' Zhao three ', ' John Doe ']
While 1:
Username = input (' Please enter a new employee name '). Strip ()
If username.upper () = = ' Q ':
Break
Name_list.append (username)
Print (name_list)

Inser: Insert Add:

L1 = ['Alex','Wusir','Taibai','Egon','King Girl','Mr. Wen Zhou','Day Day']l1.insert (1,'Bao Yuan')Print(L1)

Expend: Iterative Append

L1 = ['Alex','Wusir','Taibai','Egon','King Girl','Mr. Wen Zhou','Day Day']l1.extend ('1111'#[' Alex ', ' Wusir ', ' Taibai ', ' Egon ', ' King Girl ', ' Wen Zhou Teacher ', ' Day ', ' 1 ', ' 1 ', ' 1 ', ' 1 ']l1.extend ('Alex'#[' Alex ', ' Wusir ', ' Taibai ', ' Egon ', ' King Girl ', ' Wen Zhou Teacher ', ' Day ', ' 1 ', ' 1 ', ' 1 ', ' 1 ', ' A ', ' l ', ' e ', ' X ']Print(L1)

Delete

Pop: According to the index to delete, pop (index), index, the default is not filled out the last element of the list, pop delete will return the deleted element

L1 = ['Alex','Wusir','Taibai','Egon','King Girl','Mr. Wen Zhou','Day Day']Print(L1.pop (0)) #返回 AlexPrint(L1) #[' Wusir ', ' Taibai ', ' Egon ', ' King Girl ', ' Wen Zhou Teacher ', ' Day of the day ']

3. Nesting of Lists

Python basic data type--list list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.