Python BASICS (II): python Basics

Source: Internet
Author: User

Python BASICS (II): python Basics
2. List Overview

A built-in data type in Python is list: list.

List is an ordered set.

A list consists of a series of elements in a specific order. It is represented.

The Data Types of the elements in the list can also be different, for example:

>>> L = ['apple', 123, True]

 

2.1 index list

Starting from 0 rather than 1. When the index is out of the range, Python reports an IndexError. Therefore, make sure that the index does not cross the border.

To obtain the last element, in addition to the index location, you can use-1 as the index to directly obtain the last element.

2.2 modify, add, and delete list elements 2.2.1 add elements

Add append ()

2.2.2 insert element

InsertInsert ()Specify the index and value of the new element.

>>> Classmates. insert (1, 'jack ')

>>> Classmates

['Michael ', 'jack', 'bob', 'tracy', 'Adam ']

2.2.3 delete an element

1.1. Delete del.IndexLocation del ABC [0]

1.2. to delete the elements at the end of the list, use the pop () method.

1.3. to delete the element at the specified position, use the pop (I) method, where I is the index position.

1.4. Use pop () to delete an element (pop-up). You can use the delete value.

1.5. AccordingValueDelete element remove ()

2.2.4 modifying elements

To replace an element with another element, you can directly assign a value to the corresponding index location.

Classmates [1] = 'sara'

2.3 organization list

The method sort () is used to permanently sort the list and sort the first letter.

The sorted () function sorts the list temporarily.

Print the list reverse () upside down ().

Determine the list length len ().

2.3.1 The list contains another list

The list element can also be another list, for example:

>>> S = ['python', 'java', ['asp ', 'php'], 'scheme']

>>> Len (s)

4

To get 'php', you can write p [1] Or s [2] [1], so s can be regarded as a two-dimensional array.

3. Operation list 3.1 traverse the entire list

Use for Loop

For a in:Do not miss the colon

Print () Note indent

Summary After print () loop ends

3.2 Create a Value List

1. The range () and range () functions can print only four numbers and cannot print 5

2. Use the function list () to convert range () to list (range ))

3. ** two heart numbers represent the square

4. perform a simple statistical calculation on the number list to calculate the min () Minimum, max () maximum, and sum () total.

5. List ParsingCombine the code of the for loop and the new element into one line and automatically add the new element:

[Value ** 2 for value in range (1, 11)]

3.3 use part of the List 3.3.1 slice

For operations that often take the specified index range, loop is very cumbersome. Therefore, Python provides the Slice operator to simplify the operation.

1. [:] Slice starts from 0, [: 4] indicates 0 to 3, and [-3:] indicates the last three

2. [2:] indicates that the number ranges from 3rd to the end.

3. If you do not write anything, you can copy a list as it is by writing.

4. [: Step Size]; [:-1] Reverse the character

5. The string 'xxx' can also be considered as a list. Each element is a character. Therefore, the string can also be sliced, but the operation result is still a string.

6. Return number: str (n) = str (n) [:-1]

3.4 tuples

Another ordered list is tuple.

Tuple and list are very similar, but tuple cannot be modified once initialized. Python calls unchangeable values and immutable lists as tuples. Because tuple is immutable, the code is safer. If possible, use tuple instead of list.

3.4.1 define tuples

1. add elements to () and separate them with commas (,). The access is the same as the list. But the value cannot be modified. However, values can be assigned. Numbers can be directly (,). The characters must be enclosed in quotation marks ('A', 'B', 'C ')

2. When Python displays a tuple with only one element, it also adds a comma to avoid misunderstanding into the brackets in the mathematical sense.

T = (1 ,)

3. tuple's so-called "unchanged" means that every element of tuple is always pointed. That is, if you point to 'A', you cannot change it to 'B' or to a list. Instead, you cannot change it to another object,HoweverListIt is variable!What should I do if I want to create a tuple with the same content? Therefore, each element of the tuple cannot be changed.

3.5 set the code format

1. PEP8

2. indent format, 4 spaces

3. The President shall not exceed 79 characters

4. empty row Structure

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.