Python Day-4

Source: Internet
Author: User

1. List

A type used to store data in which elements are separated by ', ' to store various data types, in which the data is ordered. (The index method is identical to the string)

List =[' Alex ', ' Wusir ', ' Egon ', ' goddess ', ' Taibai ']

Increase:

1. Append () appended to the last.

1 list.append (' Day of the day ')

Can be used with various data processing adornments such as:

1 list.strip (). Upper ()

2. Insert () () fill in the position to be inserted (3,alex) is the front of the element where the insertion position is 3.

1 List.insert (4, ' Brother Chun ')

3. Extend () is broken into the smallest element and inserted to iterate over the object int . You can iterate over the most basic elements of an object and plug it into the end.

1 list.extend (' Alex ')     [' Alex ', ' Wusir ', ' Egon ', ' goddess ', ' Taibai ', ' A ', ' l ', ' e ', ' X '] 2 ([list.extend])      [' Alex ', ' Wusir ', ' Egon ', ' goddess ', ' Taibai ', 1, 2, 3]

By deleting:

1.Pop ()(object to be deleted) the only one that has a return value returns the deleted element, Nothing is written, and the last one is deleted.

1 name = List.pop (1) delete element with position 2 2 name = List.pop () Delete last element

2.remove () (Deleted content)

1 list.remove (' Taibai ')

3.clear () Clears the list, leaving an empty list. Similar to a command.

1 list.clear ()

4.del (list name)

1 del list[a:b] range delete 2 Del list[0:2] Delete range 0-2

Change:

by index:

1 s[position] = ' new content ' string list anything can be 2 list[0] = ' male beast '3 list[0] = []

By slicing:

1 s[a:b] = shave off the elements of the A-B position and write as much as the basic element one by one, and the list replaces the elements in the list.
1List = ['Alex','Wusir','Egon','Ritian','Goddess']2List[0:3] = [A.'Brother Chun','Brother Salted Fish']3 Print(list)4The final result becomes: [1, 2, 3,'Brother Chun','Brother Salted Fish','Ritian','Goddess']

Check:

Len(list) element number

Calculates the length of the list.

1 s = Len ([1,2,3,4,5])2print(s)

Count(Element) element number

Calculates the number of occurrences of 5.

1 list = [1,2,3,4,5,5,6]2 s = list.count (5)3print(s)

Index (Element) Find location

1 list = ['Alex','wusir','Egon ','ritian',' goddess ']2  Print(List.index (' goddess '))

Sort:

List of sort () numbers forward sort

For numbers that are not, they are output in the original order.

1 list = ['Alex','wusir','Egon ','ritian',' goddess ']2  list.sort ()3print(list)

Forward output:

1 list = [5,4,2,1,3]2list.sort ()3print(list)

Sort(reverse = True) Reverse output:

1 list = [5,4,2,1,3]2 list.sort (reverse = True)3Print (list)

Reverse reverse output (not the number can also):

1 list = [1,2,3,4,5,6]2list.reverse ()3print(list)

Nesting:

LIST[A][B] The B element in the a element in the list.

1List = ['Taibai','Enrique','Yuanhao',['Alex','Egon', 89],23]2 3 Print(list[1][1])4Name =List[0].capitalize () capitalize the element with position 05LIST[2] ='Court Day Day' Replace the element with position 2 with the day of the garden
6LIST[2] = Li[2].replace ('Hao','Ritian') Change the position of 2 Hao this element to day
7 List[3][0] = Li[3][0].upper () capitalize the No. 0 element of the list with position 3

string did not change, can only be replaced. Note Use Replace.

2. Yuan zu

Ganso read-only list, can be circular query, can be sliced. A son cannot change, and his grandson can change.

You can think of a tuple as a list, but only the surface can not be changed, the next layer to do the operation.

1 tu = (the"Alex", [2,3,4,'taibai'],' Egon ')

The elements inside [2,3,4, ' Taibai '] can be modified. (List method can be used)

Join (an Iterative object) (is a string)

1 ' Alex ' 2 ' *** ' . Join (S) 3 Print (x) 4 result is: a***l***e***x

string - list split!!!

list - string join!!!

Range(A,b, step) interval, do not take the last one,ifa is equal to 0 , then do not write.

Range is a list containing numbers [1,2,3,4,5 ...]

can be taken backwards (the tail Head - the step size)

Interview questions, there are pits. Range(0,ten,-1) No output or error

1  for  in range (3,10):2     print(i)3 result is 3 4 5 6 7 8 9
1  for  in range (0,10,3):2     print(i)3 result is 0 3 6 9
1  for  in range (10,0,-2):2     print(i)3 result is 10 8 6 4 2
1  for  in range (10,-1,-2):2     print(i)3 result is 10 8 6 4 2 0

A notation for the most basic elements of an output (nested)

1Li = [1,2,3,5,'Alex', [2,3,4,5,'Taibai'],'AfDs']2  forIinchLi:3     ifType (i) = =list:4          forJinchI:5             Print(j)6         Continue7     Print(i)

Python Day-4

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.