Python Overview: Python in the eyes of C ++ programmers

Source: Internet
Author: User
Tags floor division

In C ++ProgramScript is a wonderful logic. Of course, it may be amazing to me. However, python is not that difficult to understand from Lua. Record what you should pay attention to when learning python. It is also a quick start. If you came from C ++, you would be right.
To avoid script flash, add raw_input (). This is equivalent to Getline.
You can directly type filename. py in the command line to run the script. The import module uses import modulename. Note that there is no. py. Then use modulename. ATTR to use the corresponding attributes. You can also use the from module import ATTR command to directly use attributes. The script is run when the module is imported for the first time, but the script is not imported for the second time. You can use reload to rewrite the import module and run the module. In interactive mode, you can use Dir (modulename) to list the attribute names in the module.
Python built-in object Number: ** Represents the multiplication party. Import math to the math library. Common methods such as PI and SQRT can be used.
Import random to import the random number library. Use random. Random () to generate a random number ranging from 0 to 1. You can also select random. Choice ([1, 2, 3, 4]) in the list. Random.int (), which generates a random number from 1 to 10. String: The string is a constant and cannot be changed. You can only assign a value again. Len () calculates the length. Supports random subscript access. You can use s [-1] to represent the reciprocal, which is equivalent to s [Len (S)-1]. Slice is used to split strings. The format is X [I: J]. Indicates that the content from I to J in X is taken out, and the left side is open and right closed. If I and j are empty, 0 and Len (x) are displayed ). Of course, I and j can be negative.
You can use '+' to connect strings, for example, S = S + 'A '. S has created a new object. Use Find () for string search, and return the offset position. String replacement use replace to return the replaced string. Use the split function for string split. The split result is saved to a list. The string has some basic operations, such as Upper, isalpha, and rstrip. A complex text can be defined using three quotation marks. These characters are converted into corresponding strings, including tabs and other symbols. It can also be used as a comment, equivalent /***/

Help: The Dir (variable) method can list all attributes and methods of an object. You can use the help command to explain the specific method. For example, help (S. index ). Pattern Matching: Import re. This involves a regular expression. Check it again. List: The list can be indexed, sliced, and other operations, just like a string. For example, use [:] and [-1. Lists are somewhat similar to arrays, but they are much more powerful. The list does not have fixed type constraints. You can also increase or decrease the size as needed to respond to specific operations. There are operations such as append, Pop, '+', sort, and reverse. List parsing creates a new list by running an expression in a list. It is written in square brackets and has expressions and loop structures. It is a bit like an SQL statement. Shape: [Func (value) for value in list [statment] This statement will assign values to the elements in each list, check whether the conditions in statment match, run func (value), and create the value as an item in the new list. For example, [row [1] For row in m if row [1] % 2 = 0]. Dictionary: The representation is {key: value, key: value ...}. Value can be any data type, such as list. The memory is automatically managed and does not need to be released. The dictionary output is not ordered. I guess the internal implementation method is tree. For fixed order output, you can sort keys first, and then use the for key in keys: method. Or directly use the sorted () method. For example, for key in sorted (d ):.
You can use. has_key to determine whether a corresponding key exists.
Tuples: It cannot be changed. Others are similar to the list.
File: Open (filename, weight ). The default permission is 'R' to create or open a file. Write () to write the file. Read () read the file content. Note that only one read is allowed. In python, the calculation order is determined based on the operator priority. The table operator has a higher priority than the latter operator. Brackets are still recommended.
Floor division. The Division value after the fractional part is discarded, that is, the part of the % operator.
In python, the integer length is arbitrary. When the length exceeds int, it is automatically converted to a long integer.
Supports bit operations. The octal value starts with 0 and the hexadecimal value starts with 0x. Oct (Num), Hex (Num), INT () can convert numbers.
Script Dynamic Object implementation logic: Each declared variable name has no actual type,The variable name is a void pointer pointing to an object.. On the other hand, the object knows its own type, and each object contains a header information, marking the type of this object. Assignment has three operations: Create an object, create a variable, and connect a variable to an object.
When the variable name points to the new object, the memory of the old object is recycled (this will be a ghost if it is not slow ).
A variable cannot point to a variable. It can only point to objects. Therefore, when values are assigned between two variables, the actual meaning isConsistent. They actually share objects. In this way, there will be no memory access problems, at the cost of being inefficient.When the original variable is changed rather than the object, there will be differences. Therefore, for list L2 = L1, when the value in L1 changes, the object changes and the variable does not change, so the L2 value also changes, if you do not want this, you can simply use the sharding technology: L2 = L1 [:]. If it is a dictionary, We need to import copy and use the copy. Copy () function.
Use the "=" operator to test whether two referenced objects have the same value. "Is" operator to check whether the object points to the same object. Note that small numbers and strings are reused in the cache. Therefore, the is statement is true.
This is probably written here, and may be added here later. We will write a new article if necessary.

This blog from a shura Road, reproduced please note the Source: http://blog.csdn.net/fansongy/article/details/8986698


 

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.