Python Basics 1, Python module
### sys模块#!/usr/bin/python# _*_ coding:utf-8 _*_# Aothr: Kimimport sys #导入sys模块print (sys.path) #打印python环境变量print(sys.argv) #打印脚本相对路径,执行的时候可以传入参数print (sys.argv[2]) #取出传入的参数
###os模块import osprint(os.system("dir")) #调用系统命令cmd_res = os.popen("dir").read() #使用cmd_res变量存储执行结果,这里命令的执行结果会指向一个内存地址,所以需要加.read去读取结果。print("-->",cmd_res)os.mkdir("new_dir") #os模块中的创建目录方法
Third-party library, the default is to find the current directory, and then find the python environment variable if the module name exists.
2. Python PYC
Pre-compiled byte code
(1) Python is an interpreted language?
Beginner python has always heard that Python is an explanatory language until the existence of the. pyc file is discovered. 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) interpreted 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?
Python is also a virtual machine-based language, so let's start with a superficial 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.
Python is a language that is compiled and interpreted first.
(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.
3, Python data type 3.1, int (integer)
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
3.2, long (length integer)
自从python2.2起,如果整数发生溢出,python会自动将整数数据转为长整数类型,但是在python3.x版本,不存在long数据类型 ========python 2.7.5=========== [[email protected] ~]# pythonPython 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> type(2**50)<type ‘int‘>>>> type(2**60)<type ‘int‘>>>> type(2**62)<type ‘int‘>>>> type(2**63)<type ‘long‘>>>> ========python 3.5.2===========(venv) D:\Python18\day02>pythonPython 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> type(2**100)<class ‘int‘>
3.3, float (floating point)
1.浮点数用来处理实数,即带有小数的数字。2.浮点表示的形式是小数,但小数不一定都是浮点型,23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
3.4. Complex (plural)
复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。注:Python中存在小数字池:-5 ~ 257
3.5. Boolean value
真或假、1或0Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> a = 0>>> if a :print("a")...>>> a =1>>> if a :print("a")...a
3.6. Ways to view data types
查看数据类型,使用type(),如下:Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> print(type("www.123.com"))<class ‘str‘> #字符串>>> print(type(1.22))<class ‘float‘> #浮点数>>> print(type(1))<class ‘int‘> #整型
4. Data Operation 4.1 arithmetic operation
operator |
Descriptor |
Example |
+ |
Addition-adds value to both sides of the operator |
A + b = 30 |
- |
Subtract-subtracts the operand from the right side of the left operand |
A-B =-10 |
* |
Multiply-multiplies the values on both sides of the operator |
A * b = 200 |
/ |
Divide-by right operand divided by left operand |
b/a = 2 |
% |
Modulo-by the right operand and remainder return divided by the left operand |
B% A = 0 |
** |
Exponent-Performs a calculation of the operation Exponent (power) |
A**b = Power of 10 20 |
// |
Divide-The division of the operand, where the result is the quotient of the number of digits after the decimal point is removed. |
9//2 = 4 and 9.0//2.0 = 4.0 |
4.2 Comparison operations
operator |
Descriptor |
Example |
== |
Checks whether the values of the two operands are equal, and if so, the condition becomes true. |
(A = = B) is not true. |
!= |
Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. |
(A! = B) is true. |
<> |
Checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. |
(a <> B) is true. This is similar to! = operator |
> |
Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. |
(A > B) is not true. |
< |
Checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. |
(A < b) is true. |
>= |
Checks whether the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. |
(a >= B) is not true. |
<= |
Checks whether the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. |
(a <= B) is true. |
4.3 Assignment operations
operator |
Descriptor |
Example |
= |
Simple assignment operator, assignment from left operand of right operand |
c = A + B will specify the value A + B to c |
+= |
The addition and assignment operator, which increases the left operand of the right operand and assigns the result to the left operand |
c + = a equals c = C + A |
-= |
Minus and assignment operator, which subtracts the right operand from the left operand and assigns the result to the left operand |
c-= a equals c = c-a |
*= |
Multiplication and assignment operator, multiplied by the right operand and left operand, and assigns the result to the left operand |
c = a equals c = C A |
/= |
The Division and assignment operator, which puts the left operand with the correct operand and assigns the result to the left operand |
C/= a equals = C/A |
%= |
Modulus and assignment operator, which needs to use the modulus of two operands and assign the result to the left-hand operand |
C%= A is equivalent to C = c% A |
**= |
Exponential and assignment operators, performing exponential (power) calculation operators and assigning to left operands |
c = a equals c = C A |
//= |
The floor is divided and assigned a value that performs the floor in addition to the operation and assignment to the left operand |
C//= a equals c = c//A |
4.4-bit arithmetic
operator |
Descriptor |
Example |
& |
Binary and copy operations have been done, as a result, if it exists in two operands. |
(A & B) = 12 that is 0000 1100 |
| |
A binary or copy operation has a bit, if it exists in an operand. |
(A |
b) = 61 or 0011 1101 |
^ |
A copy of the binary XOR operator, if it is set to an operand instead of two bits. |
(a ^ b) = 49 that is 0011 0001 |
~ |
The binary complement operator is unary and has the effect of a "flip" bit. |
(~a) = 61 is 1100 0011 in the form of a 2 complement due to the signed binary number. |
<< |
Binary left shift operator. The left operand's value is shifted left by the number of digits specified by the right operand. |
A << 2 = 240 or 1111 0000 |
>> |
Binary right shift operator. The value of the left operand is moved to the right by the number of digits specified by the right operand. |
A >> 2 = 15 or 0000 1111 |
4.5 Logical operations
operator |
Descriptor |
Example |
and |
So-called logic and operators. If the two operands are true, then the condition is set. |
(A and B) is true. |
Or |
The so-called logical OR operator. If there are two operands that are non-0 then the condition becomes true. |
(A or B) is true. |
Not |
The so-called logical non-operator. The logical state used to invert the operand. If a condition is true, the logical non-operator returns false. |
Not (A and B) is false. |
4.6 Two or three-dollar operation
result = 值1 if条件 else 值2如果条件为真:result=值1如果条件为假:result=值2Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> a,b,c = 1,3,5>>> d = a if a>b else c>>> d5>>> d = a if a <b else c>>> d1
4.7 Binary Type
Binary, 01
Octal, 01234567
Decimal, 0123456789
Hex, 0123456789ABCDEF
5. Bytes Type
The most important new feature of Python 3 is probably a clearer distinction between text and binary data. Text is always Unicode, represented by the STR type, and binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between them particularly clear. You cannot stitch strings and byte packets, search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa). It's a good thing. In any case, the line between the string and the byte packet is inevitable, and the following diagram is important to keep in mind:
#!/usr/bin/python# _*_ coding:utf-8 _*_# Aothr: Kim#string和bytes类型转换,使用encode和decode的方式#decode==>bytes-->string#encode==>string-->bytes#举例msg = "我爱北京天安门"print(msg)print(msg.encode(encoding="utf-8") #转换成bytesprint(msg.encode(encoding="utf-8").decode(encoding="utf-8")) #转换成字符串执行结果如下:我爱北京天安门b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8‘我爱北京天安门应用场景:socket中传输必须是二进制,此时就需要将string进行转换成bytes类型
Getting Started with Python (ii)