Python learning Summary (1)

Source: Internet
Author: User
Tags shallow copy

(1) I (not a beginner) Learn Python Methods
Before starting the text, let me talk about how to learn Python. I have been familiar with many languages, but the code 90% I wrote is C. With some basic computer knowledge, this guy is barely a beginner. What I am talking about here is the method for non-beginners to learn Python, or to promote the method for non-beginners to learn a new language. Hey hey, I am writing a program, is abstraction good? Not much gossip. First, you have to find a classic book for getting started with python, search for it online, read it, and read it only. I chose Beginning Python From Novice to Professional 2nd Edition. Then, read the book and read the preface first. We need to know the target audience and some forum functions of this book. There may also be some learning methods and extended materials. The preface of a book is easy to ignore, but sometimes it includes some important information, such as the partition table of a disk and the forwarding table of a router.
Next, I want to read the text. (here, I would like to thank the blog Park Team for teaching me how to use two full-width spaces for formatting. It is very practical ). Entry-level books are generally designed for beginners without languages. Since we are not a beginner, there are many theories that have been understood, such as variables, data types, and so on, there is no need to read these things carefully. We are concerned about the syntax, common data structures, and some special Python languages. My method is to skip the text and look at the example program. If you can understand it, you can change the example program on the machine and try the result. If there is no problem, continue to read it, if the example program is not understood or the program result is different from what I want, stop and read the description in the article. In this way, I think there are several advantages to reading the example in the book: 1. Do not miss some simple but important language details. For example, 1 and 2 are declared as a tuples, but print 1 and 2 will print 1 and 2. I used to suffer such a loss. It took me 5 minutes to read the shell syntax, and then I went to see a small project. The result was stuck in many places and I looked for information everywhere, the results show that many problems are basic Syntax problems, which are clearly embodied in the examples in the book. Although reading the book will take a lot of time, it is definitely a prerequisite for accumulation. 2. Feel the code style of this language in large quantities. 3. Learn a lot about basic functions at a very low cost. This saves some time to query function documents. For these three reasons, I like the book that emphasizes both code and text descriptions.
After reading the example, you can view the project. It is very important in practice! I'm glad that the book Beginning Python From Novice to Professional 2nd Edition has many small projects, which saves a lot of time. You can start to explore python without cutting back the firewood.
(2) Introduction to Python
Python is very free, simple syntax, and many function libraries. What I like most about Python is its indentation. Haha, it is all explained by indentation. It's amazing for me to read only clear code.
First, we should list some basic information that requires attention:
1. '/' is different from c. A single '/' is a floating-point division, and two delimiters '//' are the total division, and the total Division is also applicable to floating-point numbers, such as 3.5/2.2, the result is 1.0.
2. The multiplication symbol is **. It's fun to have two multiplication numbers.
3. Python directly supports big integer computation. I also use C to write a large integer computation algorithm, which is long.
4. input uses input and output uses print. These two items are flexible and fun.
5. The module is a bit similar to the header file, from math import sqrt, so that you can directly use the sqrt function. I remember when I was a buddy in middle school, I had ten questions under a big question. The teacher asked each question to write "solution: original =". Then, then the buddy directly put a braces and wrote a huge "solution: original =" outside the brackets ". Now, let's look at the same thing! The math here is the "solution: original = ".
6. string can be enclosed in single quotes or double quotation marks. There is no difference. Of course, single quotes and double quotes cannot be used in combination. syntax error may occur. I tried it. I remember I also tried it when I learned shell. Haha, nature! I wrote a simple shell Using C some time ago. I only wanted to use macro replacement to mix single quotes and double quotes, but this would cause problems, for example, in python, '"happy every day" yes' may cause confusion. But I have never thought of a better solution. The three quotation marks are used to separate a string into multiple rows for writing.
(3) Common Data Structure ---- list (list)
List is a linear structure, similar to arrays in C, but there are more methods than arrays. Some methods of list can be called to implement common linear data structures such as stacks and queues. It can be seen that list is very flexible. The following are the basic knowledge points of the list I summarized and some important points worth noting:
1. list can be modified, including addition, deletion, segment addition, and segment deletion. There are many methods and flexible application. I will not go into details about the specific functions that can be found everywhere.
2. The elements in the list can be of different types. For example, list1 = ['neilhappy ', 123]. This is different from the arrays in c and the containers in c ++. After the declaration, the Data Types in the containers are the same.
3. list can be accessed by subscript. For example, if list1 = ['A', 'B', 'C'], then list1 [0] = 'A '. You can also access an element. For example, list1 [0: 2] is a semi-open and semi-closed interval, left-closed and right-open. Therefore, the result is ['A', 'B']. You can also add a parameter to indicate the increasing number: list1 [], which is similar to matlab. I was pleasantly surprised to see that list can be accessed with negative subscript. List1 [-1] indicates the last element, that is, 'c '. Now, strlen, sizeof, and other functions can all have a slight rest.
4. Differences between string and list. Of course, string can be used as a list element. More importantly, string can be directly connected: "neil" "happy ". However, the list must have a plus sign (+): ['Neil '] + ['Happy']. The result is ['Neil ', 'happy']. Pay attention to this.
5. list multiplication. This is another feature that I like very much. When I declare an array of c in the past: int a [100] = {1, 2, 3, 4, 5, 5, 5, 5}, several 5, numbers are dazzling. Now, in python: a = [1, 2, 3, 4] + [5] * 6. Wow, the world is quiet.
(4) Common Data Structure ---- tuple (tuples)
Tuple is very similar to list, but tuple is like a list that is severely restricted. It is not dynamic (changed) and cannot be powerful (there are many methods ), the declaration method is tuple1 = (1, 2, 3) or tuple1 = 1, 2. The access method is similar to list.
(5) Common Data Structure ---- dictionary (dictionary)
Dictionary is actually a set of mapping, a hash. Important knowledge points:
1. form. Dict1 = {'name': 'Neil '}. Here, 'name' is the key, and 'Neil 'is the value. Dictionary is a set of key-value pairs.
2. Shallow copy and deepcopy. Similar to c ++, light copy is equivalent to the copy pointer, and deepcopy is to allocate the corresponding memory space while copying the pointer.
3. access method: the result of dict1 ['name'] Is 'Neil '. Note that there is no order concept in dictionary, so it cannot be accessed with a numerical subscript like list, and accessed with a key.
(6) Differences and relationships between the three data structures
Containers in python can freely include each other, as are the three data structures. List, dictionary can be modified, but tuple and string cannot be modified, but the key of dictionary must be changeable, that is, tuple and string. Imagine what will happen if all the dictionary indexes are modified at will?
Note that the data structures are different when brackets are different. List: list1 = ['Neil ', 'happy'], square brackets. Tuple: tuple1 = ('Neil ', 'happy'). Here is the brackets. Dictionary: dict1 = {'name': 'Happy '}, which is a braces. It is easy to write errors here, which may cause difficult bugs.
(7) Common Data Types ---- string
I remember Peter Weinberger said, "All C Programs do the same thing, observe one character, and then do nothing ". The importance of string is visible. Just like above, I will directly write my summary and notes.
1. The string access method is basically the same as the list and tuple methods. The string method is the same as the tuple method and cannot be modified. However, the string method is much more powerful than the tuple method. There are too many functions.
2. Use % to format the string. In fact, there is no big difference with C.
?
>>>
Str1 = "% s
% S every day"
>>>
Name = ('Neil ', 'happy ')
>>> Print str1 % name
Neil
Happy every day
(8) basic statements
There is nothing else to say. Write it directly:
1. print 1, 2, 3. This is fun. Print 1 2 3. Note that there are spaces.
2. print a or B. This statement is equivalent to if (! = 0) {print a}; else {print B}; simplified, so wonderful.
3. unpack mechanism: a wonderful mechanism. When I learned C, I thought about whether a function can return multiple values. Haha, python helped me implement it.
?
>>>
Tuple1 = (3, 2, 1)
>>>
X, y, z = tuple1
>>>
X, y, z
(3, 2, 1)
4. indent to break a sentence. I think this is the essence of python.
5. Control statement: if, elif, else, for, while, break, and continue, which are similar to shell, only multiple colons, Parentheses, and indentation are required. Just give a brief introduction.
?
If statement1:
Print something1
Elif statement2:
Print something2
Else:
Print something3
6. x is y. judge whether x and y are the same object. For example, it is clear at a glance. X = y determines whether x and y are equal, but the two objects that are equal are not necessarily the same object.
?
>>>
X = [1, 2, 3]
>>>
Y = [1, 2, 3]
>>>
X = y
True
>>>
X is y
False
7. x in y. Determine whether x is in y. Very simple, but very practical.
8. The else statement in the loop. When we want to judge that there is no number in a sequence, we often need to add a variable in the cycle to do this, for example:
?
Values = [1, 2, 4, 5]
Flag = 1
For xin values:
If x = 6:
Flag = 0
Break
If flag = 1:
Print "No! "
If it is changed to else in:
?
Values = [1, 2, 4, 5]
Flag = 1
For xin values:
If x = 6:
Flag = 0
Break
Else:
Print "No! "
I feel amazing.
9. Use for in list:
?
>>>
[X * xfor xin range ()]
[,]
(9) notes and notes during the learning process (From Beginning Python From Novice to Professional 2nd Edition)
Finally, my notes and notes are scattered, but I think they are useful.
1. There are multiple manifestations of null values: False, 0, None, "", {}, [], ().
2. Remember the Python motto of learning: use the source (which basically means read all the code you can get your hands on). read more source code.
3. Blocks are indicated through indentation, and only through indentation in python.
4. indented sentences make the code style more fixed.
5. Make sure your comments say significant things and don't simply restate what is already obvious from the code. write comments, absolutely no nonsense, absolutely no simple repetition of the meaning of the code.
6. any object can contain in other objects
7. If a function call returns a sequence, you can index it directly.
8. Check whether the list method modifies the list itself and whether the returned value exists. This is a deep impression only when it is used for multiple purposes.
9. Comparison between the strong types of python and those of java and c. Python, str1 = 1, and str1 = "aaa ". However, in c, int a = 1.1 directly truncates the fractional part.
10.
?
>>> 'Ad
C d'. translate (table, 'C ')
'CD
D 'note that 'C' is deleted first and then replaced.
11. print u'{rn ~ringsl then re' the preceding u is a practical unicode encoding.
12. in dictionaries, The in operator can only be used for keys but not for values.
13. Checking for key membership in a dictionary is much more efficient than checking for membership in a list. The difference is greater the larger the data structures are. But I don't understand why
14. In python, it is so simple to exchange two numbers. The exchange of x, y = y, and x and the exchange of three numbers is essentially completed by the exchange of two numbers.
15. variables must be assigned values before they can be used. This is also a sign of the strong python type?
(10) Summary
The above is my summary of some basic knowledge about python. I have a limited level. If there is an error, please point it out. Thank you very much.
Author: NeilHappy

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.