Python notes (2)

Source: Internet
Author: User
Continue list:

To delete an element:

The code is as follows:


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


Clear List

The code is as follows:


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


The list is used as a stack (after-in first-out):

The code is as follows:


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


Use a negative index:

The code is as follows:


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


"+" Combo list:

The code is as follows:


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


Find out the number of elements in the list:

The code is as follows:


Lst. Number of (' Hello ') # Hello


List sort:

The code is as follows:


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

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


Problem with function pointers:

The code is as follows:


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

Print (F2 (1)) # 1
Print (F2 (2)) # 1, 2 L when this function is called [1]
Print (F2 (3)) # 1, 2, 3


The parameters in the function are:

* Parameter name: An arbitrary number of parameters

* *: Indicates dictionary parameter
Control statements:

IF:

The code is 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:

The code is as follows:


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


While:

The code is 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

The code is as follows:


While True:
Pass


Dictionary: Data structure of key-value pairs

Use list to construct dictionary:

The code is as follows:


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


Interesting comparisons:

The code is as follows:


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


The content is more miscellaneous, learn what to write down. Full use of the work of leisure and spare time to complete, more enriched.
  • 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.