Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic knowledge points of Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the basic knowledge points of Python". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the basic knowledge points of Python.

Python comment

Single-line comments in python begin with #.

Multiline comments in python use three single quotation marks ('') or three double quotation marks ("").

Python string

1. A string is any text enclosed in single or double quotation marks, such as' abc', "xyz, and so on. Note that''or''is only a representation in itself, not part of a string, so the string 'abc' has only three characters: a _ line _ b _ line _ c. If 'itself is a character, then it can be enclosed with "". For example, the characters contained in "Isimm OK" are six characters, I. E., I. E., I. M., spaces, and O.

two。 If there are many line breaks inside the string, it is difficult to read on one line with\ n.For simplicity, Python allows the use of''...''. Represents multiple lines of content in the format of

Python function

To define a function, use the defstatement, write the function name, parentheses, parameters in parentheses, and colons:, then write the function body in the indented block, and the return value of the function is returned with the argument statement.

Def my_abs (x): if x > = 0: return x else: return-xPython module

In Python, a .py file is called a Module.

In order to avoid module name conflicts, Python introduces a method of organizing modules by directory, called Package.

Import module:

Import sys scope

In a module, we may define a lot of functions and variables, but some functions and variables we want to give to others, and some functions and variables we want to use only within the module. In Python, this is done with the _ prefix.

Normal function and variable names are public and can be directly referenced, such as abc,x123,PI, etc.

Variables such as _ _ xxx__ are special variables that can be referenced directly, but have special uses. For example, the above _ _ author__,__name__ is a special variable, and the documentation comments defined by the hello module can also be accessed with the special variable _ _ doc__. Our own variables generally do not use this variable name.

Functions or variables such as _ xxx and _ _ xxx are private and should not be directly referenced, such as _ abc,__abc, etc.

Python Dictionary (Dictionary)

Dictionaries are another variable container model and can store objects of any type.

Each key value key= > value of the dictionary is divided by a colon: split, each key value pair is separated by a comma, and the entire dictionary is included in curly braces {} in the following format:

Built-in functions:

Serial number

Function and description

one

Cmp (dict1, dict2)

Compare two dictionary elements.

two

Len (dict)

Calculates the number of dictionary elements, that is, the total number of keys.

three

Str (dict)

The printable string representation of the output dictionary.

four

Type (variable)

Returns the input variable type, or the dictionary type if the variable is a dictionary.

Built-in method:

Serial number

Function and description

one

Dict.clear ()

Delete all elements in the dictionary

two

Dict.copy ()

Returns a shallow copy of a dictionary

three

Dict.fromkeys (seq [, val])

Create a new dictionary, using the elements in the sequence seq as the keys of the dictionary, and val as the initial value of all the keys in the dictionary

four

Dict.get (key, default=None)

Returns the value of the specified key, if the value does not return the default value in the dictionary

five

Dict.has_key (key)

six

Dict.items ()

Returns a traverable array of (keys, values) tuples as a list

seven

Dict.keys ()

Returns all the keys in a dictionary as a list

eight

Dict.setdefault (key, default=None)

Similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default

nine

Dict.update (dict2)

Update the key / value pair of the dictionary dict2 to dict

ten

Dict.values ()

Returns all values in the dictionary as a list

eleven

Pop (key [, default])

Delete the value corresponding to the given key key of the dictionary, and the return value is the deleted value. The key value must be given. Otherwise, the default value is returned.

twelve

Popitem ()

Returns and deletes the last pair of keys and values in the dictionary.

Python object-oriented 1. Create a class

Class ClassName: 'help for classes' # Class documentation string class_suite # Class body

The help information for the class can be viewed through ClassName.__doc__.

The _ _ init__ () method is a special method called the constructor or initialization method of a class, which is called when an instance of the class is created

Self represents an instance of a class, and self is necessary when defining the method of a class, although it is not necessary to pass in the corresponding parameters when calling.

Self represents an instance of a class, not a class

There is only one special difference between methods of a class and ordinary functions-they must have an extra first parameter name, which by convention is self.

Self represents an instance of the class and represents the address of the current object, while self.__class__ points to the class.

two。 Access Properti

Use a period. To access the properties of the object

Getattr (obj, name [, default]): access the properties of an object.

Hasattr (obj,name): check to see if an attribute exists.

Setattr (obj,name,value): sets a property. If the attribute does not exist, a new attribute is created.

Python built-in class properties

_ _ dict__: the attribute of the class (contains a dictionary consisting of the data attributes of the class)

_ _ doc__: the document string of the class

_ _ name__: class name

The module in which the _ _ module__: class definition resides (the full name of the class is'_ _ main__.className',. If the class is in an import module mymod, then className.__module__ equals mymod)

_ _ bases__: all parent class constituent elements of a class (contains a tuple of all parent classes)

Python object destruction (garbage collection)

Python uses the simple technique of reference counting to track and collect garbage

The destructor _ _ del__, _ _ del__ is called when the object is destroyed, and the _ _ del__ method runs when the object is no longer in use

Inheritance of the Python class

A new class created by inheritance is called a subclass or derived class, and the inherited class is called a base class, parent class, or superclass.

Inheritance syntax

If more than one class is listed in the inheritance tuple, it is called multiple inheritance.

Syntax:

The declaration of a derived class is similar to that of their parent class, and the list of inherited base classes follows the class name, as follows:

Class SubClassName (ParentClass1 [, ParentClass2,...]): Python basic overloading method

Serial number

Method, description & simple call

one

_ _ init__ (self [, args...])

Constructor function

Simple calling method: obj = className (args)

two

_ _ del__ (self)

Destructing method to delete an object

Simple calling method: del obj

three

_ _ repr__ (self)

Into a form that can be read by the interpreter

Simple calling method: repr (obj)

four

_ _ str__ (self)

Used to convert values into a form that is suitable for human reading

Simple calling method: str (obj)

five

_ _ cmp__ (self, x)

Object comparison

Simple calling method: cmp (obj, x)

Private properties of Python class and method class

_ _ private_attrs: start with two underscores, declaring that the property is private and cannot be used or accessed directly outside the class. Self.__private_attrs when used in methods within a class.

Methods of the class

Inside the class, you can use the def keyword to define a method for the class. Unlike the general function definition, the class method must contain the parameter self and be the first parameter.

Private methods of the class

_ _ private_method: start with two underscores, declaring that the method is private and cannot be called outside the class. Call self.__private_methods inside the class

Single underline, double underline, head and tail double underline description:

_ _ foo__: defines a special method, usually a system-defined name, such as _ _ init__ ().

_ foo: a variable of type protected that begins with a single underscore, that is, a protected type can only be accessed by itself and subclasses, and cannot be used for from module import *

_ _ foo: the double underscore represents a variable of private type (private), which can only be accessed by the class itself.

Web development

WSGI:Web Server Gateway Interface

Python has a built-in WSGI server, a module called wsgiref, which is a reference implementation of a WSGI server written in pure Python. The so-called "reference implementation" means that the implementation is fully compliant with the WSGI standard, but does not consider any running efficiency and is only for development and testing.

Because it is easy to develop a Web framework with Python, Python has hundreds of open source Web frameworks

At this point, I believe you have a deeper understanding of "what are the basic knowledge points of Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report