01 know python

Introduction python

python founding Face to Guido van Rossum (Guido van Rossum). During Christmas 1989, Guido van Rossum in Amsterdam in order to pass the time, depending Center Weighted
development causes a new script interpreter as a kind of inheritance ⼀ your language and the ABC.
The latest row ⾏ TIOBE list, Python has occupied the fourth position in the world, Python advocating beautiful, clear, simple, and Wide-⼀ an excellent pan the available languages.

0b130c78-7123-4642-8012-36ee20437edc.png
From the above chart can ⻅, Python overall upward trend, reflecting Python to apply it more and Wide-pan and has gradually been recognized in the industry! ! ! Python is to apply it in fields, such as: data analysis, component integration, Open networks and services, image processing fields, numerical and scientific computing. Destination time before the industry to get accustomed almost all medium and larger enterprises are interconnected ⽹ Using Python, such as: Youtube, Dropbox, BT, Quora (China know almost), ⾖ flap, know almost, Google, Yahoo !, Facebook, NASA, Baidu, Tencent , steam ⻋ house, the US group and so on.

Using the destination time before Python primarily areas:

Cloud Computing: Cloud computing most in fire of Language, typical to apply it OpenStack
WEB development: many excellent WEB framework, many are zoomed type website for Python development, Youtube, Dropbox, ⾖ flap. . . Typical WEB framework Django
scientific computing, intelligent Face ⼯: Typical library NumPy, SciPy, Matplotlib, Enthought librarys , pandas
system operation and maintenance: operation and maintenance personnel on a necessary your language and
⾦ melt: quantitative trading, financial analysis ⾦, melt in ⾦ ⼯ process areas, Python not only in Use, and most of Use on it and the importance of the year in response to high. The reason: as a dynamic of your language and
Python, your language and structure clear and straightforward, rich library, mature and stable, scientific computing and statistical analysis are ⽜ force, ⽣ far ADVANCED productivity in c, c ++, java, in particular good at ⻓ back policy
measure
graphics GUI: PyQT, WxPython, TkInter

Python in ⼀ some companies to apply it:

⾕ song: Google App Engine, code.google.com, Google earth, ⾕ song climb ⾍, Google Wide-destination time tell other items in the amount zoomed Using Python developers
CIA: CIA website for Using Python is developed by
NASA: NASA Administration (NASA) zoomed volume Using Python into ⾏ data analysis and computation
YouTube: the world is largest video website for YouTube is Using Python development
Dropbox: America is largest online cloud storage website for all Using Python implementation, ⽹ day processing station to upload and download files of one billion
Instagram: US is largest in Fig social website for still pictures to share, more than 30 million a picture Business cards are shared every day, all Using python development
Facebook: zoomed amount of base libraries are implemented by Python the
Redhat: most stream world yum package management Linux distributions ⾏ version of the camera settings that are able to work surely with that Using python development
⾖ flap: the company to get accustomed almost all business are by Python development
know almost: China is largest Q & a community, by Python development (foreign Quora)
spring to rain, your physician: a well-known website for online health care is developed Using Python
surface, on addition, there are Sohu, ⾦ ⼭, Tencent, Sheng zoomed, ⽹ easily, Baidu, a ⾥, Taobao, ⾖, Sina, shell and other companies are Using Python to complete all kinds of various
tasks like.

python is a kind of language

Programming your language and are divided into what is the difference compiled and interpreted programming your language and compiled and interpreted is?
Compiler is the source of every ⼀ statements are compiled into machine Language, and save it as ⼆ binary files , the computer may directly to your language and machine when running a program when running a while so, fast;
when ⽽ interpreter is YES the program only, it is ⼀ ⼀ Articles the language interpreted into machine language to a computer YES so when running a program after the speed is not as good as when running a compilation of the fast. this is because the computer can not directly recognize and YES we write the statement, it can only recognize your language and machine (a ⼆ hexadecimal format)
c09377c7-0f1d-4a48-bd46-8e73cb22f271.png

Compiled vs interpreted

Compiled
advantage: the compiler shoots as usual there will be pre-compiled code into ⾏ process optimization. Because the compiler only ⼀ times, you do not need when running a compile-time, so your language and compiled programs Perform efficiency ADVANCED. It may be out of your language and environment when running a single attention immediately.
Cons: After compiling If you need to modify the entire module needs to be recompiled. The compile time environment when running a corresponding ⽣ into machine code, there will be migration problems between different operating systems, the need to compile the environment when running a different operating system files may be YES.
Interpreted
advantages: a good platform compatibility, in any environment can be when running a, provided that the interpreter (Virtual Machine) installed. Flexible and modify the code directly modify when you can, you can quickly deploy, will not be used down for maintenance. Cons: every time when running a must explain ⼀ times, as compiled your language and performance.

python advantages and disadvantages

Look at the advantages

Python position is "elegant" and "clear", "simple", so Python program always looks easy to understand for beginners to learn Python, not only ⻔ easy way in. On it and will
be going deep START, you can write that comes in handy It comes in handy complex procedures.
ADVANCED development efficiency comes in handy, Python third ⽅ library comes in handy strong zoomed in, you basically want to achieve through any computer functions, Python official ⽅ library ⾥ has a corresponding module into ⾏
⽀ hold, directly after downloading Use tune in base charge before the development of libraries based on the zoomed zoomed reduce the development cycle, to avoid duplication wheel made submenus.
ADVANCED ---- When you Use your language and your language and Python when writing programs, No need for you to consider, such as how to manage your program so that low-level details like memory ⼀ Use of
portability ---- because of its open-source nature , Python has been ported on many platforms (changed to make it ⼯ made on different platforms). If you Caution
to refrain from using a system-dependent features, then all your Python programs No need for modification can get accustomed Operation with almost all the platforms on the market
scalability ---- If you need your ⼀ critical code segments when running a very fast or want some algorithms are not open, you can put part of your program Using C or C ++,
and then use them in your Python programs manipulation.

  1. Can be embedded START of ---- you can embed Python clicks into your C / C ++ program that provides scripting capability to your program from ⽽ Use households.

    Look Cons:

    Slow, Python's speed when running a C phase ⽐ your language and indeed much slower, with JAVA phase ⽐ needs to slow ⼀ more, so this is a lot of so-called zoomed ⽜ bothered to Use Python's
    the main reason, but in fact referred to individual cases from Use of slow when running a zoomed in most cases the user is directly perceived ⽆ law, requires the help of test ⼯ has to figure out, if you ⽐ Using C
    transport ⼀ programs took 0.01s, Using Python is 0.1s, so C your language and Python directly ⽐ 10 times faster, be an exaggeration comes in handy, but you are ⽆ law directly through ⾁ eye perception,
    because ⼀ Face can perceive normal time most ⼩ unit is about 0.15-0.4s, ha ha . In fact, in most situations it is full Python has been completely underexposed you can program the speed
    requirements, in addition comes in handy for you to write to extremely high speed requirements of search engines, in which case, of course, it is recommended that you go Using the C implementation.
    Code can not be encrypted because PYTHON interpretative Language, its source code is stored in the form of the name of the file, but I do not think this be ⼀ drawback, if you the item you
    requires that the source code must be encrypted, then you ⼀ Using Python start should not come to realize.
    Thread can not benefit Using multi-CPU problem, this is Python is Face the most criticized ⼀ disadvantage, GIL namely the global interpreter lock (Global Interpreter Lock), is a computer
    programming your language and an interpreter Use to ⼯ with thread synchronization so that any ⼀ threads in just a moment perform, Python threads are original ⽣ operating system threads. On Linux
    is pthread, on Windows Win thread, entirely by the operating system thread scheduling YES. There ⼀ strip main thread, and a plurality of Use within ⼀ a python interpreter process
    Perform thread user program. Even on multi-core CPU platform, because of the GIL, it is prohibited multithreading and ⾏ YES. On the subject of compromise Remedies, we
    ⾥ charge before discussed in detail later in the chapter threads and processes.

Of course, Python as well as other ⼩ ⼀ some shortcomings, this is not ⼀ ⼀ lists, I want to say is, whichever is ⻔ your language and are not perfect, there are good at and not good ⻓ ⻓ things to do, it is recommended you do not take a ⼀ your language and disadvantages to talk for another time advantage of your language and come and go ⽐ than, your language and just ⼀ with a ⼯, is thought to work surely achieve programmers tools, just like before when we get accustomed secondary school , sometimes need to compasses, sometimes need ⻆ Using three feet ⼀ like, take appropriate to work surely has to do something it had been masters ⻓ is the right choice. Before I ask a lot Face Shell and Python in the end which is better? I replied that Shell is a script Language, but not just a Python script Language, can do more, then there is a separate warranty tip drill ⽜ ⻆ Face said absolutely no need to learn Python, Python can do Shell can do as long as you are an ample supply ⽜ B, then gave a separate warranty Using the Shell can write block Russia ⽅ this game, and I can say that can only be expressed, not with theory SB, SB will draw you to him like ADVANCED ⼀ degree and then use the full experience put you down.

python interpreter

When we write Python code, we get a ⼀ files containing Python code files with this extension of .py. To when running a code, you need Python interpreter to Perform .py files. Since the entire Python from specification to your language and interpreter are open source, so in theory, as long as enough ADVANCED Horizontal, Face can write any Python interpreter to Perform Python code
(of course, very difficult zoomed). In fact, the existence of multiple Python interpreter.

CPython
when we download and install Python 2.7 from Python official website for ⽅, we have direct access to the ⼀ a ⽅ official version of the interpreter: CPython. Using the interpreter C is your language and the development of so called CPython. At the command ⾏ when running a python is to start CPython interpreter. Using the most CPython is the full wide Python interpreter. All tutorials are also in the code under CPython YES.
IPython
IPython is based on a CPython's ⼀ interactive interpreter, that is to say, IPython only been enhanced in the interactive mode, which is however Perform Python code is fully functional and CPython ⼀ like. Good ⽐ many domestic browser, although different appearance, but the kernel are actually tune Using the IE.
PyPy
PyPy is for another time Python interpreter, its destination time is marked Perform speed. PyPy recorded using JIT techniques for Performing Dynamic compiler Python code (note not explained), it is possible to significantly improve speed YES ADVANCED Python code. Absolutely part of Python code can be zoomed Operation with under PyPy, but CPython have ⼀ PyPy and some are different, which leads to Perform the same Python code may have different results in the two interpreter. If you are under the code to put PyPy YES, you need to understand the different points of PyPy and CPython.
Jython
jython is Python when running a Java interpreter on the platform, can be directly Python code is compiled into Java bytecode YES. IronPython IronPython and Jython similar, but is IronPython Operation with Microsoft's .Net platform Python interpreter can directly .Net Python code into bytecode.

History python development

In 1989, in order to pass the Christmas holidays, Guido (⻳ t) began to write Python your language and compiler. Python name to automatically Guido beloved TV series Monty
Python's Flying Circus. He hoped that the new language you called Python, able to meet his ideal: to create ⼀ kind between C and shell, ⾯ full-featured, easy to learn and use, be
scalable language you want.
In 1991, the first frame a Python compiler birth ⽣. Use your language and it is the C implementation, and can be adjusted Using Library files of your language and C. From the ⽣ ⼀, Python already has: class,
function, exception handling, Center Weighted core data types, including tables and dictionaries, and a module-based expansion system.
Granddaddy of Python Web frameworks, Zope 1 WAS Released in 1999
Python 1.0 - January 1994 increased the lambda, the Map, filter and the reduce.
Python 2.0 - October 16, 2000, filling with the memory recovery mechanism, constitute now Python your language and framework basic
Python 2.4 - November 30, 2004, the same year the destination time before most streams perform while the birth ⽣ WEB framework Django
Python 2.5 - September 19, 2006
Python 2.6 - October 1, 2008
Python 2.7 - July 3, 2010
In November 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be no
2.8 release as users were expected to move to Python 3.4+ as soon as possible
Python 3.0 - December 3, 2008
Python 3.1 - June 27, 2009
Python 3.2 - February 20, 2011
Python 3.3 - September 29, 2012
Python 3.4 - March 16, 2014
Python 3.5 - September 13, 2015

python installation

79361918.png

First Time a python program

There are two procedures written in python Modes:

  1. Gets into cmd console input START python gets into edit mode. At this time we can directly write python program
  2. You can also write in .py files in python code to Perform python code through the python command.
    Note: there will be problems in encoded files python2 in Python2 Using the ASCII code encoding process can not replace the file we need. utf-8 encoded.
1 # -*- encoding:utf-8 -*-

variable

Variable: the intermediate result of the operation to the temporary memory, for subsequent transfer program naming rules Use variables:
variables ⺟ by a word, number, underscore combinations ⽽ to
begin Using digital not, but can not fully digital
not be the pythond keywords, symbols and words ⺟ python has been accounted for using, it can not change
Do not Use the File
name to be meaningful
not too ⻓
distinguish zoomed ⼩ write
recommendation zoomed home Using the hump body or underscore naming
hump body: in addition to word ⺟ therefore especially outside each word therefore especially other words ⺟ zoomed write
underline: Use an underscore between each word separately

a = 10
2 _ = 30
3 __不要⽤中⽂哦 = "好的"
4 1_2 = 33 # 这个是错误的

constant

Absolute constants do not exist in python. Convention, all written word is constant zoomed ⺟

PI = 3.141592653
BIRTH_OF_SYLAR = 1990

Note

Sometimes we write East ⻄ not ⼀ are set to Use households to read. Or you do not want an interpreter YES. Then we can use the # to comment out the code is annotated content will not perform to can ⽅ and after you have read the screen for programmers to code a single ⾏ Notes: # annotated content is
more than ⾏ Note: '' 'content is annotated' '' '' 'this is also a multi ⾏ comment "" "

The basic data types python

What is a data type?

Face like we can easily distinguish the difference between numbers and characters, but the computer does not die, although very strong zoomed computer, but from the point of view of some ⻆ separate warranty silly, unless you explicitly ask it comes in handy, is the number 1, "Han" is the word the file, otherwise it is hard to tell the difference between 1 and 'Chinese' is, therefore, ⼀ a stuff called data type in your language and ⾥ each program will, in fact, a variety of frequently used ⾏ data types into a clear division, you want the computer to enter ⾏ numerical computation, you pass it to digital, you want him to word processing files, you pass a string type to him. Using a variety of data types Python often today, we do talk about three kinds of numbers, strings, Boolean type (proposition).
Integer (int)
in python3 all integers are of type int. Use to calculate or zoomed ⼩ What do the ⽐ than integer operations:

  1. Math.
    +, -, *, /,%, //
    first four do not say calculates the remainder.
  2. ⽐ compared zoomed ⼩.
a = 10
b = 30
print(a < b) # 真
print(a > b) # 假
print(a == b) # 假

String (str)
in Python, all Using quotation marks, and the whole string. Strings can cause Use single quotes, double quotes, or three quotes, there is no difference, just ⼀ need some special format will not be used ⽐ quotes such as:

msg = """
今天我想写⾸⼩诗,
歌颂我的同桌,
你看他那乌⿊的短发,
好像⼀只炸毛鸡
"""

Numeric types are + - * / character

#字符串的拼接
s1 = 'a '
s2 = 'bc'
print(s1 + s2)
#相乘 str*int
name = '坚强'
print(name*8)

Boolean value (bool), true or false, True and False, that is the main condition for Use judge

a = 20
b = 30
print(a < b) # 真
print(a > b) # 假
print(a == b) # 假

Using user interaction

Using the input () function allows us to interact together and computer syntax: content = input (prompt) can get directly to individual cases from the user input START Use of content

a = input("请输⼊⼀个数字a:")
b = input("请输⼊⼀个数字b:")
print(a + b)

At this point you will find that the result is not as you think the reason is to get input data are str type. In this case the sum is two str string concatenation. So, we now need to convert the string data types an int int (STR) thrown into the string. a number is obtained. then the program would end up with the screen for

Flow control if statement

You live in ⽣ is not often encounter a variety of options, such as playing discolored ⽐ submenus, select the required status guess zoomed lower, and the election onto clothing and so on. Python program will encounter this situation, which if to Use statements.
First Time kinds of syntax:

if 条件: # 引号是将条件与结果分开。
结果 # 四个空格,或者⼀个tab键,这个是告诉程序满⾜这个条件的
如果条件是<真(True)>执⾏<结果>
money = int(input("兜⾥的钱:"))
if money > 100:
    print("⼤腰⼦. 没⽑")

Note: At this point, whether successful or not conditions <> ⼀ set will be printed.

Second shot kinds of syntax:

if 条件:
    结果1
else:
    结果2

The third syntax:

if 条件1:
    结果1
elif 条件2:
    结果2
elif 条件3:
    结果3
    …
else:
    结果n

If <1> to <> YES <1> Otherwise, if <2> to <> YES <2> ... Otherwise, YES
fourth grammar (nested):

if 条件1:
    if 条件2:
        结果1
    else:
        结果2
else:
    结果3

-While loop flow control

In ⽣ live, we come across things cycle, right? The cycle ⽐ songs. In the program, but also keep it, and this is an empty statement processes while.
Cyclic analog ⼀ songs:

while True:
print('我们不⼀样')
print('我们都⼀样')
print('有啥不⼀样')

grammar:

while 条件:
    循环体

Note: If <> is <>, then YES <> then judged again <> until the condition is <> - stop the cycle>.

Case: Output 1, 2, 3, 4 ... .100

i = 1
while i <= 100:
print(i)
i = i + 1

Case: two kinds ⽅ case Output 1, 3, 5, 7, ..., 99

i = 1
while i <= 100:
    print(i)
    i = i + 2

i = 1
while i <= 100:
    if i%2 == 1:
        print(i)
    i += 1

Then how do we end for still circulating it?
End of the cycle:
1, changing the conditions.
2, BREAK

Process control and continue -break

1, break: immediately while interrupting means out of the cycle.
2, Continue: Stop this cycle continues Perform next time the cycle.

while True:
    s = input("输⼊你要喷的内容(Q退出):")
    if s == "Q":
        print("⽼娘今天⼼情好. 不跟你⼀般⻅识")
        break # 直接结束循环
    print("打野. 我想对你说:" + s)
while True:
    s = input("输⼊你要喷的内容(Q退出):")
    if s == "Q":
        print("⽼娘今天⼼情好. 不跟你⼀般⻅识")
        continue # 停⽌当前本次循环. 继续执⾏下⼀次循环
    print("打野. 我想对你说:" + s)

break shoots as usual well understood. The hard part was continue. one Hash write to you pseudo-code. Your Own discretion to record the next. What are the benefits continue to bring us is

while True:
    data = 从数据库读取的数据. 每次循环读取⼀条. ⼀共有100W条
    if data == 垃圾数据: 通过条件判断. 把脏数据获取到了.
    # 对于垃圾数据⽽⾔. 我们肯定是不希望处理的. 所以.此时怼上⼀个continue在合适不过
        continue
        …
疯狂的处理数据的代码(500多⾏)

to sum up

python是⼀⻔解释型⾼级编程语⾔. 它的代码是运⾏在python解释器上的. 我们⽬前使⽤的是CPython.
优点: 代码简单.容易上⼿
缺点: 代码执⾏效率没有C⾼
2.
注释: # 单⾏注释, """多⾏注释
3.
变量: 在内存中存储⼀个数据, ⽅便后⾯使⽤
⽬的: 在内存中, 众多数据中, 找到你需要的那个

变量的数据类型:
int, 整数, 加减乘除, 余数, 整除
str, 字符串, ', ", ''', """都是字符串, 操作: +, *
bool, 布尔值. True, False
4.
常量: 所有字⺟⼤写(约定俗称)
5.
⽤户交互: input()
注意: input获取到数据都是字符串类型, 如果想要进⾏数学运算或者⽐较. 必须转化成int()
6.
条件分⽀
if 条件:
结果
elif 条件2:
结果2
...
else:
结果n

 当条件成⽴. 执⾏相应的结果.
7.
循环分⽀
while 条件:
循环体
只要条件成⽴. 就⼀直执⾏循环体. 每次执⾏完循环体. ᯿新判断条件
8.
break: ⼲死⼀个循环.
continue: 继续执⾏下⼀次循环

Guess you like

Origin www.cnblogs.com/songda/p/12106955.html