Python Learning Notes (python 3)

Source: Internet
Author: User
Tags python list
first, tuple (yuan group)

Tuple and list are very similar, but tuple cannot be modified once initialized, such as:

Classmates = (' Michael ', ' Bob ', ' Tracy ')

Classmates this tuple can not be changed, it also has no append (), insert () such a method. The other way to get the element is the same as the list, you can use classmates[0],classmates[-1 normally, but you can't assign a value to another element.

What is the meaning of immutable tuple. Because the tuple is immutable, the code is more secure. If possible, use tuple instead of list and try to use tuple tuple traps.

To define a tuple that has only 1 elements, if you define this:

t = (1)
T
1

The definition is not tuple, but the number 1. This is because the parentheses () can represent both the tuple and the parentheses in the mathematical formula, which creates ambiguity, so Python stipulates that in this case, the calculation is done in parentheses, and the result is naturally 1.
Therefore, only 1 elements of the tuple definition must be added with a comma, to eliminate ambiguity:

t = (1,)
T
(1,)

Python also adds a comma when displaying a tuple of only 1 elements, lest you misunderstand the parentheses in the meaning of the mathematical calculation.

Finally, let's look at a "variable" tuple:

t = (' A ', ' B ', [' A ', ' B '])
t[2][0] = ' X '
t[2][1] = ' Y '
t
(' A ', ' B ', [' X ', ' Y ']]
Second, Python list type slices 1. Basic Usage

①con[start_index]: Returns an object with an index value of Start_index

②con[start_index:end_index]: Returns a contiguous object with an index value of Start_index to End_index-1

③con[start_index:end_index:step]: A continuous object that returns the index value between Start_index and End_index-1, and the difference between the index value and the Start_index can be divisible by step

2. Default Usage

①con[start_index:]: Default End_index, which means starting from Start_index to the last object in the sequence

②con[: End_index]: Default Start_index, representing fragments from the first object in the sequence to the End_index-1

③con[:]: Default Start_index and End_index, representing the complete fragment from the first object to the last object

④con[::step]: Default Start_index and End_index that represent the value of a rule that the entire sequence is divisible by the step in the index

you can do slices with negative numbers. Negative numbers are used at positions beginning at the end of the sequence

S[::-1] can be considered a rollover effect

3, two-dimensional ndarray slices in numpy

For the specific use of one-dimensional data and similar in Python

For two-dimensional numeric usage, x[:,:] Separate two dimensions with commas, and for each dimension usage, take the following two-dimensional array as an example:

x[:,1] takes all values in the x-axis direction, and the Y axis equals 1, resulting in:

x[1:3,1] Take x axis x greater than or equal to 1, less than 3,y axis equals 1, Result:

simple use of lambda expressions

For simple functions, there is also a simple representation, namely: lambda expression

# ###################### normal function ######################
# definition function (normal)
def func (ARG): return
    arg + 1

# Executive function C5/>result = func (123)

# ###################### Lambda ######################

# definition function (lambda expression)
My_ Lambda = lambda Arg:arg + 1

# executive function Result
= MY_LAMBDA (123)
Four, __call__
Class X (object):
    def __init__ (self, A, B, range):
        self.a = a
        self.b = b
        self.range = range
    def __call_ _ (self, A, b):
        SELF.A = a
        self.b = b
        print (' __call__ with ({}, {}) '. Format (SELF.A, self.b))
    def __del__ (s Elf, A, b, range):
        del self.a
        del self.b
        del self.range
>>> xinstance = X (1, 2, 3)
>>> xinstance (1,2)
__call__ with (7, 8)
Related Article

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.