Python_day1_ Foundation 1

Source: Internet
Author: User

First, Python introduction

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

Python advocates beauty, clarity and simplicity, and is an excellent and widely used language.

Python can be used in many fields, such as data analysis, component integration, network services, image processing, numerical computing, and scientific computing.

Python is a dynamically explanatory, strongly typed definition language. So what are the advantages and disadvantages of python that these genes have achieved?

Python the pros and cons

Advantages:

    1. Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
    2. Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
    3. High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
    4. Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
    5. Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
    6. Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.

Disadvantages:

    1. Slow, Python runs faster than the C language, and slower than Java, so this is the main reason why many so-called Daniel disdain to use Python, but in fact, this refers to the speed of slow in most cases the user is not directly aware of, Must rely on the use of testing tools to reflect, such as you use C a program to spend 0.1s, with Python is 0.01s, so c directly than Python faster 10s, is very exaggerated, but you can not directly perceive through the naked eye, Because the smallest unit of time a normal person can perceive is 0.15-0.4s or so. In fact, in most cases python has been fully able to meet your requirements for the speed of the program , unless you want to write to the speed of the most demanding search engine, in this case, of course, it is recommended that you use C to achieve.
    2. Code can not be encrypted, because Python is an explanatory language, its source code is stored in the form of a name, but I do not think this is a disadvantage, if your project requires that the source codes must be encrypted, then you should not use Python in the beginning to implement.
    3. Threads do not take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil, the Global Interpreter lock (interpreter lock), is a tool that the computer programming language interpreter uses to synchronize threads so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil. A compromise solution to this problem is discussed in more detail later in the Threads and Processes section.

Language is just a tool, is the tool to implement the idea of the programmer, take the appropriate tools to do what it is best at the right choice.

Python Interpreter

When we write the Python code, we get a text file that contains the Python code for the .py extension. To run the code, you need the Python interpreter to execute the .py file.

CPython

When we downloaded and installed Python 2.7 from the official Python website, we immediately got an official version of the interpreter: CPython. This interpreter was developed in C language, so called CPython. Running at the command line python is to start the CPython interpreter.

CPython is the most widely used Python interpreter. All the code for the tutorial is also executed under CPython.

IPython

Ipython is an interactive interpreter based on CPython, meaning that Ipython is only enhanced interactively, but the functionality and CPython of executing Python code are exactly the same. Like many domestic browsers although the appearance of different, but the kernel is actually called ie.

CPython >>> is used as a prompt, while Ipython is used In [ 序号 ]: as a prompt.

PyPy

PyPy is another Python interpreter whose goal is to perform speed. PyPy uses JIT technology to dynamically compile Python code (note that it is not interpreted), so it can significantly improve the execution speed of Python code.

Jython

Jython is a Python interpreter running on the Java platform that compiles python code directly to Java bytecode execution.

IronPython

IronPython is similar to Jython, except that IronPython is a Python interpreter running on the Microsoft. NET platform that compiles python code directly into. NET bytecode.

Python has a lot of interpreters, but the most widely used is cpython. If you want to interact with the Java or. NET platform, the best approach is not to use Jython or IronPython, but to interact via network tuning to ensure the independence of each program.

Ii. History of Python
  • In 1989, in order to pass the Christmas holiday, Guido began to write the Python language compiler. The name Python, from Guido's beloved TV show Monty Python's Flying Circus. He hoped that the new language, called Python, would fit his ideals: create a language that is all-powerful, easy to learn, easy to use, and extensible, between C and Shell.
  • 1991, the first Python compiler was born. It is implemented in C language and can call the C language library file. From birth, Python already has: classes, functions, exception handling, core data types including tables and dictionaries, and module-based expansion systems.
  • Granddaddy of Python Web frameworks, Zope 1 is released in 1999
  • Python 1.0-january 1994 adds lambda, map, filter and reduce.
  • Python 2.0-october 16, 2000, added a memory recovery mechanism that forms the basis of the Python language framework Now
  • Python 2.4-november 30, 2004, the same year now the most popular web framework Django was born
  • Python 2.5-september 19, 2006
  • Python 2.6-october 1 (compatible with 2.4 Previous versions )
  • Python 2.7-july 3, 2010
  • In November, it is announced that Python 2.7 would is supported until 2020, and reaffirmed that there would is no 2. 8 release as users were expected to move to Python 3.4+ as soon as possible
  • Python 3.0-december 3 ( no longer compatible with 2.x version )
  • 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
Third, Python 2 or 3?

in Summary:python 2.x are legacy, Python 3.x is the present and future of the language.

All are UNICODE now

No longer bothered by annoying character encodings

Iv. installation of Python Windows 1 、下载安装包      https: / / www.python.org / downloads / 2 、安装      默认安装路径:C:\python27 3 、配置环境变量      【右键计算机】 - - 》【属性】 - - 》【高级系统设置】 - - 》【高级】 - - 》【环境变量】 - - 》【在第二个内容框中找到 变量名为Path 的一行,双击】 - - > 【Python安装目录追加到变值值中,用 ; 分割】 如:原来的值;C:\python27,切记前面有分号 Linux, Mac 无需安装,原装Python环境 ps:如果自带 2.6 ,请更新至 2.7V. Hello World Program print ( "Hello World!" ) localhost:~ jieli$ vim hello.py localhost:~ jieli$ python hello.py Hello World! #!/usr/bin/env python print "hello,world"Six, variable \ character encoding #_*_coding:utf-8_*_ name = "Alex Li"Single-line comment: #多行注释: "" Seven, character encoding

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256-1, so the ASCII code can only represent a maximum of 255 symbols.

UNICODE (Universal Code): 4 bytes (32 bits)

Utf-16:2 byte (16-bit) 65535 symbol

UTF-8: 1-byte (8-bit) variable-length encoding set, compression and optimization of Unicode encoding, he no longer uses a minimum of 2 bytes, but all characters and symbols are categorized: the contents of the ASCII code are saved in 1 bytes, the characters in Europe are saved in 2 bytes, East Asian characters are saved with 3 bytes

About Chinese:

GB2312 (1980) contains a total of 7,445 characters , including 6,763 Kanji and 682 other symbols. The inner code range of the Chinese character area is high byte from B0-f7, low byte from A1-fe, occupy code bit is 72*94=6768. 5 of these seats are d7fa-d7fe.

GB2312 supports too few Chinese characters. The 1995 Chinese character extension specification GBK1.0 contains 21,886 symbols, which are divided into Chinese characters and graphic symbol areas. The Chinese character area consists of 21,003 characters. The 2000-year GB18030 is the official national standard for replacing GBK1.0. The standard contains 27,484 Chinese characters , as well as Tibetan, Mongolian, Uyghur and other major minority characters. Now the PC platform must support GB18030, the embedded products are not required. So mobile phones, MP3 generally only support GB2312.

Eight, user input #!/usr/bin/env python #_*_coding:utf-8_*_ #name = raw_input("What is your name?") #only on python 2.x name = input ( "What is your name?" ) print ( "Hello " + name )When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely: #!/usr/bin/env python # -*- coding: utf-8 -*-   import getpass   # 将用户输入的内容赋值给 name 变量 pwd = getpass.getpass( "请输入密码:" )   # 打印输入的内容 print (pwd)Nine, the first knowledge of the module

The power of Python is that he has a very rich and powerful library of standards and third-party libraries, and almost any functionality you want to implement is supported by a Python library, and later in the course you'll delve into the various libraries that are commonly used.

At this point you will put this tab.py in the Python Global environment variable directory, basically put in a directory called python/2.7/site-packages .

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 .

Xi. initial knowledge of data types

1. Digital

int (integer), float (floating-point), complex (plural)

2. Boolean value

3. String

String formatted output

PS: string is%s; integer%d; floating-point number%f

String Common functions:
    • Remove whitespace
    • Segmentation
    • Length
    • Index
    • Slice

4. List

5, tuples (immutable list)

6. Dictionary (unordered)

Pending additions

12. Data Operation

Arithmetic operations, comparison operations, assignment operations, logical operations, member operations, identity operations, bit operations

Operator Precedence

13. Expression If ... else # 提示输入用户名和密码   # 验证用户名和密码 #     如果错误,则输出用户名或密码错误 #     如果成功,则输出 欢迎,XXX! #!/usr/bin/env python # -*- coding: encoding -*-   import getpass     name = raw_input ( ‘请输入用户名:‘ ) pwd = getpass.getpass( ‘请输入密码:‘ )   if name = = "alex" and pwd = = "cmd" :      print ( "欢迎,alex!" ) else :      print ( "用户名和密码错误" )14. Expression for loop The simplest loop 10 times #_*_coding:utf-8_*_ __author__ = ‘Alex Li‘ for i in range ( 10 ):      print ( "loop:" , i )

Python_day1_ Foundation 1

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.