Python Learning list Notes

Source: Internet
Author: User

Python Learning list Notes

The list is the most frequently used data type in Python;

Support for characters, numbers, strings can even contain lists (so-called nesting)

1. Definition:

List = [1,3,4,5, ' goog ', ' well ', 777]

2. Increase from the last column:

List.append ("Your is good!")

Show:

Direct input: List

Display: [1, 3, 4, 5, ' goog ', ' well ', 777, ' Your is good! ']

3. Add a column from the middle such as:

Use: Insert function:

Here's how to use it:

>>> List.insert (8, ' Liwen ')

>>> List

[1, 3, 4, 5, ' goog ', ' Liwen ', ' well ', ' Liwen ', ' Liwen ', 777, ' Your is good! ']

4. Delete one:

Using the Del

Such as:

>>> List

[1, 3, 4, 5, ' goog ', ' Liwen ', ' well ', ' Liwen ', ' Liwen ', 777, ' Your is good! ']

>>> del list[1] #删除下标为第一个的值

>>> List

[1, 4, 5, ' goog ', ' Liwen ', ' well ', ' Liwen ', ' Liwen ', 777, ' Your is good! ']


5. Traverse All values:

list = [1, 4, 5, ' goog ', ' Liwen ', ' well ', ' Liwen ', ' Liwen ', 777, ' Your is good! ']

Use for loop traversal:

For I in list:

Print I


6. View list length using the Len function


list = [1, 4, 5, ' goog ', ' Liwen ', ' well ', ' Liwen ', ' Liwen ', 777, ' Your is good! ']

Print Len (list)

print ' ===================== '


A = ' ddddddddddddddddddd '

Print Len (a)


10

=====================

19

=====================



7, Calculation: Statistics 5 appeared several times:


c = [1,3,4,5,5,5,5,5,4,5,4,6,9]

ncount = 0

For I in C:

if i = = 5:

ncount = ncount + 1

Print Ncount


Results:

6

==========ncount===========

Even and odd:

Range (1,101,2) odd

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

Range (2,101,2) even

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64 , 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]


Add:

1.

#!/usr/bin/env python

#-*-Coding:utf-8-*-

if __name__ = = ' __main__ ':

list = [' html ', ' JS ', ' css ', ' python ']


# method 1

Print U ' Traversal list Method 1: '

For I in list:

Print (U "ordinal:%s Value:%s"% (List.index (i) + 1, i))


Print U ' \ n Traverse list Method 2: '

# method 2

For I in range (len (list)):

Print (U "ordinal:%s Value:%s"% (i + 1, list[i]))


# Method 3

Print U ' \ n Traverse list Method 3: '

For I, Val in enumerate (list):

Print (U "ordinal:%s Value:%s"% (i + 1, val))


# Method 3

Print U ' \ n Traverse the list Method 3 (set the start of the traverse starting at the beginning, changing only the starting sequence): '

For I, Val in enumerate (list, 2):

Print (U "ordinal:%s Value:%s"% (i + 1, val))




Python Learning list Notes

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.