Python Program Language Quick Start Tutorial _python

Source: Internet
Author: User
Tags php basics php example php foreach switch case web services ssh vps amazon cloud services
I was going to find an introductory tutorial on the Internet, but since Python is rarely the language that programmers learn in their first contact program, it's not much of an online tutorial, or a decision to write it yourself.

If you don't have a program base, you might find this article a bit more covered. In contrast to the teaching speed of C, which is often taught in universities, this article has about four or five hours of content; A video tutorial on a program class on a Web site is roughly equivalent to a two or three-hour content; For a book that turns a program, it's about one hours. Therefore, if there is an in-depth study of the plan, in order to efficiency or recommend reading.

It doesn't matter if you don't understand some of the content in this article for the time being, because they're all the basics that you'll often use, and you'll always encounter them when you actually write your code. There will be about two or three articles on practical code written later, and you can choose to deepen your impression of that knowledge at that time.

But if you feel that the technology is not necessary for SEO and not interested in this article, how do you intend to SEO as a career? If you're going to really do it well, it should be reasonable to take a few Baisi hours from inside to learn some technology in at least tens of thousands of hours of energy.

If you are not able to understand any of the articles that are not clearly stated, be sure to help out. The first time to write a more complete tutorial class, there are inevitable omissions.

Why learn Python

If you decide to learn some technology to help SEO, at least for the programming language in my opinion Python is the first choice.

First , Python Getting started relatively simple

If someone in the university curriculum in contact with C language but do not know much about the program, may feel that the program is like C so troublesome, but actually, learned the beauty of the Python code will know the C code is smelly and long. Even if you don't have access to a program at all, Python itself is a good choice for getting started with programs.

Although I have written a program very early, but at that time is the interest, never in-depth, formal contact procedure is to contact SEO after things, about learning a few hours later can write some simple collection procedures, so far as long as can think of the need for technology can not be achieved. So there is no basis for the problem, no matter what the basis is the need to lay their own.

Secondly, Python code is very flexible

To see a very common code example, if you do not have the basis of the program will not understand the code, but it does not matter, look at the number of lines can also be compared to the simplicity of code.

Php:
Copy Code code as follows:

<?php
$list _1 = array (1,2);
$list _2 = Array ();
foreach ($list _1 as $current) {
Array_push ($list _2, $current + 1);
}
Print_r ($list _2);
?>

Python:
Copy Code code as follows:

List_1 = [1,2]
List_2 = [current+1 for current in list_1]
Print List_2

The 2nd line of Python code, called "List derivation," provides a number of handy language features that make the code very concise.

While flexible grammar is not always a good thing, flexible code writing means that each person's written code is often different in style, which will make the team member's level gap is not conducive to team development. But for small SEO application requirements are generally personal development, at this time the writing efficiency of the code is more important than the team cooperation.

its three, python use of a wide range

Python is popular in programming languages around 5-10 bits. There are C #, Java, PHP, and so on, but many times not because they are more powerful, but because of the reasons mentioned earlier, their code is relatively inflexible and suitable for team development, so it is more popular.

In general, most Web services that provide APIs provide Python interfaces (such as the Baidu Network union, Amazon Cloud services, etc.), while others that are more powerful than Python have less support, such as the Go language, which is more flexible in code. (Most Web services have API interfaces: C #, PHP, Java, Python, Ruby, Perl, so choose at least one language learning in general)

Python also has a lot of modules, and many of the existing modules mean you can do a lot less. For example, downloading a Web page requires 5-10 lines of code in many languages, and Python takes only two lines:

Copy Code code as follows:

Import Urllib2
Print Urllib2.urlopen (' http://g.cn/'). Read ()

It's also interesting that Google previously limited its in-house use of Python language for development. The reason for this is 2:1, because Python writing is efficient, it was used too much before, and the Python language is slow to execute. But for SEO, it is the most need to focus on the efficiency of writing and generally do not care about implementation efficiency.

So much nonsense, but the interest is often the best motivation to learn, hope no white nonsense.

installation Environment and configuration

Python programs and ordinary programs are slightly different, usually do not have directly under Windows to double-click the operation of the EXE file, although it is not impossible to compile into an EXE but relatively troublesome, the actual use of this operation is rarely.

A python program is a piece of text code that is saved to a file with a suffix named. py. You can run the Py file after your computer has the Python running environment installed. (in fact, many programs are dependent on the operating environment, the most common is the. NET Framework, but this is pre-installed on Windows, generally do not feel it)

For Windows systems that require a running environment, the Linux system typically comes with Python without configuration.

The download link for the Python Runtime environment is: http://python.org/getit/

On this page, usually in the first download link, look for and download: Python 2.7.x windows Installer (Windows binary-does not include source)

It should be noted that the best download is the 2.7.x version (X represents any number, 2.7 is similar to the large version), and Python 3 above the version and the previous version of the larger difference, for beginners do not recommend the use.

Install after downloading, there is no need to pay special attention to the place. Generally installed in the default path, that is, c:\python27\, do not put it in the directory level is very deep folder, otherwise the running time is more troublesome.

Operating System

As mentioned earlier, Python can operate on Windows and Linux systems, and it also supports more systems such as Macs, and can even write and run Python programs on devices such as the jailbreak iphone.

But because one of the main strengths of Python is the support of its modules, it's easy to do a lot of things with modules. And the configuration of the module in Windows and other systems often some trouble, Linux is often much easier, so after the introductory phase, recommended in the Linux environment for development.

The Linux environment is built with three choices:

1. This machine installs the Linux system. Suitable for long-term work under Linux, people need to have a certain understanding of Linux basic knowledge. The main disadvantage is that there are always some windows inside to run the program, occasionally need to switch systems. I work mainly in such an environment.

2. Install virtual machines such as VMware in Windows and install Linux in virtual machines. Suitable for beginners, but because of the relationship between virtual machines, unless the computer configuration is very good, otherwise the operating efficiency of both systems will be relatively low. And the virtual machine is not conducive to shaping the atmosphere of learning Linux, because when the virtual machine encounter problems, most people will prefer to use Windows solution rather than using Linux, it is more difficult to become familiar with it. This choice is rarely recommended by individuals.

3. Buy a Linux VPS, remote operation using SSH. The main advantage is that both at work and at home can be very convenient to connect to the server, do not have to daily procedures, data, etc. with the U disk and so on to bring; You can also use Linux without affecting Windows use. But the disadvantages are also obvious, there is no graphic interface to face the novice unfriendly; Because of the network latency, the efficiency of writing code is often affected and so on. For beginners, personal comparisons recommend this solution.

As for the VPS purchase, recommend Linode or Photonvps, about monthly 50-150RMB can buy a VPS that can be used to write running ordinary program, also can put two or three small website on top.

Linux has many distributions to choose from, such as Ubuntu,fedora.

For a Linux environment on this computer, because of the graphical interface, if the work is the main purpose, recommended installation fedora. It and the server most commonly used CentOS system with Red Hat series, many places are close, familiar with it for familiar with CentOS also have a great help.

If you are installing on this computer and sometimes use Linux as a way to watch videos and other entertainment, you recommend Ubuntu, which is slightly better than fedora in everyday usage.

For the VPS to build Linux, it is recommended CentOS system, because it is the most widely used, so the support of various software is often better.

corresponding to the Linux environment Solution 1, the general use USB disk to load the ISO image for installation; For Solution 2, it is generally directly loaded with the virtual machine software installation of ISO image; For Solution 3, the general VPS can be directly selected in the rear of the operating system without its own installation, more convenient.

Editor

Before writing code, you need to have a suitable editor first. Although Windows does not have a notepad to work with, writing programs can be very uncomfortable.

For the Windows System Editor, the simpler I recommend notepad++, of course, can also choose a more popular ultraedit.

You can also use the IDE, a general term used to develop a software project's integrated environment, which has a great advantage in developing large projects, but may appear slightly inflexible when dealing with small requirements. The IDE is better with Eclipse, plus pydev plug-ins to develop Python programs that are available in Windows and Linux.

If you usually need to write a large amount of code, or more confident of your ability to learn, for Linux recommended Vim,windows recommended Gvim (Vim's graphical interface). It and ordinary editor writing code is very different, often need a few days of learning time and a few months of skilled time, but the efficiency of writing code can generally be much faster.

Start Writing Programs

Hello world!

The earliest source of "Hello world" is one of the most classic books in the computer field-the C programming Language, which was later widely used. The purpose of the Hello World program is simply to output a "Hello World" on the screen, albeit very simply, as it has a significant meaning when it is the first program written by everyone.

Open the editor, enter a line: (Friendly tip: Copy and paste adverse memory)
Copy Code code as follows:

Print "Hello world!"

Complete. Save file to c:\hello.py (path free).

Press Win + R shortcut, open "Run", enter CMD, return, open the command line prompt. (Linux graphical interface words are ctrl+alt+t to open the terminal)

Input:

(Windows)
C:\python27\python.exe c:\hello.py

(Linux)
Python path/hello.py

You can see the program running and displaying a line of "Hello world!". At this point, the first program is complete.

Variables and Assignments

Copy Code code as follows:

s = ' Hello world! '
Print S


Program output:
Hello world!

The above code, S is called a variable; the first line of action is called an assignment.

Another example to deepen the impression:

Copy Code code as follows:

A = 1
b = 2
Print A + b


Program output:
3

Function

Copy Code code as follows:

def hello (name):
s = ' Hello ' + name + '! '
return s
Print hello (' world ')
Print hello (' semwatch ')

Program output:
Hello world!
Hello semwatch!

Functions are used to wrap a series of behaviors by passing one or more arguments into the function (def hello (name)), and then the function returns a computed value (return s).

So print hello (' world ') is equivalent to the output of the Hello function after the operation of the result, that is, the output Hello world!

You can look at this example again to deepen your understanding:

Copy Code code as follows:

Def add_num (A, B)
Return a + b
Print Add_num (1, 2)
Print Add_num (2, 3)

Program output:
3
5

Circulation and judgment

The For loop, while loop, and if judgments that are mentioned later, are the basis of all program languages and must be fully mastered.

(There is no other language in Python that has a Do While loop, no switch case judgment)

For loop:

PHP Example:
Copy Code code as follows:

For ($i =0 $i <10; $i + +) {
echo "$i \ n";
}
?>

One example of PHP here is because most program languages have a For loop that is similar to the syntax for this example, first in this form.

It should be easier to understand, give the variable i assignment 0 ($i =0), each time the loop +1 ($i + +), in I is less than 10 of the loop ($i <10), so the loop 10 times.

The Echo statement is responsible for outputting the digital output of each loop, in this case outputting 0-9 of these numbers, but it is not necessary to study the echo syntax, just understand the concept of the For loop.

the Python for loop syntax is a bit unorthodox, but it's also concise:

Copy Code code as follows:

For I in range (10):
Print I


The two statements, like the previous one, output 0-9 of these digits, and the print statement automatically wraps each time it is printed.

While loop:

Copy Code code as follows:

i = 0
While I < 10:
i = i + 1
Print I


The meaning of the code is to give me a value of 0 first, and then when I is less than 10, the contents of the loop body are executed continuously. This is to add the value of I by 1 and output I.

The results of the run output are 0-9 digits in turn.

If judgment:

Copy Code code as follows:

i = 1
If I < 2:
Print "I<2"
Else
Print "i>=2"

Program output:
I<2

The program first assigns a value of 1, then determines if I is less than 2, if less than 2 output i<2, otherwise output i>=2.

In addition equals and not equal to the judgment symbol is as follows:

Copy Code code as follows:

i = 1
if i = = 1:
Print "I=1"
Elif I!= 1:
Print "I!=1"


equals = = = Symbol, this is easy to confuse. The use of the = symbol creates problems in most languages and is directly considered a code error in Python. And not equal to the symbol is!=, can also use <&gt, but the <> in today's programming language is less use, may be discarded in the future version of Python, so generally recommended to use!=.

The third line in the above code elif is the shorthand for else if. If I==1 is not satisfied, then continue to judge whether I!=1 is satisfied.

Lists and dictionaries

Most languages have an "array", which is a variable with multiple values. And Python does not, like "lists" and "dictionaries" and so on.

If you have PHP basics, look at the following comparisons to quickly understand them:

List:

Php

Copy Code code as follows:

$l = Array (1,2,3);
?>


Python

Copy Code code as follows:
L = [1,2,3]


Dictionary:

Php

Copy Code code as follows:

$d = Array (
' A ' => 1,
' B ' => 2,
' C ' => 3,
);
?>

Python

Copy Code code as follows:

D = {
' A ': 1,
' B ': 2,
' C ': 3,
}


If there is no other language basis, it may be more appropriate to understand the list and the dictionary through the actual code.

List:
Copy Code code as follows:

L = [1,2,3]
For this in L:
Print current

The results of the run output 1-3 of these digits in turn. (There is no PHP foreach syntax in Python, all with a for completion)

Dictionary:

Copy Code code as follows:

D = {
' A ': 1,
' B ': 2,
' C ': 3,
}
For key in D:
Print key
Print D[key]

The results of the run are output a1b2c3 and are displayed in six rows.

Explain the dictionary, the structure of the dictionary is this:

Dict = {key:value, key2:value2, ...}

For loops in each cycle, the corresponding key assigned to the variable, if you want to access the corresponding value, you need to use similar dict[key], that is, to find the corresponding key in the Dict value.

Similarly, lists have similar operations, such as:

Copy Code code as follows:

L = [1,2,3]
Print L[1]

Run the output:
2

It means the 1th item of the output list L. It's important to note that for most programming languages, it starts with the No. 0 item. That is, here l[0] is the 1,l[1] is the 2,l[2] is 3.

Strings and slices

A string, such as its name, is a string of characters. such as print "Hello world!" is the output string Hello world!. The strings in Python are special, and they belong to the same sequence type as the list, and many use methods and lists.

Copy Code code as follows:

s = "abc"
Print S[1]


The result of the run is output B.

Based on this feature, you can derive some concise code usage. For example, to output "Hello world!" The first 5 characters of this string, most languages will be similar to PHP:

Copy Code code as follows:

$s = "Hello world!";
Echo substr ($s, 0,5);
?>


And Python is much simpler:

Copy Code code as follows:

s = "Hello world!"
Print S[:5]


Both pieces of code will output hello.

The S[:5] uses Python's "slice" feature, the complete writing should be s[0:5], but 0 can be omitted, which represents the output of this sequence index number from 0 to 5 before the content.

Of course, a list can also use slices, such as:

Copy Code code as follows:

L = [1,2,3]
Print L[1:]

Post-run output [2,3].

That means starting with index number 1 and outputting to the end of the list.

Summary of Knowledge points

Combine all the previous content with some new knowledge points and merge into a piece of code. Think carefully about how each output is going to come out and understand it better. Because the knowledge point is more, may temporarily superficial understanding, but these are any program language inside will have the foundation content, finally is must grasp.

Code, #号意味着注释的开始, these contents will not be run.

In addition, the first line of code declares that the code is encoded as Utf-8. Because non-English characters appear in the annotation, you need to specify the encoding or the program will complain.

Copy Code code as follows:

#-*-Coding:utf-8-*-
str_1 = ' Hello '
str_2 = ' world! '

#以下语句都会输出Hello world!

#print语句中的逗号意味着不换行, but spacing them with spaces
Print str_1, str_2

#以下和上面一句是同样的效果
Print str_1,
Print str_2

#同样可以用字符串连接符
Print str_1 + ' + str_2

#也可以用格式化操作符, it is more common than the former
#会将后面的变量依次替换到前面的%x above
#%s specifies to be replaced with a string, if the replacement integer is%d, the decimal%f
print '%s '% (str_1, str_2)

#会输出00 11 22
For I in range (3):
print '%d%d '% (i, i),
print ' #输出一个空的字符串用于换行

#会输出11 22 33
For I in range (1, 4):
print '%d%d '% (i, i),
print '

#会输出00 22 44
For I in range (0, 5, 2):
print '%d%d '% (i, i),
print '

#会输出0 1 0 1 0
i = 0
While I < 5:
i + 1 #等同于i = i + 1
If I% 2==0: #如果i除2的余数为0
Print 1,
Else
Print 0,
print '

#会输出c
i = 3
If i==1:
print ' A '
Elif i==2:
print ' B '
Elif i==3:
print ' C '
Else
print ' ERROR '

#会输出1 2 3 3 4 4 5 5 6
For I in range (10):
i + 1
Print I,
If I < 3:
Continue #如果i <3 interrupts the cycle and runs back from the head of the loop body
Elif i > 5:
Break #如果i >5 interrupts the entire loop
Print I,

At last

Although the program is now mentioned, SEO should involve more than just the process of technology. Although I would like to start from the beginning to mention all the aspects should be learned, but this is a few books are not finished, only a few articles more can not be exhausted.

In the following article, will be skipped some knowledge points, if the reader does not have the relevant basis, you need to learn. The following will be skipped including but not limited to:

1. Basic knowledge of the web, such as the URL of a non-ASCII character set requires coding and other common sense: http://www.w3school.com.cn/html/html_urlencode.asp

2. The use of browser developer tools, you can choose the chrome developer tools, or Firefox firebug Plug-ins, and personal recommendations with chrome. In particular, it is necessary to understand the grasp of the package function, in the collection of data often need to use. Grab pack function in the Chrome Developer tool, the corresponding "Network" tab.

3. Independently configure Python's Pycurl module. There are two main options here, one is to install curl in the Windows system, reinstall the Setuptools, the last installation module, more cumbersome and not recommended, and later other modules in Windows configuration may also be so troublesome. Another option is to configure a Linux environment, in the CentOS and Fedora systems, execution: sudo yum install Python-pycurl, in Ubuntu and other systems, the implementation of sudo apt-get install Python-pycurl. After that will be a lot of use to this module, so be sure to configure well. Linux entry commands are visible: Http://ooxx.me/common-ssh-commands.orz

4. Understand regular expressions. It is a powerful tool for handling strings, you can find information on Baidu to do a preliminary understanding of it, at least first know. * and [\s\s]*] The meaning of these two, they are most commonly used in the process of collecting data. This is a very good but difficult regular tutorial, may not be very suitable for beginners: http://manual.phpv.net/regular_expression.html

In addition, it is best to buy a python book, although because SEO does not need to be involved in the program deeply, most of the program class books on the importance of most of the knowledge is not high. For example, each book will spend a lot of space to introduce object-oriented programming ideas and implementation, but most of the SEO requirements are often only hundreds of lines of code, are generally not very likely to involve these. However, the beginning of some basic knowledge in the process of learning, easily overlooked or forgotten knowledge, there is a book will be much better.

I haven't found a Python book that is more suitable for SEO. I have a "python core programming" on hand and I should say it's OK. If you are not sure which to buy, you can go to this forum to find the PDF look, it above all kinds of computer books should be relatively the most complete: http://club.topsage.com/forum-300-1.html

But do not only look at the PDF, generally learn a language at least a paper book, it should be in their own unclear or forget what knowledge points, you can always take to look at the reference books.

Article Author: ZERO

This article from Semwatch Reprint please indicate the source link and the author.
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.