Python notes (2) _python

Source: Internet
Author: User
Continue list:

To delete an element:
Copy Code code as follows:

A =[1, 2, 3, 4]
A[2:3] = [] #[1, 2, 4]
Del a[2] #[1, 2]

Empty list
Copy Code code as follows:

a[:] = []
Del a[:]

List is used as a stack (back-first out):
Copy Code code as follows:

stack = [3, 4, 5]
Stack.append (6)
Stack.append (7)
Stack.pop () # 7
Stack.pop () # 6
Stack.pop () # 5

Index with negative numbers:
Copy Code code as follows:

B=[1, 2, 3, 4]
B[-2] #3

"+" combination list:
Copy Code code as follows:

end = [' st ', ' nd '] + 5*[' th '] + [' XY '] # [' St ', ' nd ', ' th ', ' th ', ' th ', ' th ', ' ', ' ', ' th ', ' xy ']

To find out the number of an element in a list:
Copy Code code as follows:

Lst. (' Hello ') # the number of Hello

List sort:
Copy Code code as follows:

Sort ()
#对链表中的元素进行适当的排序.

Reverse ()
#倒排链表中的元素

Problem with function pointer:
Copy Code code as follows:

def F2 (A, l=[])
L.append (a)
Return L

Print (F2 (1)) # 1
Print (F2 (2)) # 1, 2 L in this function call is [1]
Print (F2 (3)) # 1, 2, 3

The parameters in the function are:

* Parameter name: An argument that represents any number

* *: Indicates dictionary parameter
Control statement:

IF:
Copy Code code as follows:

If x < 0:
x = 0
print ' Negative changed to zero '
elif x = = 0:
print ' Zero '
elif x = = 1:
print ' single '
Else
print ' More '

For:
Copy Code code as follows:

A = [' Cat ', ' window ', ' defenestrate ']
For X in a:
Print x, Len (x)

While:
Copy Code code as follows:

A, b = 0, 1
While B < 1000:
Print B,
A, B = B, a+b
#1 1 2 3 5 8 13 21 34 55 89 144 233 377 610-987

Pass: null Action statement
Copy Code code as follows:

While True:
Pass

Dictionary: Data structure of key-value pairs

Use list to construct dictionary:
Copy Code code as follows:

Items = [(' Name ', ' DC '], (' Age ', 78)]
D = dict (items) #{' age ': +, ' name ': ' DC '}

Interesting comparisons:
Copy Code code as follows:

x = [] #list
X[2] = ' foo ' #出错
x = {} #dictionary
X[2] = ' foo ' #正确

The content is quite miscellaneous, and learn what to write down. Full use of the work of leisure and spare time to complete, more substantial.



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.