Python 3 day2 (top)

Source: Internet
Author: User

Day 2 (UP)

First, the initial knowledge of the module

Python's strength is that he has a very rich and powerful standard library and third-party library, almost any function you want to implement has the corresponding Python library support, later in the course will be in depth to explain the various libraries commonly used, now, we first to symbolically learn 2 simple. The standard library is what you use most often in Python, and a third-party library is the one you have to download and install before you can use it, such as Django.

The result prints the path of a row, what are these paths for? This is the path that Python calls a library, typically a third-party library is placed in sie-packages, and the standard library is placed below Lib:

Meet the OS module again

Let's look at the input results for 0, which is why? Because Dir, this line of code only executes commands and does not save the result:

If I do. To read the results, change the system to Popen, and then add a. Read () to remove the results, as shown below:

When we want to create a new directory, what should we do? It is simple to add a line of code for Os.mkdir ("New_dir") and click to run, the original directory from 3 to four:

After understanding the basic module, we come to see the magic of the module, first open before we write the passwd program, run, enter the user name and password, the display is welcome to login ..., if we call this passwd module directly in another PY program, Is it possible to run the same results? The answer is yes.

Isn't it amazing?

Ii. What is PYC?

1. Is python an interpreted language?

The first thing I heard about Python when I was a beginner python was that Python was an explanatory language, and I kept believing until I found the *.pyc file. If it is an interpreted language, what is the generated *.pyc file? c should be the abbreviation of compiled!

In order to prevent other people who learn python from being misunderstood by this remark, we will clarify this issue in the text and make some basic concepts clear.

2. Explanatory and compiled languages

Computers are not able to recognize high-level languages, so when we run a high-level language program, we need a "translator" to engage in the process of translating high-level languages into machine languages that computers can read. This process is divided into two categories, the first of which is compilation, and the second is interpretation.

A compiled language before a program executes, the program executes a compilation process through the compiler, transforming the program into machine language. The runtime does not need to be translated and executes directly. The most typical example is the C language.

The explanatory language does not have this process of compiling, but rather, when the program is running, it interprets the program line by row, then runs directly, and the most typical example is Ruby.

Through the above example, we can summarize the advantages and disadvantages of the explanatory language and the compiled language, because the compiler language before the program has already made a "translation" of the program, so at run time there is less "translation" process, so the efficiency is higher. But we also can't generalize, some interpretive languages can also be optimized by the interpreter to optimize the whole program when translating the program, thus more efficiently than the compiled language.

In addition, with the rise of virtual machine-based languages such as Java, we cannot simply divide the language into two types-----explanatory and compiled.

In Java, for example, Java is first compiled into a bytecode file by a compiler and then interpreted as a machine file by the interpreter at run time. So we say that Java is a language that is compiled and interpreted first.

3. What exactly is Python?

In fact, Python, like java/c#, is also a virtual machine-based language, let's start with a simple look at the Python program's running process.

When we enter Python hello.py on the command line, we actually activate the Python interpreter and tell the interpreter: You're going to start working. But before the "explain", the first thing that actually executes is the same as Java, which is compiled.

Students familiar with Java can consider how we execute a Java program on the command line:

Javac Hello.java

Java Hello

Just when we were using an IDE like Eclipse, we fused these two parts into a piece. In fact, Python is also the same, when we execute Python hello.py, he also executes such a process, so we should describe the Python,python is a first compiled after the interpretation of the language.

4. Brief description of Python's running process

Before we say this question, let's start with two concepts, pycodeobject and PYC files.

The PYC we see on the hard drive naturally doesn't have to say much, and pycodeobject is actually the result of a Python compiler actually compiling it. Let's just get to the bottom of it and keep looking down.

When the Python program runs, the result of the compilation is saved in the Pycodeobject in memory, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.

When the Python program runs for the second time, the program will first look for the PYc file on the hard disk, and if it is found, load it directly or repeat the process.

So we should be able to locate Pycodeobject and pyc files, we say that PYc file is actually a kind of persistent saving way of pycodeobject.

Third, the data type first knowledge

1. Digital

2 is an example of an integer.
Long integers are just larger integers.
3.23 and 52.3E-4 are examples of floating-point numbers. The e tag represents a power of 10. Here, 52.3E-4 means 52.3 * 10-4.
( -5+4j) and (2.3-4.6j) are examples of complex numbers, where -5,4 is a real number, j is an imaginary number, and what is the plural in mathematics?

int (integral type)

On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807long (Long integer)
Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite.
Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data.
Float (float type)        First Literacy http://www.cnblogs.com/alex3714/articles/5895848.html
A floating-point number is used to process real numbers, which are numbers with decimals. Similar to the double type in C, accounting for 8 bytes (64 bits), where 52 bits represent the bottom, 11 bits represent the exponent, and the remaining one represents the symbol. Floating-point numbers are represented as decimals, but decimals do not represent all floating-point numbers.
Complex (plural)
The complex number consists of real and imaginary parts, the general form is X+yj, where x is the real part of the complex, and Y is the imaginary part of the complex, where x and y are real numbers. Note: Small number pools exist in Python:-5 ~ 257 2. Boolean valueTrue or False 1 or 0 3. String    Hello World Note: String formatted output,String Common functions:
    • Remove whitespace
    • Segmentation
    • Length
    • Index
    • Slice
4. List Creation list:

Basic operation:

    • Index
    • Slice
    • Additional
    • Delete
    • Length
    • Slice
    • Cycle
    • Contains
5, tuples (immutable list) Create tuples:

6. Dictionaries (unordered) Create dictionaries:

Common operations:

    • Index
    • New
    • Delete
    • Key, value, key-value pairs
    • Cycle
    • Length
Iv. Data OperationsArithmetic operations:

Comparison operation:

Assignment operation:

Logical operation:

Member Operations:

Identity operation:

Bit operations:

Five. Byte data type

1.bytes type

2. Ternary operation

If the condition is true: result = value 1
If the condition is false: result = value 2

3. Binary:

    • Binary, 01
    • Octal, 01234567
    • Decimal, 0123456789
    • Hex, 0123456789ABCDEF binary to 16 binary conversion http://jingyan.baidu.com/album/47a29f24292608c0142399cb.html?picindex=1

Binary to hexadecimal method: take 4-in-1 method that is, from the binary decimal point is the dividing point, left (or right) every 4 or 1 bits represents a 16-bit. For example, 101110011011001 translates to hexadecimal as b9b9.

Note: hexadecimal notation: denoted by the letter H suffix, such as BH for hexadecimal number 11, can also be represented by a 0X prefix, such as 0x23 is hexadecimal 23.

Here you need to remind: in the left or right to take 4 bits, take the highest level if you cannot make up 4 bits, you can go to the left or right of the decimal point 0, the conversion.

For example: 10111.011 converted to hexadecimal number is 176.

Let's take a look at the hexadecimal turn binary, the reverse method is divided into four.

4.encode

Output Result:

5. List

Slice operations in the list:

The way to take the last value in the list:

But if we don't know the last value is how the last value should be taken (when there is more data), Print here (list name [-1]):

Similarly, [-2] is the second lowest value in the list, that is, the second value is taken from right to left:

A bit more complicated, what if I take the bottom 1 and 22 values, what should I do?

Why does this logic not correctly fetch the required values? Let's take a look:

However, we find that the value taken by this operation is not the desired reciprocal of 1, 2 digits, but a reciprocal of 2, 3 numbers, because Gu Tou the principle of the tail:

So the correct value should be [-2:]:

We can find that the sectioning process is 0 before and after the time can be omitted, the results of the operation is the same.

So what if I want to insert a new value into the list? There are two ways to introduce the direct append method, using the. Append:

The results of the operation are as follows, and the Leihaidong is successfully appended to the end of the list:

If you don't want to put the new values at the end of the list but randomly place them somewhere in the list, the answer is yes, for example now we want to insert Chen Ronghua in front of Guyun:

It is important to note that the insert operation can only be step-by-step and not bulk inserted.

Now we're talking about a replacement, and now the "add", "change", for example changing the gu to another value:

"Additions and deletions" are basic operations, the following describes how to delete the operation of a value:

These three methods can successfully delete the specified value, the result is as follows:

Finally talk about how to "check", if the whole list has more than 80 people, I want to know except Sheedi in which location, how should I inquire?

If there are two or several duplicate values in the list, you need to count the number of duplicate values, and count is used here to calculate the duplicate values of 2:

The rest of the reverse (reverse list), sort (sort), copy (copy) are explained in detail in the next session.

Python 3 day2 (top)

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.