Python Foundation One

Source: Internet
Author: User

A Python introduction

The founder of Python is Guido van Rossum (Guido van Rossum). 1989 Christmas period, Guido van Rossum (Chinese name: Uncle Turtle) in order to pass time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.

(Uncle Tortoise: 2005 joined Google to 2012, 2013 joined Dropbox until now, still grasp the core direction of Python development, known as benevolent dictator).

Current Python main application areas:

    • Cloud computing : The hottest language in cloud computing, typically using OpenStack
    • Web Development : A number of excellent web frameworks, many large sites are Python development, Youtube, Dropbox, watercress ... , a typical web framework has Django
    • Scientific Computing, artificial intelligence : Typical library numpy, SciPy, matplotlib, Enthought Librarys,pandas
    • system Operation and maintenance: the necessary language for operation and maintenance personnel
    • Finance : Quantitative trading, financial analysis, in the field of financial engineering, Python is not only used, but also used the most, and the importance of the year. Reason: As a dynamic language Python, the language structure is clear and simple, the library is rich, mature and stable, scientific calculation and statistical analysis are very good, production efficiency is much higher than c,c++,java, especially good at strategy backtesting
    • graphical GUI: PyQT, Wxpython,tkinter

2 Python is an interpreted weakly typed programming language.

2.1 Compile-and-interpret type.

The compiler compiles each statement of the source program into a machine language and saves it as a binary file so that the computer can run the program directly in machine language at a very fast speed.

And the interpreter is only in the execution of the program, only one interpretation of the machine language to the computer to execute, so the speed is not as fast as the compiled program to run.

This is because the computer does not directly recognize and execute the statements we write, it only knows the machine language (in binary form)

Compiled type
Pros: Compilers typically have pre-compiled procedures to optimize code. Because the compilation is done only once, the runtime does not need to compile, so the program execution of the compiled language is highly efficient. Can run independently from the language environment.
Cons: After compilation, the entire module needs to be recompiled if modifications are required. When compiling the machine code according to the corresponding running environment, porting between different operating systems will be problematic, and you need to compile different executables according to the operating system environment you are running.

Explanatory type
Pros: Good platform compatibility, can be run in any environment, provided the interpreter (virtual machine) is installed. Flexible, modify the code when the direct modification can be quickly deployed, without downtime maintenance.

Cons: Every time you run, you have to explain it again, performance is not as good as the compiled language.

2.2 Dynamic languages and static languages
Usually we call dynamic language, static language refers to dynamic type language and static type language.

(1) Dynamic type language: Dynamic type language refers to the language of data type checking during run time, that is, when programming in a dynamic type language, you never have to specify a data type for any variable, and the language will record the data type internally when you assign it to a variable for the first time. Python and Ruby are a typical dynamic type language, and many other scripting languages, such as VBScript, are also dynamic type languages.

(2) Static type language: Static type language is just the opposite of dynamic type language, its data type is checked during compilation, that is, when writing a program to declare all variables of the data type, C + + is a static type language typical representative, other static type language also has C #, Java and so on.

2.3 Strong type definition language and weak type definition language

(1) Strongly typed definition language: A language that enforces the definition of a data type. In other words, once a variable is assigned a data type, it is always the data type if it is not cast. For example: If you define an integer variable A, the program simply cannot treat a as a string type. A strongly typed definition language is a type-safe language.

(2) Weakly typed definition language: a language in which data types can be ignored. In contrast to strongly typed definition languages, a variable can assign values of different data types.

Strongly typed definition language may be slightly slower than weak type definition language, but the rigor of strong type definition language can effectively avoid many errors. In addition, "The language is not a dynamic language" and "the language is the type of security" is completely no connection between!
For example: Python is a dynamic language and is a strongly typed definition language (type-safe language); VBScript is a dynamic language and is a weak type definition language (type unsafe language); Java is a static language and is a strongly typed definition language (type-safe language).

With these introductions, we can conclude thatPython is a strongly typed definition language that is dynamically interpreted.

3 Types of Python

CPython

When we downloaded and installed Python 3.6 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.

Most python code can run under PyPy, but PyPy and CPython are somewhat different, which results in the same Python code being executed under both interpreters. If your code is to be executed under PyPy, you need to understand the differences between PyPy and CPython.

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.

Summary:

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.

4 single-line comments and multiline comments

When line comment: # is commented content, ' comment content ', ' comment content '

Multi-line Comment: ' Annotated content ', or ' "' annotated Content '" "

5 variables

What is a variable? Variables: The intermediate results of running the program are temporarily present in memory for subsequent code calls.

5.1. Declaring variables

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3   4 " zhh "

The code above declares a variable named: Name, and the value of the variable name is: "Zhh"

The role of a variable: a nickname that refers to what is stored in an address in memory

Naming conventions for variables

1 variables are combined by letters, numbers, and underscores
2 can not start with a number, not a full number
3 cannot be a python keyword, these symbols and letters have been occupied by Python and cannot be changed

Such as:

[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
4 Do not use Chinese
5 words to make sense
6 Not too long
7 Case-sensitive
We recommend that you use the camel or underline name
Camel body: Capitalize each word except the first letter
Underline: Each word is separated directly by an underscore

Recommended definition style camel or underline

# Hump Body     = = = # underline = 80
View Code

2. The data type of the variable:

①.int integers: +,-, *,/,% (calculates remainder),//(divisible)

②.str string: ', ' ', ' ', ' ', ' enclosed in the contents (note: Quotation marks use the same type in a string.)

The string can be +,*.

' + ' means that the concatenation between strings ' * ' represents a multiple of the contents of the string multiplied by.

③.bool boolean value. Only true or FALSE, true and false.

3. Assigning values to variables

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 " zhh " 5 " Zhangqiang "

1 # !/usr/bin/env python 2 # -*-coding:utf-8-*- 3 4 " zhh " 5 name2 = name1

Examples of ways to define bad variables

    • The variable is named Chinese, pinyin
    • Variable name too long
    • Variable nouns are not expressive

6 User Program Interaction

 1  #  !/ Usr/bin/env python  2  #  -*-coding:utf-8-*- 3  4
     #   5  name = input ("   Enter your username:   " )  6  7  #   Print input  8  print  (name) 

Converts a string into an integer: str = = int =>int (str) For example

# Let the user enter a, let the user enter B, the computer calculates the result of a+b a = input (" Please enter a:")    #input received the content is str B = input (" Please enter B:")    # converts a string into an integer   int (string) c = Int (a) + int (b)print(c)

string concatenation

Strings can be "added" and "multiplied" operations. Is the concatenation of strings and the repetition of strings

>>> name'zhh'>>> age' >>> >>> name + age  # add up is actually simply splicing 'zhh22'  # multiplying is actually copying how many times, and then stitching together 'zhhzhhzhhzhhzhhzhh'

7 Flow Control Statement if

1. Syntax One:

If condition: #引号是将条件和结果分开

Result 1. #四个空格, or a tab key that tells the program to meet this condition

Results 2

If the condition is true (true) execution Results 1, then result 2, if the condition False (false) direct result 2

2. Syntax Two:

If condition:

Results 1

Else

Results 2

Code 3

Print("crashed crashed crashed, who is it?") Gender= Input ("is it a man or a woman, please?")ifGender = ='male':    Print("please find the next door .")Else:     Print("come on in.")

= = Indicates the value of the assignment

3. Syntax Three:

If condition 1:

Results 1

Elif Condition 2:

Results 2

........

Else

Result N

For example

month = input ("Please enter a month")ifmonth = ='January':    Print("Eat Bananas")elifmonth = ='February':    Print("Eat apples")elifmonth = ='March':    Print("Eat Cake")elifmonth = ='April':    Print("Eat Grapes")..........Else:    Print("What do you want to eat?")

4. Statement four--nesting

If condition 1:

Results 1

If condition 2:

Results 2

Else

Results 3

Else

Results 4

Print("crashed crashed crashed, who?") Gender= Input ("excuse me, are you a man or a woman?")ifGender = ='male':    #Pass # Pass. Indicates the integrity of the syntax    Print("The man goes next door.")Else:#not a man.Age = Input ("Dalglish this year?")    ifInt (age) > 48:#input receives a string. 48 is an int. These two data types are not comparable        Print("aunt, who are you looking for? Maybe in the next room.")    Else:        Print("My family's melon, especially sweet.")

Python Foundation One

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.