The basic idea of cross-language learning and the fundamental learning of Python

Source: Internet
Author: User
Tags thread class throw exception

I am a C # born, university four years majoring in C #, working three years is also C # language development. But in the school other languages also have the corresponding curriculum, eg:java,php,c++ have learned, of course, just learn fur (university, you know), strictly speaking may not be introduced, but these language code reading is not a problem, after all, comprehend by analogy, saying goes "A law pass, Wan Faming", It's no harm to learn more than anything else.

Recently seconded to other projects, because the new project is Python, I do not have Python development experience (just know the Language), the duration of more than two weeks, it is simple, time is tight, task heavy, and the development environment is completely unfamiliar, so you have to quickly learn to use a new language. How to learn across languages becomes a problem that must be considered. I'll start with a little bit of my thinking for reference.

First: environmental construction. This is the first place, if the environment is a problem, the future development is basically empty talk. Generally not recommended to go online to see the tutorial, this time directly ask the old staff, or let him help. (Time is tight, if the time is ample, you can try to build the development environment)

Second: Familiarize yourself with the IDE. First, try to use the IDE as recommended by the project team, and avoid using other Ides to cause problems when the problem occurs, unanswered. Of course, if there is an expert directly in the development of a text editor (mainly in the interpretation of language or scripting language development), you can ignore this article. Familiar with the IDE, on the one hand familiar with the interface, which part is what function, on the other hand familiar with shortcut keys, improve development efficiency. Commonly used shortcut keys are not much, only choose their favorite shortcuts intensive training.

Third: Learn the language. Language learning has a quick, slow-down. Time allows the situation, the proposal slowly, will be a solid foundation. When time is not allowed, work-oriented, what you need to learn. How to crash, I also groped a little experience.

1. Basic types of language. Because it is a cross-language learning, your mind must have other language interference, this do not care, you need to care about the differences between the basic types, personal advice to have a comparative study. It is a good habit to record the differences and not just memorize the brain and read the notes.

2. Basic grammar of the language. Most programming languages have only 3 programming logic, judging, looping, and jumping. You need to take shorthand for all of the three logic's notation and variants. The following is an example of C #:

Judgment is generally if-else or switch-case, conditional judgment and value judgment.

Loops are generally for, foreach, while, do-while, known or specified lengths of loops and convenience loops.

Jumps are usually break, continue, return, goto (goto is taboo usage, unless it is clearly logical to run, otherwise not recommended), the loop jumps and values are returned.

3. Common operation. Commonly used operations are string processing, type conversion and formatting data, common methods of operation of each data type, file, folder and path operation, read/write stream operation, database operation, network communication and multithreading. familiar with the corresponding operation method and method overloading.

Through the above steps of learning to strengthen, in the short term to understand a new language is no problem. Of course, development is not only to understand, more important is to write out, this need to do their own, who can not help you, must write their own, a lot of writing. Only in this way can the short-term crash. In fact, the above experience also applies to the 0 basis of learning, but the difficulty will be geometric times the rise, and also does not recommend 0 basis for people to choose the crash. In addition, less to see some tutorials, directly to find language-related APIs, this is the most direct and rude approach, because many of the tutorials are non-original, and no one practice, and even not suitable for your environment, these are likely to slow down your learning progress.

-------------------------------------------------------separators: The following is a personal Python basic learning note, For reference only------------------------------------------------------------------

Data type:

There are five standard types of data types:
Numbers (numeric) string (string) list (list) Tuple (tuple) Dictionary (dictionary)
Numbers can be defined in complex numbers, A+BJ, A-BJ (A, B, real, imaginary), and J can be uppercase or lowercase.

The list of lists, unlike the list in C #, is the same type of list in C #, not Python.
eg
List = [123, ' 1354 ', "78e", 12.5],[32,254, "WEWR", 22.5];
Print list;//all output
Print list[0][1:3];//Returns a list of elements that precede the second element of the first list to the fourth element, note that the pre-closed interval [' 1354 ', ' 78e '] (3-1) elements
Print list[1][0];//returns a specific value
Print list[1][1:];//Returns a list of all elements that start with the second list.

Strings can be used as list operations, and rules are the same as list
Tuples with parentheses, list with brackets, dictionary with curly braces. Tuples do not support updates, List supports updates
A dictionary is a key-value pair, one-dimensional. The list is multidimensional.
The dictionary is unordered and the list is ordered

Common Methods :

Range (start,stop,step=1) returns a list from start to stop (not included), accumulates step, commonly used for loop Eg:range (2,5,2) results [2,4]
String manipulation:
1.strobj.count (search string, start position, end position) returns the number of occurrences of the retrieved string in the original string
2.strobj.endswith (search string) returns whether to end with a search string, and its overloaded methods are not recommended.
3.strobj.find (search string) returns the index of the search string if there is no return-1
4.strobj.index (search string) returns the index of the search string, if no throw exception exists
5.strobj.replace (replaced by, replaced by)
6.strobj.strip ([string]) By default removes whitespace at both ends of the string, specifying the argument, and removing the specified string

List operations:
1.list.count (obj) returns the number of occurrences of obj in the list.
2.LIST.__LEN__ () returns the length of the list
3.list.extend (SEQ) inserts multiple elements into the list. Populate another list with one list
4.list.insert (Index,obj) Inserts an object into the list at the specified location
5.list.pop ([index]) returns the last element (like a stack's advanced post-out) by default, and deletes the element. Returns the specified object when the index is specified.
6.list.sort ([func]) is sorted by the default method or by the method provided.


File operation:
1.open ("File name", "mode of Operation"). Returns a File object. The file default is the current path, you can specify the path to note the way of writing, the operation of the common "r+", "w+", "A +", are read and write operations only the initial position of the cursor is different.
2.fileobj.close () Closes the current file object. When a file object is re-assigned to another file, the previous file is closed, but it is recommended to use close.
3.fileobj.write () can write binary data or text. Corresponding to open the file using binary mode. This method does not automatically add a line break at the end of the string.
4.fileobj.read ([bytes]) All content is read out without parameters. Specifies the length at which the content of the specified length is read and the cursor rests at that position.
5.fileobj.tell () Gets the position of the cursor within the file
6.os.rename (current name, new name) renaming a file requires an OS entry
7.os.remove (file name) delete files need to be introduced into OS
8.fileobj.next () Gets the contents of the next line
9.fileobj.readline ([bytes]) reads the entire line, including "\ r \ n", if the specified number of bytes, returns the specified length of the cursor position

Folder operations:
Need to introduce OS module
1.os.mkdir () Create a folder under the current directory
2.os.chdir (path) can jump to a specified path note the way the specified path "\" needs to be escaped in the string
3.OS.GETCWD () Displays the current working path Os.getcwdu () displays the Unicode object for the current working path
4.os.rmdir () Delete the folder. You must now empty all of the files when you delete them.
5.os.chmod (directory, permissions) use Modify directory permissions with caution
6.os.listdir (path) returns a list showing folders and files in the specified directory
7.os.removedirs (path) to delete a directory recursively

Regular Expressions:
Need to introduce RE modules
1.re.match (expression, content [, matching mode]) attempts to match from the starting position of the string, and the match returns a matching object successfully, if the match is not returned to none;
2.re.search (expression, content [, matching mode]) scans the entire string, returns the first matching result, and returns none if no match is reached
3.re.sub (expression, replace with string, original string [, replacement number, matching pattern])

Network communication socket:
Need to introduce a socket module
Server-side
1.socket () Creating a Socket object
2.sobj.bind (host name, port number) bind address socket
3.sobj.listen (maximum number of connections) Start listening on the port and specify the maximum number of listeners
4.sobj.accept () Blocking accept TCP Requests
Client
5.sobj.connect (host name, port number) to connect to host
General methods
6.SOBJ.RECV (buffer size) to receive data, returned as a string
7.sobj.send (String) Send string
8.sobj.close () Close socket

Multithreading:
Need to introduce the thread module
Thread.start_new_thread (thread function, parameter tuple [, optional parameter]) start a new thread
Need to introduce threading modules
Threading.currentthread () returns the current thread variable
Threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends
Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).
The thread class inherits the thread class, and it is recommended that you override the _init_ (), run () method, and that the descendant class does not actively call the parent class constructor
Method of Run () thread activity
Start () Startup thread
IsAlive () returns whether the thread is active
GetName ()
SetName ()
Join ([timeout]) waits for the thread to abort (call Abort, exit gracefully, or throw an exception). If you set the time-out period, the thread is no longer blocked if there is no blocking thread within the timeout period. In practice, in the case where the previous thread is not completed, the next thread sets a time-out period from the end of the previous thread.
Thread synchronization: Threading.lock () creates a thread lock object
Tlock.acquire () plus lock
Tlock.release () Unlock
The code that allows only one thread to run is placed between the two methods of the lock object.

Json:
Need to introduce a JSON module
Json.dumps (object) Converts an object to a JSON string
Json.loads (String) converts a JSON string into a Pytho data type from the output result is a dictionary

Object-oriented Python

Python has an object-oriented idea at the beginning of its design.
The common concepts of classes, methods, inheritance, objects, instantiation are also present in Python. However, it is important to note that class variables (or properties) are not the same as property variables in common object-oriented languages.
The most important feature of class variables in Python is that they are common throughout the instantiation, so it is not recommended to define variables in the class. Similar to static public variables in C #. When used inside a class, it is called with a dot in the class name.
A constructor is a specific __init__ () that differs from the construction method of C # or Java.
The Declaration implementation of the method is also different, and the DEF keyword represents the declaration method, the method must have an extra parameter, the first parameter name is self, which represents an instance of the class. You do not need to pass a value on the parameter when you call it. Self is not a keyword, and it can be performed normally with Runoob.
Creating an instance does not use new to instantiate the object, the direct class name is enclosed in parentheses, and the call is similar to the method.
You can use the DEL keyword to reclaim objects, or you can use destructors __del__

Inheritance, Python is multi-inherited, and the declaration uses parentheses to wrap the parent class. Python's Inheritance features:
1. Subclasses do not automatically invoke the constructor of the parent class and need to be called manually in the constructor of the subclass.
2.python calls the method based on the type, and if it does not exist in the subclass, it tries to invoke the method of the parent class. Primarily for method overrides.

Both private fields and private methods start with two underscores and are accessible only internally.

-------------------------------------------------------PS------------------------------------------------------------

General talk, not worth a sinewy, only for the discussion.

The basic idea of cross-language learning and the fundamental learning of Python

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.