Python Basics 10 Looking back

Source: Internet
Author: User

From the initial "Hello world", go to object-oriented. It's time to look back and see if there's anything missing from the tutorial.

We mentioned a word before, "everything is Object". So let's take a deep look at this sentence.

Need to first introduce two built-in functions,dir () and help ()

Dir () is used to query all properties of a class or object. You can try it.

>>>print dir (list)

Help () The description document used to query. You can try it.

>>>print Help (list)

(list is a python built-in class that corresponds to the lists we've explained earlier)

list is a class

On top and see, the table is a class that Python has already defined. When we create a new table, for example:

>>>NL = [1,2,5,3,5]

In fact, NL is an object of the class list.

To experiment with some list methods:

>>>print Nl.count (5) # count, see how many of them in total 5

>>>print Nl.index (3) # Query The subscript of the first 3 of NL

>>>nl.append (6) # Add a new element at the end of NL 6

>>>nl.sort () # Sort the elements of NL

>>>print Nl.pop () # Removes the last element from NL and returns the element.

>>>nl.remove (2) # Remove the first 2 from NL

>>>nl.insert (0,9) # Insert 9 at position Subscript 0

In summary, list is a class. Each list belongs to that class.

The Python supplement has an appendix to the list of common methods.

operator is a special method

When using Dir (list), you can see an attribute, which is __add__ (). From the formal perspective is a special method (underline, underline). Where is it special?

This method defines the meaning of the "+" operator for the list object, and the actions that occur when the two list objects are added together.

>>>print [+] + [5,6,9]

operators , such as +,-,, <, and subscript references [start:end], and so on, are essentially methods defined within the class.

Try

>>>print [+]-[3,4]

there will be an error message stating that the operator "-" is not defined. Now we inherit the list class and add the definition of "-"

classsuperlist (list):def __sub__(self, b): a=self[:] # Here, Self is the object of Supelist. Since Superlist inherits from list, it can use the same reference method as list[:] to represent the entire object. b=b[:] whileLen (b) >0:element_b=B.pop ()ifElement_binchA:a.remove (element_b)returnaPrintSuperlist ([i])-superlist ([3,4])

The built-in function len () is used to return the total number of elements contained in the list. the built-in function, __sub__ (), defines the action of "-": Remove elements from the first table that appear in the second table. if __sub__ () is already defined in the parent class and you define it in the subclass, then the object of the child class will refer to the definition of the subclass, not the definition of the parent class. The same is true for any other attribute.

(A list of special methods is also given at the end of the tutorial)

Defining operators is useful for complex objects. For example, humans have multiple attributes, such as name, age, and height. We can define human comparisons (<, =) to look at age only. This allows you to increase the number of non-existent operations on the object for your own purposes.

Next Step

Hopefully you've got a basic understanding of Python. You may be tempted to write some procedures to practice. It will be good for you.

But the big part of Python's power is that it provides a lot of objects that are already written and ready to use. We've seen the built-in list, the tuple, and so on. They are easy to use. In Python's standard library, there are a number of objects that can be used for operating system interaction, Internet development, multi-threading, and text processing. And on top of all of these, there are a lot of external libraries that define richer objects such as NumPy, Tkinter, Django, etc. for scientific computing, GUI development, web development libraries, and a wide variety of objects. For the average user, it's much easier to use these libraries than to start from scratch. We're going to start climbing the Giants ' shoulders.

Thank you for your attention,

Welcome to the world of Python.

Summary

Len () dir () help ()

The data structure list (list) is a class.

Operator is a method

Python Basics 10 Looking back

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.