day1 ------ python basis

python basis

Introduce a .python

Python founder of Guido van Rossum (Guido van Rossum). During Christmas 1989, Guido van Rossum in Amsterdam in order to pass the time, determined to develop a new script interpreter, ABC language as an inheritance.

TIOBE latest rankings, Python has occupied the fourth position in the world, Python advocating beautiful, clear and simple, it is an excellent and widely used language.

 

TOP 10 programming languages TIOBE index movements ( 2002-2018 )

 

Seen from the figure, the overall upward trend Python, Python reflect more widely and have gradually been recognized in the industry! ! !

Python is used in many fields, such as: data analysis, component integration, many networking services, image processing, numerical calculation and scientific computing. Almost all of the Internet industry's medium-sized enterprises are using Python, such as: Youtube, Dropbox, BT, Quora ( China know almost), watercress, know almost, Google, Yahoo, Facebook, NASA , Baidu, Tencent, car home,! US group and so on.

Currently Python primarily used areas:

Cloud computing: cloud computing the hottest language, typical applications OpenStack

WEB development: many excellent WEB framework, many large sites are Python development, Youtube,

Dropbox, watercress. . . Typical WEB framework Django

Scientific computing, artificial intelligence: Typical library NumPy, SciPy, Matplotlib, Enthought

librarys,pandas

System operation and maintenance: operation and maintenance personnel must-language

Financial: the most quantitative trading, financial analysis, financial engineering, Python not only with, and use, and the importance of improving year by year. The reason: As Python dynamic language, clear and simple language structure, rich library, mature and stable, scientific computing and statistical analysis is very fast hardware, production efficiency is much higher than c, c ++, java, especially good strategy backtesting

Graphics the GUI : PyQT, WxPython, TkInter

Python applications in some companies:

Google: Google App Engine, code.google.com, Google Earth, Google reptiles, Google ads and other projects are in extensive use of Python development

CIA: CIA Web site was developed with Python

NASA: US Space Agency (NASA) extensive use of Python for data analysis and computation

YouTube: the world's largest video site YouTube is to use Python development

Dropbox: America's largest online cloud storage sites, all implemented in Python, handle every day the site to upload and download 000 million files

Instagram: America's largest photo-sharing social networking sites, more than 30 million photos every day to be shared, all with python development

Facebook: a lot of basic libraries are implemented by Python

Redhat: the world's most popular Linux distribution version of yum package management tool that is developed with python

Watercress: Almost all of its services are developed by Python

Know almost: the largest Q & A community, by the Python development (foreign Quora)

Rain Doctor: well-known online medical website is developed with Python

In addition to the above, as well as Sohu, Jinshan, Tencent, Shanda, Netease, Baidu, Alibaba, Taobao, potatoes, Sina, shell and other companies are using Python to complete a variety of tasks.

1.1.python is a kind of language

Programming language mainly from the following angles are classified, compiled and interpreted, static and dynamic languages, strongly typed and weakly typed language definition language, we look at the compiled languages ​​and interpreted languages. Besides strongly typed later and weakly typed

The difference between compiler and interpreter is what it?

The compiler is the source of each statement sentences are compiled into machine language, and save it as a binary file, so when the computer is running

Directly in machine language to run this program, very fast;

The interpreter is only in the implementation of the program, only one by one into machine language to explain the computer to perform, so speed is not as good as the program compiled to run fast.

This is because the computer can not directly recognize and enforce our written statement, it can only recognize machine language (binary form)

 

Compiled vs interpreted

Compiled

Advantages: The compiler usually have pre-compiled code optimization process. Only once because the compiler does not need to compile and run time, so the high efficiency of program execution of compiled languages. It can operate independently from the locale.

Cons: After compiling If you need to modify the entire module needs to be recompiled. Compile time to generate machine code according to the corresponding execution environment, migration there will be problems between different operating systems, according to the environment you need to compile and run a different operating system executable files.

Interpreted

Pros: good platform compatibility, can run in any environment, provided that the interpreter (Virtual Machine) installed.

Flexible and modify the code directly modify when you can, you can quickly deploy, not down for maintenance.

Disadvantages: time for each run should explain it again, as compiled language performance.

1.2.python advantages and disadvantages

advantage:

1. Python's position is "elegant", "clear", "simple" , so Python always looks simple and easy to program

Understand, beginners learn Python, not only easy entry, and future deep down, you can write those very, very complex programs.

2. The development efficiency is very high , Python has a very powerful third-party libraries, you basically want to achieve through any computer functions, Python official library has a corresponding module support, directly after downloading calls, based on the basis of the library again development, greatly reducing development cycle, to avoid duplication of-create the wheel.

3. Advanced Language ---- When you write programs in Python, you need to consider, such as how to manage memory ⼀ your program uses low-level details of a class

4. Portability ---- because of its open-source nature, Python has been ported on many platforms (ie changed

To make it work on different platforms). If you are careful to avoid the use depend on the characteristics of the system, then all your Python programs without modification can run on almost all platforms on the market

5. Scalability ---- If you need a critical piece of code to run very fast or want some algorithms are not open, you can put part of your program Use written in C or C ++, then in your Python programs use them.

6. Embeddability ---- you can embed Python within your C / C ++ program, which provides scripting capability to your program's users.

Look Cons:

1. slow , Python's speed compared to the C language does a lot slower, but also slower compared with JAVA, so this is a lot of so-called Daniel bothered to use Python's the main reason, but in fact refers to the speed here slow in most cases the user is unable to perceive directly, must use test tools can be reflected, for example, you spend 0.01s shipped with a C program, Python is 0.1s, so the C language directly to 10 times faster than Python, considered very exaggerated, but you are not directly perceived by the naked eye, since around the time of a normal person can perceive the smallest units 0.15-0.4s, ha ha. In fact, in most cases, Python has been fully meet the requirements of your program unless you write speed for speed demanding search engines, in which case, of course, it is recommended that you use C to achieve.

2. The code can not be encrypted , because PYTHON is an interpreted language, its source code is stored in text form in the name, but I do not think it can be considered a drawback, if your project requires that the source code must be encrypted, then you start it should not come with Python implementation.

3. The thread can not take advantage of multi-CPU problem , which is a disadvantage Python is the most criticized people, GIL namely the global interpreter lock (Global Interpreter Lock), is a tool for computer programming language interpreter for thread synchronization so that any time only a thread execution, Python threads are native operating system threads. On Linux is pthread, on Windows Win thread, entirely by the operating system schedules threads of execution. There are a main thread of execution threads, and a plurality of user programs within a python interpreter process. Even on multi-core CPU platform, because of the GIL, the prohibition of parallel execution of multiple threads. About a compromise solution to this problem, and we have discussed in detail in later chapters threads and processes.

Of course, Python and some other minor shortcomings, this is not in eleven, I want to say that any one language is not perfect, there are good things and do good, it is recommended you do not take a language another advantage of weaknesses talk the language of comparing come and go, language is just a tool to achieve programmers thought of tool, like when we are before high school geometry, sometimes you need a compass, and sometimes need to use the same set square, take the appropriate tools to do what it does best thing is the right choice. Many people have asked me before Shell and Python in the end which is better? I replied that Shell is a scripting language, Python is not just a scripting language that can do more, and then another dead end of said absolutely no need to learn Python, Python can do Shell can do, as long as you cow enough B, then lifted with Shell

Tetris can write this game, and I can say that can only be expressed, not with theory SB, SB will draw you to the same degree with him, and then the full experience you down.

1.3.python interpreter

When we write Python code, we get to .py extension of a text file containing Python code. To run the code, you need Python interpreter to execute .py file. Since the whole Python language interpreter from specification to are open source, so in theory, as long as the level is high enough, anyone can write Python interpreter to execute Python code (of course, very difficult). In fact, the existence of multiple Python interpreter.

CPython

When we download and install Python 2.7 from Python official website, we have direct access to an official version of the interpreter: CPython. The interpreter is written in C language development, so called CPython. At the command-line operation is to start python CPython interpreter.

CPython is the most widely used Python interpreter. All the code tutorials are also executed in CPython.

IPython

IPython is based on an interactive CPython interpreter, that is to say, just IPython has been enhanced in the interactive mode, but the execution of the functions and CPython Python code is exactly the same. Like many domestic browser Although the appearance is different, but the core are actually called IE. >>> CPython used as prompt, and with IPython In [ID]: as a prompt.

PyPy

Python interpreter PyPy another, its goal is to speed execution. PyPy using JIT technology, Python dynamically compiled code (note not explained), it is possible to significantly improve the execution speed of the Python code. Most of Python code can be run under PyPy, but CPython PyPy and some are different, which leads to the same Python code execution may have different results in the two interpreter. If you want to put under PyPy code execution, you need to understand the different points of PyPy and CPython.

Jython

Jython Python interpreter is running on the Java platform, Python code can be directly compiled into Java bytecode execution.

IronPython

IronPython and Jython similar, but IronPython Python interpreter is running on the Microsoft .Net platform, Python code can be directly translated into .Net byte code.

And Shi .python development

In 1989, in order to pass the Christmas holidays, Guido (turtle t) began to write Python language compiler.

The name Python, Guido from the beloved TV series Monty Python's Flying Circus. he

We hope that this new language called Python, able to meet his ideal: to create a C shell and between, full-featured, easy to use, can show the language.

In 1991, the first born Python compiler. It is written in C language and be able to call the C language library. From birth, Python already has: classes, functions, exception handling, core data types, including tables and dictionaries, as well as expansion module-based system.

  • Granddaddy of Python web frameworks, Zope 1 was released in 1999
  • Python 1.0 - January 1994 increased by lambda, map, filter and reduce.
  • Python 2.0 - October 16, 2000, joined the memory recovery mechanism, now constitutes the Python language

The basic framework

  • Python 2.4 - November 30, 2004, the same year the most popular of the birth of Django framework WEB
  • 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

Three .python installation

All the way OK. I remember tick next path, (environment variables)

Download: Python's official website: https://www.python.org/

IV. The first python program

There are two ways to write python program:

1. Enter cmd console. Enter the python to enter edit mode. At this time we can directly write python program, both configured environment variable to run.

2. You can also write the code in python .py file. Python code to execute commands through python

Note: The Chinese have a coding problem python2 in Python2 using ASCII code encoding process is not Chinese we need to replace the utf-8 encoding...

# -*- encoding:utf-8 -*-

V. Variables

Variables: The intermediate result of the operation of the temporary memory, so they follow the program calls.

a = 'YJ concentrically' where a is a variable. a temporary recording value for use later in the program to continue execution, the execution order. The value assigned to the right-hand side of the left operand, variable refers to an amount will become .

Variable naming rules:

1, a variable with letters, numbers, underscores combination

2, can not start with a number, but can not be fully digital

3, can not be pythond keywords, these symbols and letters have been occupied python, can not be changed

4, do not use Chinese

5, to be meaningful name

6, not too long

7, the distinction between capital

We recommend the use of a hump or body underlined name

Hump body: Yongjie other except for the first letter of each word is capitalized

Underline: yong jie _ between each word separated by an underscore

VI. Constants

Absolute constants do not exist in python. Convention, all capital letters is constant

For example: PI = 3.141592653

BIRTH_OF_SYLAR = 1990

VII. Notes

Sometimes we write something not necessarily to the user to see. Or you do not want the interpreter to perform. Then we can use # to comment out code is annotated content will not be implemented. Programmers can easily come back I read your code

Single-line comments: # annotated content

Multi-line comments: '' 'is the content of the comment' '' '' 'This is a multi-line comments "" "

Eight .python basic data types

What is a data type?

We humans can easily distinguish the difference between numbers and characters, but the computer does not die, although very powerful computer, but in some ways and silly point of view, unless you explicitly tell it, is the number 1, "Han" is the text, otherwise it is the difference and 'Chinese' tell of 1, therefore, in each programming language will have a data type of stuff is called, in fact, a variety of commonly used data types are clearly divided, you want the computer numerical calculation, you can transfer it to digital, you want him to word processing, you pass a string type to him. There are several commonly used data types in Python, today we have no talk about three kinds of numbers, strings, Boolean type (proposition).

8.1 Integer (int)

Common numbers are int. For comparing the calculated size or

Int 32-bit machine in the range of: 31 ~ -2 ** 2 ** 31-1, i.e. -2147483648 2147483647

Int 64-bit machine in the range of: 63 ~ -2 ** 2 ** 63-1, i.e. -9223372036854775808 ~ 9223372036854775807 sufficient for you Note that these bars are integers.

8.2 string (str)

In Python, all enclosed in quotation marks, and the whole string

Strings can be enclosed in single quotes, double quotes, or a three-quotation marks, there is no difference, but some special formatting needs without quotes such as:

msg = "My name is Feng Yongjie, 19 years old"

This needs to fit single and double or single quotes.

content = """

content

"""

Would like to assign a string line, you need three quotes.

Numeric types are + - * / String too?

Only the string * +

String splicing

1 s1 = 'a '
2 
3 s2 = 'bc'
4 
5 print(s1 + s2)

Multiplied str * int

 . 1 name = ' YJ refueling ' 2 Print (name *. 8) 

8.3 Boolean value (bool), true or false, True and False

IX. User interaction

Using the input () function allows us to interact with computers up

Syntax: variable = input ( "prompt")

1 Content = the INPUT ( " What is your name? " )
 2  
3  Print ( " we received in the console: " + Content)

Here you can get directly to the user input

X. flow control if statement

Your life is not often encounter a variety of options, such as playing dice, guess the size, for example, choose to go that way home? Python program will encounter this situation, which is used in the if statement.

The first syntax:

if conditions are: # quotes is to separate conditions and results.

Results 1. # Four spaces, or a tab key, this is to tell the program to meet this condition

Results.

If the condition is true the results (True) 1, then the result of 2, if the conditions are false (False) as a direct result 2

The second syntax:

If conditions:

Results 1

else:

Results

Code 3

The third syntax:

If Condition 1:

Results 1

elif Condition 2:

Results

..

else:

N results

The fourth syntax (nesting):

If Condition 1:

Results 1

If Condition 2:

Results

else:

Results

else:

Results 4

Unlimited nesting, but in the actual development. Try not to exceed three nested.

Supplementary 1: in and not in

May determine whether the string that appears in the xxx xxxxx string

1 Content = the INPUT ( " Please enter your comment " )
 2  
3  IF  " YJ is the Best of programmers "  in Content or  ' YJ is the world's first '  in Content:
 4  
5      Print ( ' what you entered is not legitimate ' )
 6  
7  the else :
 8  
9      Print ( " comments successful " )

 

XI. -While process control loop

while conditions:

Loop

else: out of circulation after normal circumstances will perform here

 1 a = 1
 2 
 3 while a < 11:
 4 
 5     if a == 8:
 6 
 7         break
 8 
 9         pass
10 
11     else:
12 
13         print(a)
14 
15     a = a+1
16 
17     else:
18 
19         print("你好")            

 

Note: If the cycle is to break through the exit, then back while else would not be executed, while only conditional is false when it will execute the else

pass: does not mean anything to the integrity of the code of occupying it.

In life, we encounter things cycle, right? For example, the cycle songs. In the program, but also keep it, and this is an empty statement processes the while .

Simulate what cycle songs:

. 1  the while True:
 2  
. 3  Print ( " we are not the same ' )
 . 4  
. 5  Print ( ' we ⼀ comp ' )
 . 6  
. 7  Print ( " what's different ' )

If the condition is true, execute direct result of then determine the condition again until the condition is false. Stop the cycle that how we terminate the loop it ?

End of the cycle:

1, changing conditions

2,  break

XII. -Break process control and continue

1, break:. Immediately out of the cycle interrupt means

2, continue: stop this cycle, the next cycle continues.

November 1, 2019

Guess you like

Origin www.cnblogs.com/yjtxin/p/11779807.html