Python Basic Learning

Source: Internet
Author: User
Tags save file

1. What is Python?

Python is a programming language that allows you to work faster and integrate your system more efficiently.

Python is an object-oriented, interpreted computer programming language.

2. Python History and development

Python was invented by Guido van Rossum in 1989 and the first public release was released in 1991.

Python official now offers two versions per cent:

Version One: 2.x.x (currently the latest official version number: 2.7.11)

Version two: 3.x.x (currently the latest official version number: 3.5.1)

Although there are two versions, the code written in these two versions is incompatible. Here's an explanation:

python needs to address some of the dross from previous Python language designs in Python 3, which is a big upgrade compared to earlier versions of Python. In order not to take too much of a burden,Python 3.0 did not consider backwards compatibility when designing. Many programs designed for early Python versions do not work correctly on Python 3.0. To take care of the existing program, Python2.6, as a transitional version, basically uses the syntax and library of Python 2.x, taking into account the migration to Python 3.0, allowing the use of some of the syntax and functions of Python 3.0. programs that run on Python 2.6 without warning based on the earlier Python version can be migrated seamlessly to Python 3.0 through a 2TO3 conversion tool. Python 2.7 was identified as the last Python2.x version, which supports some Python 3.1 syntax in addition to Python 2.x syntax.

Therefore, the current version of the study is mainly to start to 3.x.x version of the Close, which is also a trend. 3. Python Application scope
    • Network and Internet development status
    • Database access
    • Desktop Graphical user interface
    • Science and Digital
    • Education
    • Network programming
    • Software and game development
4, the installation of Python program

Slightly...

5. The first Python program: Hello world!5.1 Python Interpreter

You need to put the following code in the first line of the file:

1 # !/usr/bin/env python

Or

# !/usr/bin/python

The code above means that the file is in the Python language as the file interpreter.

5.2 First Program

The python operation, which is interactive at the command line, is typically used to debug and test a small amount of code. The other is to edit the text, enter the code, save it as a file in the way of execution.

The code is as follows:

1 Print ("Hello world! ")

Operations on the command line

  

Save the file in the form (note that the suffix of the save file is ". Py"):

  

1 Vim hello_wold.py  Cat
3 Print ("Hello world! "

Hello World

However, as a development to use, need to use some integrated development environment software, to edit, develop code, easy to debug, such as: Pycharm

5.3 Comments in Python

Single-line comment: Annotate with "#"

Such as:

1 # This is the first program2 print ("Hello world!")

Or

1 print ("Hello world!") # This was the first program

Multiline Comment: Use ("" "..." "") or ("...")

1 " " 2 This is a comment 3 This is a comment 4 This is a comment 5 ""
Or
1 """ 2 This is a comment 3 This is a comment 4 This is a comment 5 """

6. Variables

First look at an example:

1 " topking " 2 Print (name) 3 topking

The "name" in this equation is a variable, and "Topking" is the value of the variable, which means defining a variable named "name" and assigning the value "Topking" to the variable "name".

The internal implementation in the computer is this way: first the computer will open a space in memory to store "topking", then "name" to the memory address of "topking". This is illustrated below:

  

Let's look at an example:

1>>> name ="topking"2>>> name_2 =name3>>>Print(name_2)4 topking5>>> name ="topspeedking"6>>>Print(name_2)7 topking8>>>Print(name)9Topspeedking

Why is the value of the last variable "name" and "name_2" different? Let's take a look:

  

In fact, the "name_2" point has not changed, but the name of the point to the new memory address

Naming rules for variables:

1, the first character can be letters, underscores, cannot be numbers, other parts can be letters, numbers and underscores.

2, to distinguish between case.

3. You cannot use an internal name.

4, the name of the variable to have meaning, can reflect the role of variables.

5, the writing rule best uses the hump format, namely username.

7. Data type

String: ABC,ABC, "123"

Digital:

Integer: 1,200,-200

Floating point: 1.2,3.102,5.0

Sequence:

List: ["1", "2", "3"]

Array:

Dictionary:

8. Process Control

The relationship between code blocks in Python is determined by the amount of code being indented, and the official recommendation is that the standard code indent is: four spaces in length.

8.1-Piece judgment if

code example:

1 number = 52if number > 0:3     print("number Biger than Zero. ")

The results are as follows:

1 number biger than Zero.

The code here means that if number is greater than 0, then the printed digit is greater than 0.

What if the conditions increase? Can do this:

Please look at the code:

1 number = 52if number > 0:3     print("  Number Biger than Zero. " )4Else:5     print("number Smller than Zero. ")

The results of the implementation are as follows:

Number Smller than Zero.

8.2 Cycle Structure

function range (), which can be used as a counter

8.2.1 for Loop

Structure Style:

1  for  in range:2     print(i)

Execution results

1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8
   
    10 9
   

  

Not to be continued ...

Python Basic Learning

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.