Python full stack Development a python first knowledge

Source: Internet
Author: User

1.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 is the same as what type of language

Python is a strongly-typed, interpreted, dynamic language.

The difference between a strong type and a weakly typed language:

Strongly typed definition language

A language that always enforces type definitions. Java and Python are mandatory type definitions. If you have an integer, if you do not display the conversion, you cannot treat it as a string. If you define an integer variable A, it is not possible for a program to treat a as a string type at all. A strongly typed definition language is a type-safe language.

Weak type definition language
The data type can be ignored by the language. In contrast to strongly typed definition languages, a variable can assign values of different data types. Vb,php are common.

The difference between a static type and a dynamic type:

Static type definition language

At compile time, the data type is a fixed language. Most static type definition languages enforce this, and it requires that you declare their data types before you use all the variables. Java and C are static type definition languages.

Dynamic type definition language

A language that discovers data types during execution, as opposed to static type definitions. VBScript and Python are defined as dynamic types because they are the type of the first time a variable is assigned a value.

The difference between an explanatory and a compiled language:

Interpreted language

The explanatory type translates the program into machine language at run time, so the speed is slower than the compiled language. such as Python,c#,java

Compiled languages

Compiled language before the program executes, there is a separate compilation process to translate the program into machine language, and later when the program is executed, it is no longer necessary to translate. For example, C + +

The advantages and disadvantages of the Python language:

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.01s, Python is 0.1s, so c directly than Python 10 times times faster, is very exaggerated, but you can not directly perceive through the naked eye, Because a normal person can perceive the smallest unit of time is 0.15-0.4s around, haha. 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.

2.Hello World Program

Because Python2 and Python3 in the user input on the difference, but considering the next Python3 rapid popularization, python some chicken operation was canceled, so the next code is Python3.

Hello World Program

Print ('Hello world! ')  # Direct output on the screen, Linux has its own Python # create file execution, Windows can be used under the shell under the execution

User input:

A = input (' Please enter content:'# The program will prompt the user for input when it executes

User-entered content is not assigned to variable a. Note that the user input will be defined as a string, even if the input is a number, it will be defined as a string, want to get the corresponding numeric type, you must use the corresponding method to convert.

b = Int (input (' Enter a number:'))   # The numeric value entered is a string, but it is converted to an integer by int. 

3. Rules for defining variables
    1. Can only be composed of letters, numbers, and underscores.
    2. Must be preceded by a letter or underscore.
    3. cannot be a keyword.
    4. It is best to have a certain meaning for each variable in English, which makes it easy to read the code.
    5. If you want to define a variable that is not modified, it can all be made up of uppercase letters.
1 ' Wallace '   # The name is Wallace .

4. Common numeric Types

Common numeric types are shaping (int), floating-point (float), Boolean (bool), plural (complex), which are easy to define because they are strongly typed, and therefore directly identify what type. For example:

1 a = 1    #int type 2 b = 1.2  #float type 3# BOOL Type 4 # Complex Type

5. Common data types (list, tuples)

Two common lists of data types and tuples are described below, with examples that describe the two data types.

List:

1 li = [up,'a','list']  #  The elements in the list are separated by commas, and the elements in the list can be for most data types 23 li1 = [1,3,[3,4],'Wallace' ]   # The list itself can also be used as an element

Indexes and slices:

1Li = [1,2,4,'a']#Define a list2>>LI[1]#index starts with li[0]324>>LI[-1]#index can be started at the end5 'a'6>>li[0:2]#The slicing principle is left closed right, that is, left without right7The8>>li[2:]#If you want to go to the last data, you don't write any data .94,'a'  Ten>>LI[-2:-1]#You can also have negative slices One4,'a' A                

List add element:

1>>li = [1,3,4]2>>li.append ('a')#add an element at the end3>>Li4[1,3,4,'a']5>>li.insert (2,'b')#add an element at a specified location6>>Li7[1, 3,'b', 4,'a']8>>li.extend ([2,'C'])#add multiple elements, which is the extended list9>>LiTen[1, 3,'b', 4,'a', 2,'C']

List search:

1 >>li = [1,3,'b', 4,'a', 2,'C  ']2 >>li.index ('b')                #  returns the index position of the corresponding element, or an error if it does not exist 3 2

List Delete:

1>>li = [1, 3,'b', 4,'a', 2,'C', 3]2>>li.remove (3)#Remove the first occurrence of the corresponding value3>>Li4[1,'b', 4,'a', 2,'C', 3]5>>li.pop ()#The value at the end of the pop-up list637>>Li8[1,'b', 4,'a', 2,'C']

Tuple (tuple):

1 tu = (1,2,3,[2,3])  # tuple is to change the bracket of the list to the parentheses 2'a', ' b '       # This approach defines a tuple as well. 3 tu = 1,             # Note You must have a comma to represent a tuple

There may be questions about why a list also defines a tuple, which is not superfluous, of course not: The list is an ordered collection that can be added and removed at any time. Tuples are immutable, i.e. they cannot be added, modified, or deleted once they are well defined.

Python full stack Development a python first knowledge

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.