Python Learning Notes (10)-Advanced features

Source: Internet
Author: User
Tags iterable

One, slice

1. Definition:

It is often tedious to take an operation with a specified index range, so Python provides a slice (Slice) operator.

2. Syntax:

A[1:3]          Remove 1 to 3, is a positive case, the missing fill is 0 (first), End (the last) B[-3:] Take the third to the bottom of the penultimate, is negative, the absence of           filling is 1 (last) C[1:10:2]     From 1 to 10, each of 2 takes one

The same syntax as MATLAB, that is, a vector can be a matrix operation. Can be used for list.tuple,string,dict,set, etc.

Second, slicing

1. Definition:

Given a list or tuple, we can iterate through for the list or tuple through a loop, which we call an iteration (iteration).

2, Python Iteration special point:

Python has a more for circular abstraction than the Java for Loop, because the Python for loop can be used not only on a list or a tuple, but also on other objects that can iterate. List this data type although there is subscript, but many other data types are not subscript, but as long as the object can be iterated, whether or not subscript, can iterate, such as dict can iterate:

>>> d = {' A ': 1, ' B ': 2, ' C ': 3}>>> for key in D: ...     Print (key) ... ACB

since dict are not ordered sequentially, the resulting iterations may differ in order.

3, Dict iterative attention points:

(1) can be iterated by key (default), as above

(2) You can also iterate by value

For value in D.values ()

(3) You can also iterate both (Python special point)

For K, V in D.items ()

4, how to determine whether the iteration: using the Collections Module Iterable type to judge

>>> from Collections Import iterable>>> isinstance (' abc ', iterable) # Whether STR can iterate true>>> Isinstance ([i], iterable) # Whether the list can iterate true>>> isinstance (123, iterable) # integer can be iterated false

  

5, how to implement the C language subscript implementation: Python built-in enumerate functions can change a list into an index-element pair

>>> for I, value in enumerate ([' A ', ' B ', ' C ']):     ... Print (i, value) ... 0 A1 B2 C

  

Python Learning Notes (10)-Advanced features

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.