Python notes (2)

Source: Internet
Author: User

Continue List:

Delete element:Copy codeThe Code is as follows: a = [1, 2, 3, 4]
A [2: 3] = [] # [1, 2, 4]
Del a [2] # [1, 2]

Clear listCopy codeThe Code is as follows: a [:] = []
Del a [:]

List is used as a stack (post-in-first-out ):Copy codeThe Code is as follows: stack = [3, 4, 5]
Stack. append (6)
Stack. append (7)
Stack. pop () #7
Stack. pop () #6
Stack. pop () #5

Index with negative number:Copy codeThe Code is as follows: B = [1, 2, 3, 4]
B [-2] #3

"+" Combination list:Copy codeThe Code is as follows: end = ['st', 'nd'] + 5 * ['th'] + ['xy'] # ['st', 'nd ', 'th', 'xy']

Find the number of elements in the list:Copy codeThe Code is as follows: lst. ('hello') # Number of hello'

List sorting:Copy codeThe Code is as follows: sort ()
# Sort the elements in the linked list.

Reverse ()
# Inverted list elements

Function pointer problems:Copy codeThe Code is as follows: def f2 (a, L = [])
L. append ()
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 include:

* Parameter Name: any number of parameters

**: Indicates the dictionary parameter.
Control statement:

IF:Copy codeThe Code is as follows: if x <0:
X = 0
Print 'negative changed to zero'
Elif x = 0:
Print 'zero'
Elif x = 1:
Print 'singles'
Else:
Print 'more'

FOR:Copy codeThe Code is as follows: a = ['cat', 'window', 'deletestrate']
For x in:
Print x, len (x)

WHILE:Copy codeThe 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

Pass: NULL operation statementCopy codeThe Code is as follows: while True:
Pass

Dictionary: Data Structure of key-value pairs

Use list to construct a dictionary:Copy codeThe Code is as follows: items = [('name', 'dc'), ('age', 78)]
D = dict (items) # {'age': 78, 'name': 'dc '}

Interesting comparison:Copy codeThe Code is as follows: x = [] # list
X [2] = 'foo' # error
X ={}# dictionary
X [2] = 'foo' # correct

The content is complex. Remember what you learn. It makes full use of free time and spare time at work.

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.