Python Full stack learning--day4

Source: Internet
Author: User

List

Description: A list is one of the underlying data types in Python and is enclosed in [], with each element separated by commas, and he can hold various data types such as:
1li = [' Alex ', 123,ture, (Wusir), [],{, ' xiaoming ', ' name ': ' Alex '}]
Lists can not only store different data types, but also store large amounts of data, compared to strings. Lists are ordered, indexed, and can be sliced for easy value. index, Slice, step
Li = [' Xiao ', 123,true, (A-P, ' Wusir '), [A/P, ' xiaoming ',],{' name ': ' Xiao '}]print (li[0]) print (li[2]) print (Li[1:4]) print ( Li[:5:2]) #倒着取, after 2 print (Li[-1:-3:-1])

Ganso

===== the above-mentioned learning content to be completed =====

===== Study Homework Practice =====

Li = [' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ']
1. Calculate the length of the list and output

Print (Len (LI))

2. Append the element ' seven ' to the list and output the added list

Li = [' Alex ', ' Wusir ', ' Eric ', ' Rain ', ' Alex ']li.append (' seven ') print (LI)

3. Please insert the element ' Tony ' in the 1th position of the list and output the added list

Li.insert (0, ' Tony ') print (LI)

4. Please modify the element in the 2nd position of the list to ' Kelly ' and output the modified list

Li[1] = ' Kelly ' Print (LI)

5. Add each element of the list l2=[1, ' A ', 3,4, ' heart ' to the list Li, a line of code is implemented, and the loop is not allowed to add.

L2 = [1, ' A ', 3,4, ' heart ']

6. Add each element of the string s = ' Qwert ' to the list Li, a line of code is implemented, and the loop is not allowed to add.

#使用迭代添加l2 = [1, ' A ', 3,4, ' heart '] #方法一: Li.append ([1, ' A ', 3,4, ' heart ']) #方法二: L3 = li.copy () + l2print (L3) #方法三: Li.extend (L2 ) Print (LI)

7. Remove the element ' Eric ' from the list and output the added list

s = ' Qwert ' li.extend (s) print (LI)

8. Delete the 2nd element in the list and export the deleted element and the list after the element is deleted

Li.pop (2) print (LI)

9. Delete the 2nd to 4th element in the list and output the list after the delete element

p = li.pop (1) print (p, "\ n", Li)

10. Invert all elements of the list and output the inverted list

Del Li[1:4]print (LI)

11. Please calculate the number of times the ' Alex ' element appears in the list Li and output the number of times.

Li.reverse () print (LI)
2, write code, such as the following table, using slices to achieve each function

Python Full stack learning--day4

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.