Programmers who are not proficient in writing scripts are not good programmers-Python (1)

Source: Internet
Author: User

Author: Chen Xi

Date: 11:18:06

Environment: [Mac 10.7.1 lion intel-based I3, supports 64-bit command Python 2.7.1 gcc4.2.1]

Reprinted please indicate the source

Q1: What is the essence of the script?

A: It is a piece of code, but it can be executed by the corresponding interpreter. It corresponds to the compiled code and needs to be compiled into an executable or target file, run or use on the specified platform.

Q2: Why are programmers who are not proficient in writing scripts not good programmers?

A: If a programmer mainly writes a specific type of script, it must be like this. If not, when the main task is not to write a script, the programmer does not just write his own job, it is also inevitable that relevant testing and verification work needs to be processed. For example, if a website cannot be accessed, You need to determine whether the address is accessible and what the server returns to determine the problem. At this time, if you want to compile such code in a compiled language, the time and effort in practice are not enough. Therefore, the script has become a good choice.

Q3: Which script do you like best?

A: For the next batch processing in Windows, among the Linux Shell, Mac shell, Python, Perl, and VBScript scripts, python is the favorite. the reason is that, on the one hand, it is very similar to the C language, concise, and often easy to write code. On the other hand, it is very similar to C ++ and supports object-oriented processing.

Q4: Give a hello code for the Python language in Mac.

A: First, make sure that the python interpreter has been installed.

As you can see, it has been installed. If not, you can use macport for installation.

Write the following code and save it as hello. py:

print "hello, python!\n"

Run:

Q5: every time you input python in the command line, it seems quite annoying. Is there a simpler way?

A: Create a python soft link named P. As long as there is no conflict.

In this case, you can use the p command to replace python.

Q6: How do I write such code to create and use variables?

A: There is no strange syntax for creating and using variables. Unlike bash or Perl, it requires a $ symbol and sometimes $. I learned Bash from my sophomore year and will still forget how to deal with the $ symbol. Python only looks at it for one day and remembers its processing method, because it does not need $ to declare and define variables.

i = 5print ii = i + 1print ii = '12'print i

Save as var. py.

Run:

It can be seen that its processing is simple, I is a variable, I is initialized, And I is still used, which is easy to understand.

Q7: Sometimes, you may need to calculate the result of a large integer. If you use a programming language, such as a C language, you may need to write code for a large integer. This may be a time-consuming process, can Python provide similar computing functions?

A: Yes. A basic function provided by the python interpreter is the computing function. It also provides the direct input function, which is simple and clear. As follows:

Q8: But the C language also provides the double type, which can operate a lot of data. Can the above process be easily implemented using the C code?

A: Yes, it can be implemented, but if you need to calculate an expression using C language, the process is too complicated.

Q9: Sometimes, there are many expressions to be calculated. Isn't it too complicated to input one input as above?

A: The basic function provided by python is to interpret strings, put expressions into strings, and let Python interpret them.

The above eval can be used to calculate string parameters. When many expressions need to be calculated, we can put the expressions in a file:

1+12*312%312**214.5+34.5-12.5

Save as expr_cal. Then write a script to read the data in the file and calculate the value in one row:

file = open("expr_cal", "r")all_lines = file.readlines()for line in all_lines:    print eval(line)file.close()

Save as expr_cal.py. Then execute:

As you can see, the above Code has been correctly executed.

Q10: Sometimes, the encoding problem is annoying. Can Python provide a simple method to determine the encoding data of a character?

A: Yes. As follows:

As shown above, the Unicode encoding corresponding to the string "hello", utf8 encoding and gb2312 encoding are easy to query. In case of Encoding Problems, do not panic. python can easily give the results.

Q11: Does Python provide corresponding implementations for complex operations?

A: Yes. Complex operations are also simple and straightforward:

Q12: Sometimes I forget what is the default line break of the platform. Is there any way to get it?

A: You can use the OS linesep to obtain it. As follows:

Different results may be obtained on other platforms.

Q13: when developing and fixing bugs, you may need to operate on a specified size file. How can you quickly generate a specified size file?

A: Python provides file operation interfaces to solve this problem easily. As follows, a MB file is generated, and the last byte is 0:

After running, you can find a file of the following size:

Q14: During network access, you often need to determine the HTML data corresponding to a webpage, and sometimes search for HTML data. What is a better method?

A: using scripts is a good choice. Data Analysis is direct, although using a browser to view the source code is also a way. As follows:

The data read above is not completely displayed.

From the above we can see that the script actually plays a major role. To do a good job, you must first sharpen the tool. python is a good tool.

Author: Chen Xi

Date: 11:18:06

Environment: [Mac 10.7.1 lion intel-based I3, supports 64-bit command Python 2.7.1 gcc4.2.1]

Reprinted please indicate the source

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.