Python Road, Day2-Dictionary

Source: Internet
Author: User
Tags create directory

One, the Python environment variable 1, import module load path

[' C:\\users\\123\\pycharmprojects\\untitled\\day2 ', '

C:\\users\\123\\pycharmprojects\\untitled ', '

C:\\users\\123\\appdata\\local\\programs\\python\\python35\\python35.zip ',

' C:\\users\\123\\appdata\\local\\programs\\python\\python35\\dlls ',

' C:\\users\\123\\appdata\\local\\programs\\python\\python35\\lib ' #Python standard library path,

' C:\\users\\123\\appdata\\local\\programs\\python\\python35 ',

' C:\\users\\123\\appdata\\local\\programs\\python\\python35\\lib\\site-packages ' #Python third-party library path]

2. SYS module
    • Print (sys.argv) script relative path
3. OS Module
  
    • Os.system ("dir") invokes the system command, executes the command, does not save the result, cannot be used as a variable

    • Cmd_res = Os.popen ("dir"). read ();print (cmd_res) Executes the result of the command as a variable
    • Os.mkdir ("test1") Create directory

Ii. What is a 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.

List

names = ["Zhangyang", "Guyun", "Xiangpeng", "Xuliangchen"]
Print (Names[0:3])  #切片, [0-3] Gu Tou regardless of the tail
Print (Names[-3:-1])
Names.append ("Test1") #添加, the default last
Names.insert (1, "Test") #插入,
NAMES[1] = "LB" #改


Del Names[1] #删除下标 1
Names.pop () #默认删除最后一个, subscript can be specified
Names.clear () #情况names列表

Print (Names.index ("Guyun")) #查找列表里guyun的下标
Print (Names.count ("Guyun")) #统计列表里有几个guyun

Names.reverse () #发转列表排序
Names.sort () #特殊符号, numbers, larger, lowercase sort
Names.extend (Names2) #合并列表

Name2=names.copy (names)
Name2=names.deepcopy (names)
#copy默认为浅copy, copy only the first layer, such as a child list and a child list after copy, to the same memory address.
Deepcopy full copy list, deep copy

Python Road, Day2-Dictionary

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.