Python initial experience (1)-Basics

Source: Internet
Author: User

Why do I learn python?

First, my first language is C/C ++ (Object-Oriented subset ). On the one hand, I chose to learn Python because many people say that python development is highly efficient, so I want to verify it. On the other hand, Eric S. RaymondArticle: How to Become a hacker's recommendation for python. On the other hand, the design philosophy of Python: it is better to use only one method to do one thing, which I agree.

Now, the entry material I selected is a concise Python tutorial. Below are some notes and thoughts on my study, with obvious traces of C and C ++.
Control Structure:

    1. There is a natural string concept that C does not have.
    2. I ++ is not supported. I welcome this. ++ Will induceProgramNew Employee makes mistakes. I basically abandoned ++ in C.
    3. Python does not have a switch. You can use if Elif else to implement the same function.
    4. While can be followed by an else clause.
    5. For I in range ():, from 1 to 5, but not 5.
    6. The break statement can still stop the loop. However, after the loop is stopped, the else block is not executed.
    7. Continue is the same as before.
    8. Function input parameters have no type. If the real parameters and the form parameters do not match, how can we avoid them. If the type does not match, the Operation will fail.
    9. Global can tell the function that the variable is outside the function. However, global variables are not encouraged. It can declare multiple.
    10. Default parameter: similar to C ++, it is represented by "= value. Unified, it can only set the default parameters for the last parameter.

Data structure:

Dictionary:

A dictionary is similar to a binary tree, but does not know what the dictionary uses internally.Algorithm.

The key value must be unique. If it is not unique, what will happen when it is added? Linux Binary Trees are covered.

Answer: If a key already exists in the dictionary, it will overwrite the original value. It is set. This is consistent with the binary tree in Linux.

Similar to Binary Trees, keys cannot be changed. What are the consequences of a change. At least after the binary tree changes, the structure of this number will be corrupted. Moreover, the compiler cannot detect the vulnerability. Will Python check?

Answer: Yes. An error is prompted during running. Because he does not have the concept of compilation. So...

Can I get a variable as a keyword?

Answer: This is acceptable, and there is no problem with running. But what if I change this keyword?

But what if I change this keyword?

Answer: After the change, the dictionary does not change. It means that he only uses the value of this variable, takes its value as a constant, and uses it as a keyword instead of a variable as a keyword.

Is the dictionary efficiency good? What algorithm is used internally? Binary Tree, hash, or others?

Answer: Hash table.

Can dictionary keys be of different types?

Answer: Yes.

A dictionary is a dict object, a tuples are tuple objects, and list objects are displayed in the list.

Sequence:

Two features of a sequence are index operators and slice operators. One is to get a project, and the other is to get a subsequence.

The list and metadata are sequences.

The sequence starts from zero. This is the same as an array. But he can be a negative number !?

Answer: When a negative number is used, it indicates removing the last element, and-1 indicates the last element.

What is slice?

Answer: Obtains a subsequence in a sequence. For slice, the colon is required, and the number is not required. If there is no number, it indicates starting from or ending from scratch. No number indicates the entire sequence.

Does slice assign values to a new sequence?

Answer: Yes.

What if the retrieved element is out of the range?

Answer: An error occurred while running.

What if the slice is out of the range?

Answer: It was originally thought that an error would occur, but it was not displayed, but it was blank.

List, tuples, and strings are all sequences. You can use the sequence method to operate them.

tuples

the only difference from list is that it cannot be changed.

unlike C ++, values of different types can be stored in tuples and lists.

myempty = () indicates an empty tuples.

myempty = (2,) You must add ',' to indicate that only one tuple exists; otherwise, it is an object.

object and reference

when you create an object and assign it a variable, this variable only references that object, rather than the object itself! That is to say, the variable name points to the memory of the object stored in your computer. This is called name-to-object binding.

I am familiar with pointers in C. They should be equivalent to pointers, but they can't be opened from the appearance. I feel that this feature is somewhat different from the original intention of the language: it may lead to errors for programmers.

general feeling: The syntax is indeed simple and easy to start.

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.