The Python program language Quick Start tutorial

Source: Internet
Author: User
Keywords Seo python

Intermediary transaction SEO diagnosis Taobao guest Cloud host technology Hall

This article is for the SEO crowd of the Python Program language Introductory tutorial, but also apply to other no program basis but want to learn some programs to solve simple practical application needs of the crowd. In the later will try to use the most basic angle to introduce this language.

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 written about practical code that you can choose to impress again.

But if you feel that 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 hundreds 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.

Second, Python code is very flexible

To see a very common code example, if there is no basis for the program will not understand the code, but it does not matter, look at the number of lines can also compare the code simplicity.

PHP:

<?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:

1 2 3 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.

Third, Python is widely used

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:

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

Another interesting thing is 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 less efficient. 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 it is difficult to run.

Operating

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 this 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, recommended for Linux vim,windows 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. #p # subtitle #e#

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

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 is ctrl+alt+t to open the terminal)

Enter:

(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

1

s = ' Hello world! ' print s

Program output:

Hello world!

The above code, S is called a variable;

Another example to deepen the impression:

2 3 A = 1 b = 2 print a + b

Program output:

3

function

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 outputting the result of the operation of the Hello function, that is, the output Hello world!

You can look at this example to deepen your understanding:

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) #p # subtitle #e#

For loop:

PHP Example:

<?php 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 alternative, but also concise:

1 2 for I in range: 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:

i = 0 while i &lt; 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:

i = 1 if i &lt; 2:print "i&lt;2" Else:print "i&gt;=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:

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 <>, 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 doesn't, like "lists" and "dictionaries".

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

List:

PHP

&lt;?php $l = Array (1,2,3);?&gt;

Python

L = [1,2,3]

Dictionary:

PHP

&lt;?php $d = Array (' A ' =&gt; 1, ' B ' =&gt; 2, ' C ' =&gt; 3,);?&gt;

Python

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:

1 2 3 L = [1,2,3] for current 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) #p # subtitle #e#

Dictionary:

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:

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.

s = "abc"
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.