Translation: "Practical Python Programming" 01_01_Python

Contents | [Next section (1.2 The first program)] ()

1.1 Python

What is Python?

Python is an interpreted high-level language, usually classified as a "scripting language" and considered similar to languages ​​such as Perl, Tcl, or Ruby. The syntax of Python is roughly inspired by the C programming language.
Python was created by Guido van Rossum in 1990 and named after Python to commemorate the circus Monty Python that Guido van Rossum liked.

Where to get Python?

You can get Python from Python.org . For this course, all you need is a basic installation of Python. It is recommended to install Python 3.6 version or a newer Python version, because Python 3.6 is used in the course and practice questions.

Why was Python created?

Why was Python created? In the words of the creator of Python:

My original motivation for creating Python was that I needed a higher-level language in the Amoeba [operating system] project. Considering the time it takes to develop system management tools in C language is too long, and for various reasons, performing these operations in the Bourne shell (translator's note: Bourne shell is a type of shell) is invalid, so a Language to bridge the gap between C language and shell.

  • Guido van Rossum

Where is Python on the computer?

Although Python can be run in many environments, Python is usually installed on your computer as a program, and this program can be run in a terminal or a command-line shell. On the terminal, you can enter Python like this:

bash $ python
Python 3.8.1 (default, Feb 20 2020, 09:29:22)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>>

If you are just starting to use the shell or terminal, you may want to stop first, find a short course about shell or terminal to learn, and then return here to continue this course.
Although you can write Python code in many non-shell environments, if you can run, debug, and interact with the terminal, you will become a more powerful Python programmer. This is the native environment of Python. If you can use Python in the native environment, then you can use Python anywhere else.

Exercise

Exercise 1.1: Use Python as a calculator

On your computer, start Python and use it as a calculator to solve the following problems.
Fortunately, Larry bought 75 shares of Google at a price of $235.14 per share. Today, Google’s stock price is $711.25 per share, using Python’s interactive mode as a calculator to calculate how much profit Larry will make if he sells all of his shares.

>>> (711.25 - 235.14) * 75
35708.25
>>>

Advanced technique: Use the underscore (_) variable to use the last calculation result. Example: If a stockbroker takes away 20% of the rake, how much profit does Larry make?

>>> _ * 0.80
28566.600000000002
>>>

Exercise 1.2: Get help

Use help()function to obtain the relevant abs()help functions. Then you can also use the help()function to get on round()the help function. Just type without parameters help()function can enter the interactive help viewer (viewer).
Use help()function has a point to note is: help()function does not apply to basic Python statements, for example for, if, whilethe statement (that is, if you enter help(for), you will get a syntax error (SyntaxError)). The alternative is: put the topic that needs help in quotation marks (quotes, translator's note: in Python, single quotation marks and double quotation marks are the same, so quotes can refer to single quotation marks ('') or refers to the double quotation marks ( "")), for help("for")example: . If this is still useless, then you have to search online.
Advanced: Visit < http://docs.python.org> and find abs() the documentation for the function (hint: you can find it in the Built-in Functions section of the Library Reference).

Exercise 1.3: Cut and Paste

This course is composed of a series of traditional web pages. It is recommended that you try to manually enter these interactive Python sample codes yourself . Especially for those who are learning Python for the first time, this "slow method" is recommended. By slowing down the learning speed, entering the code manually, and thinking about what you are doing, you will have a better understanding of the language Python.
If you have to cut and paste sample code, from &gt;&gt;&gt;the beginning of the prompt selection, has been selected to the last, but do not exceed the first empty row or the next &gt;&gt;&gt;prompt (whichever comes first the &gt;&gt;&gt;prompt date). Select "Copy" from the browser, then return to the Python window, and then select "Paste" to copy the code into the Python shell. After you paste it, in order for the code to run, you need to press the Enter key (Translator's Note: On a Windows computer, it is the Enter key , and on a Mac computer, it is the Return key ).
Use "cut and paste" to execute Python statements in this session:

>>> 12 + 20
32
>>> (3 + 4
         + 5 + 6)
18
>>> for i in range(5):
        print(i)

0
1
2
3
4
>>>

Warning: Paste can not be a plurality of Python commands ( command means &gt;&gt;&gt;behind the sign statements (statements) ) to substantially the Python shell, the command can only be a paste.
Now that you have completed this step, remember that by entering the code slowly and thinking about it-instead of cutting and pasting, you will gain more after the course.

Exercise 1.4: Where is my bus?

Now try some advanced content, type the following sentence, and see if the people waiting for the next Northbound CTA #22 bus at the corner of Clark street and Balmoral street in Chicago need to wait how long:

>>> import urllib.request
>>> u = urllib.request.urlopen('http://ctabustracker.com/bustime/map/getStopPredictions.jsp?stop=14791&route=22')
>>> from xml.etree.ElementTree import parse
>>> doc = parse(u)
>>> for pt in doc.findall('.//pt'):
        print(pt.text)

6 MIN
18 MIN
28 MIN
>>>

The above about 6 lines of code download a web page, parse an XML document, and extract some useful information. The data accessed above is actually provided to the website < http://ctabustracker.com/bustime/home.jsp> ;. Run the above code again and observe the expected changes.
Note: This service only reports the arrival time of buses within the next 30 minutes. If you are in a different time zone than Chicago, and it happens to be 3 AM in Chicago, you may not get any output. You can use the tracking link above (< http://ctabustracker.com/bustime/home.jsp> ;) to double check.
If the first import statement import urllib.requestimport fails, it is probably because you're using Python 2. For this course, you need to make sure you are using Python 3.6 or newer. If necessary, please visit < https://www.python.org> ; to download.
If your work environment requires the use of an HTTP proxy server, you may need to set HTTP_PROXYenvironment variables in order to make this part of the exercise normally. Example:

>>> import os
>>> os.environ['HTTP_PROXY'] = 'http://yourproxy.server.com'
>>>

If you can't complete the exercises in this section, it doesn't matter, the rest of the course has nothing to do with parsing XML documents.

Contents | [Next section (1.2 The first program)] ()

Note: see for full translation https://github.com/codists/practical-python-zh

Guess you like

Origin blog.51cto.com/15137915/2667919