First knowledge of Python

Source: Internet
Author: User
Tags gz file pycharm download

1 Python Introduction 1.1 What is Python

Python is an object-oriented, interpreted computer programming language, invented by Dutchman Guido van Rossum (Guido van Rossum) in 1989, and the first public offering was released in 1991. The latest version of Python's current release is Python3.6.

Python is purely free software, and the source code and the interpreter follow the GPL (GNU general public License) protocol. Python syntax is simple and clear, and one of the features is to force whitespace (white space) to be indented as a statement.

Python has a rich and powerful library. It is often referred to as glue language, and it is easy to integrate various modules made in other languages, especially in C + +. A common scenario is the use of Python to quickly build a prototype of a program (sometimes even the final interface of the program), and then to the special requirements of the part, in a more appropriate programming language rewrite, such as the 3D game Graphics rendering module, performance requirements are particularly high, can be rewritten in C + +, It is then encapsulated as an extension class library that Python can invoke. It is important to note that you may need to consider platform issues when you use the Extended class library, and some libraries may not provide cross-platform implementations.

Although Python source code files (. py) can be executed directly using Python commands, Python does not actually directly interpret the Python source code, but rather first compiles the Python source code into Python byte code (python bytecode, The bytecode file extension is typically. pyc) and is then executed by Python virtual machine (a Python VM, which can be referred to as PVM) for Python byte code. That is to say, Python is an interpreted language, referring to the interpretation of Python Byte code, not the Python source code. The basic idea of this mechanism is consistent with Java and. Net.

Although Python also has its own virtual machines, Python's virtual machines are different from Java or. NET virtual machines, and Python's virtual machines are a more advanced virtual machine. The high-level here is not in the usual sense, not that Python's virtual machines are more powerful than Java or. NET, but that Python's virtual machines are farther away from the real machine than Java or. Net. Alternatively, Python's virtual machine is a higher level of abstraction than a virtual machine. The process of compiling and running the Python Language program code is shown.

1.2 Python's application areas

Python is a cross-platform programming language that, in theory, can run on any operating system platform. The most common operating system platforms available today are Windows, Mac OS x, and Linux. Undoubtedly, these 3 platforms will become the main battlefield of Python.

Python's simple, easy-to-learn, numerous third-party libraries, and fast-running features make Python an incredibly wide area of application. Python's applications are mainly in the following areas.

  • Linux/unix operation and Maintenance
  • Command-line program development
  • GUI program development (PYQT, Kivy, etc.)
  • Web program Development (Django and other frameworks)
  • Mobile app Development (PYQT, Kivy, etc.)
  • Server-side program development (socket-based protocol)
  • Web crawler (provides data sources for search engines, deep learning, etc.)
  • Data analysis
  • Deep learning
  • Scientific calculations

    Although not all of Python's areas of application are listed here, the areas listed in the light include most of the development scenarios. Readers of Mac OS X or Linux will find that the Python interpreter is already built into the two operating systems, meaning that Python programs can run directly on Mac OS X and Linux. So many OPS engineers are accustomed to using Python to do a lot of automated things. And python in the operation of the network, the text is particularly prominent. The first version of the Google search engine was written in Python. Now super-hot deep learning is inseparable from Python, and Python has become the first language of deep learning. So, from every point of view, whether it's a student, a programmer, a data analyst, or a scientist, you can't live without python. Python has become a world language in the field of programming languages.

    1.3 How to learn Python

    How to learn Python language, is all beginners need to face the problem, in fact, each programming language learning methods are very similar. Here are some suggestions for beginners.

  • Get a general idea of how Python is a programming language and the main areas of application. To be blunt, it is to understand what Python can do and what it does best. There is a macro understanding of the Python language.
  • Build a development environment, no matter 3,721, first make a Hello World (you can copy the existing code). This will give yourself the confidence to continue learning. We can imagine, after one weeks of learning how to write a line of code, how much more interest will continue to learn?
  • Do not delve into design patterns, these things are for those who have many years of experience in the preparation of programmers, and design patterns are not for learning, not to remember, is based on their own years of experience and practice. It's like learning English, just say it, whatever grammar, say more, English nature is pure. So in the beginning to write a program, just write it, let the design pattern to hell!
  • Imitate the example code in the book, you must write your own code, of course, the first to see the results of the operation, you can copy the examples in the book directly, but be sure to write it all over again, the code can be different from the example in the book, as long as the same function can be achieved.
  • In the process of writing code, there is no need to memorize the language of Python, if some of the grammar can not remember, put these grammar on the card, or simply put on the Youdao cloud notes, so that they can be viewed at any time, write more naturally remember.
  • Beginners do not need to read a lot of Python books, nor need to look over the Internet too much technical information, because in their own skill has not reached a certain degree of heat, too much information will distract their energy, may be counterproductive. In a martial arts in the words said is: the obsession. Therefore, for beginners, should make full use of the code given in this book to do more practice, when the book gives a variety of knowledge and skills, you can take whatever you want to get the knowledge.
  • Readers should make the most of the Python video lesson presented with the book, which is synchronized with the book. The difference between the book and the video is that the book can only give a result, and the video can not only give the results, but also a detailed demonstration of the operation process, which is particularly important for beginners.
  • In the process of copying the code in the example of a book, there may not be a thorough understanding of some of the code, which does not matter, readers should read this book carefully, and use the accompanying book of Python video lessons to watch repeatedly, in order to understand the mysteries. If you do not understand, you can also go to the Geek origin it question and answer community (https://geekori.com) to ask questions, there will be a lot of people to answer the questions you ask.
  • This book is equipped with a large number of exercises, readers can use these exercises to better grasp the knowledge points in the book and video, and these exercises are not rigidly given, but through the "through", "testing ability" and other forms given (these functions need to rely on the Ourui technology, "geek Question Bank" small program implementation). Through these functions, readers can complete their learning tasks in a breakthrough way and know their own knowledge points.
  • Often summing up is a good habit, which is especially important for programmers. Readers can often put their learning experience, and often need to look at the content of the blog (such as https://geekori.com), so as not only to improve their technical skills, but also improve their language expression ability.
  • Frequently answering the technical questions raised by peers is also a way to improve technical capabilities and be more effective. If you are not able to answer any technical questions, or can only answer less than 10% of technical questions, then you are a little white. If you can answer more than 30% of the technical questions, then you have at least reached the programmer's intermediate level, if this ratio is 50%, then congratulations, you have become a master in the field. If it increases to 80%, it will become an expert in this field without any suspense. In addition, Ourui Technology's geek origin it question and answer community (https://geekori.com) contains a wide range of questions raised by Li Ning's many students and readers, and readers can also try to find out which level they belong to. In addition, ask questions, answer questions and write blogs in https://geekori.com, and you'll get a decent amount of points for more learning resources.
  • Read the source code extensively. If the previous method readers do well, then believe that the reader's technical level has a qualitative leap, the rest of the work is to further improve their technical ability. Reading the source code of various open source projects is a very good way. Also, be aware that reading the source code is not an end in order to understand the rationale behind the source code, and of course, the ultimate goal is that you can implement a similar function. Readers can get the various types of Python source code to https://github.com.
2 Build Python Development Environment 2.1 Install the official Python runtime environment

No matter what tool you use to develop a Python program, you must install Python's operating environment. Because Python is cross-platform, before installing, you need to determine which operating system platform to install, the most common is the Windows, Mac OS x and Linux three platforms. Because the number of people currently using Windows is the largest, this book mainly focuses on the development of Python's operating environment and program.

The reader can download the Python installation package for the appropriate operating system platform directly on the Python website.

Https://www.python.org/downloads

On the download page, the browser will display a different Python installation package download link depending on the operating system. If the reader is using the Windows platform, the Python download page appears as shown.

If the reader is using the Mac OS x platform, the Python download page appears as shown.

The "Download python 3.6.2" and "Download python 2.7.13" two buttons (which may vary slightly over time) appear on the download page of any operating system platform. Since this book is explained in Python 3, click the "Download Python3.6.2" button. If it is the Windows platform, download EXE installer, if it is the Mac OS X platform, download the pkg file, which is the installation program on Mac OS X, install directly.

Now it's mostly about how the Windows platform installs the Python runtime environment. First run the downloaded EXE file, which will display the Python installation interface as shown. It is recommended that the reader select the "Add Python 3.6 to path" check box below the interface so that the installer will automatically add the Python path to the PATH environment variable.

In this interface there are two installation options, "Install Now" and "Customize installation", generally click "Install Now", click on this option, will start to install Python, is to display the installation progress of the interface, Readers just have to wait patiently for Python to finish installing.

When the installation is complete, the installation Success screen shown will appear.

2.2 Configuring the PATH environment variable

After installing the Python runtime environment, we can test the Python runtime environment, but if you forgot to select the "Add Python 3.6 to PATH" check box during the installation of Python, by default, The Python installer does not add the Python installation directory to the PATH environment variable. In this way, we cannot execute the Python command in any directory in the Windows command-line tool, and must go to the Python installation directory to use the Python command.

To make it easier to execute Python commands, it is recommended that you add the Python installation directory to the PATH environment variable. The steps for configuring the PATH environment variable on the Windows platform are as follows:

1. Go back to the desktop of Windows, right-click on "Computer", and in the pop-up menu you will see the "System" window as shown in the "Properties" menu item.

Clicking Advanced System settings on the left side of the system window pops up the System Properties window as shown.

Click the environment variables (N) ... button below the System Properties window to bring up the environment Variables window as shown.

There are two lists in the Environment Variables window, the above list is set environment variables for the Windows currently logged on user, and the environment variables set here are only valid for the currently logged on user. The following list is an environment variable set for all users, meaning that these variables are valid for all users. Where the reader can set the PATH environment variable, this book sets the PATH environment variable in the list above. If there is no PATH environment variable in the list, click the "New (N) ..." button to add a new PATH environment variable. If you already have the PATH environment variable, double-clicking the path will bring up the Edit User Variable dialog box.

The reader needs to add the Python installation directory to the variable value (V) text box, separated by semicolons (;) between the multiple paths. So how do we find the Python installation path? In fact, underneath the "Install Now" button on the Python installation interface is the default installation path for Python, which can be modified, although it is generally kept by default. If you still use the Python default installation path, you need to add the following path at the end of the PATH environment variable.

C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32

We can go to that path and we'll see the contents of the directory as shown. Obviously, we're going to use the Python.exe file in the directory.

Now open the Windows command-line tool, execute the python--version command, if output Python 3.6.2, the Python installation is successful.

2.3 Installing the Anaconda Python development environment

To develop a complete Python application, it is not enough for the light to use the modules provided by Python itself, so a large number of third-party modules are needed. Installing these third-party modules when you publish your Python app is a headache, but with Anaconda, it's a lot easier to do. The anaconda is an integrated Python runtime environment. In addition to the operating environment that contains Python itself, many third-party modules are integrated, such as the NumPy, pandas, flask, and other modules that are integrated in the anaconda, so that, as long as Anaconda is installed, the modules do not need to be installed.
Anaconda installation is quite simple, first into the Anaconda download page, the address is as follows:
Https://www.anaconda.com/download

Anaconda's download page will also automatically switch to the appropriate Anaconda installation package based on the operating system that the user is currently using. Anaconda is a cross-platform support for Windows, Mac OS x, and Linux. No matter which operating system platform the installation package, download the direct installation.

Anaconda's installation package is divided into python3.x and python2.x two versions, because the current python3.x the latest version is Python3.6, and python2.x the latest version is Python2.7, so it is customary to call the two version Python3.6 version and Python2.7 Version, as this book uses Python3.6, so readers are advised to download the Python3.6 version of Anaconda. The download interface is as shown. After downloading, install directly, if the Python path environment variable is not set after installation, you can set it as the previous section.

2.4 Installing Pycharm

Pycharm is a dedicated IDE for developing Python programs developed by JetBrains, a company that has developed a number of very popular Ides, such as Webstorm, Intellj idea, and so on, where Android Studio (IDE for Android app development) is based on the INTELLJ idea Community Edition.

There are two versions of Pycharm: Community Edition and Professional Edition. The Community edition is free, but features are limited, but the case for writing this book with Pycharm is sufficient.

Readers can download the Pycharm installation file at the Pycharm website below.

Https://www.jetbrains.com/pycharm

Although Pycharm downloads the page, slide the page vertical scroll bar to the lower middle, you will see the download button for Pycharm Pro and Community edition as shown.

The pycharm download page automatically switches to the appropriate installation file based on the operating system that the user is currently using, Windows is an EXE file, Mac OS x is a DMG file, and Linux is a tar.gz file. The reader simply clicks the "DOWNLOAD" button on the right to download the installer for the appropriate operating system platform.

After downloading Pycharm, you can run Pycharm, run Pycharm for the 1th time, and display the Welcome screen as shown. Click the "Create New Project" button to create a Python project.

2.5 Configuring Pycharm

Now click on the "Create New Project" button on the Pycharm Welcome screen to display the "New Project" window, which is used to create the Python project. Enter the name of the Python project in the Location text box, and if the reader wants to select a different Python runtime, click "Project Interpreter" in the "New project" Below the window is displayed as shown in the Python Runtime Environment selection interface.

If the reader has already configured the Python runtime environment in Pycharm, select a python runtime environment from the interpreter list. If the reader has not yet configured the Pycharm, you will need to click the button to the right of the "Interpreter" list box and then click on the "Add Local ..." menu item in the pop-up menu to pop up the "add local Python Interpreter" window as shown.

Select "Virtualenv Environment" in the list on the left and click the ellipsis button to the right of the Interpreter list box on the right, and a "Select Python Interpreter" window will appear, as shown in. Select the anaconda or other Python interpreter in the window and click the "OK" button to close the window.

Next, go back to the New Project window, select the Python runtime you just specified in the interpreter list, and then click the Create button to build the Python project. An empty Python project is shown.

Python source code files can be placed anywhere in the Python project, usually the Python source code files in the SRC directory, and then select the SRC directory, in the context menu, click "New" > "Python File" The menu item creates a Python file (here is test.py) as shown in.

The first time you run a Python program can select the test.py file, and then select the "Run ' Test" menu item in the right-click menu to run the test.py script file and run it later, you can directly click the green arrow button in the upper right corner of the Mycharm main interface. Now enter a simple line of code for the test.py file, such as print (' Hello World '), and then run the test.py script file to get the output as shown. If the reader follows the previous steps and gets this output, it means that Pycharm has been installed successfully.

### 2.6 Python中的REPL环境

Python has the following 3 modes of operation.

    • Run directly from the python command
    • Running in the Python IDE
    • Running in Python's REPL environment

      This section describes how to run a python program in a REPL environment, where REPL is the abbreviation for Read-eval-print Loop, a simple interactive programming environment, or a python repl environment called the Python console. For the sake of unification, the chapters later in this book refer to Python repl as the Python console.

      The reader only needs to execute the Python command in the Windows command-line tool to enter the REPL environment. Enter print ("Hello World") after the command prompt (>>>) and press ENTER to output "Hello World" in the REPL environment, as shown in.

      If you press CTRL + Z to exit the REPL environment under Windows, press Ctrl+d to exit the REPL environment if you are in Mac OS x.

1.3 First Python program

This section will write our first Python program. This program defines two integer-type variables n and m, adds two variables, and finally calls the print function to output the sum of the two variables.

(1) The reader can create a demo.py file using any text editor, Eclipse, or Pycharm, and enter the following Python code.

n = 20m = 30print("n + m =",n + m)

It is recommended that the reader use Eclipse or pycharm to create the Demo.dy file, because doing so can execute the Python program directly in the IDE, and does not need to switch back and forth between the text Editor and the Window command-line tool like a command-line run of a Python program.

(2) running a python program

If you want to run demo1.py in the Windows command-line tool, you can go to the directory where demo1.py is located in the command-line tool and run demo1.py with the following command.

Python demo1.py

The output of running demo1.py in the command-line tool is as shown in.

1.4 Debugging Python Programs

When developing a complex Python program, if a bug occurs (that is, an error in the program), you need to debug the program to find the line of code that corresponds to the bug. There are many ways to debug a program, for example, you can use the Print function to output the appropriate information at different points in the program to narrow the scope of the bug. This approach is primitive, however, and it is now common to use the debugger to keep track of the lines of code in a way that makes it very easy to find where the bug is.

Because Pydev is an Eclipse plug-in, debugging a Python program can be a similar approach to debugging Java (Pycharm's debugging approach). Eclipse provides a debug interface that invokes the debugger for various programming languages in the background. Python's debugger is called when debugging Python code.

The steps for debugging Python code with the Python debugger are as follows:

(1) Setting breakpoints

Setting breakpoints is an essential step in the process of debugging a program. The Python debugger suspends the current thread each time it encounters a breakpoint, that is, pauses the current program's run.

You can display the location of the code line number in the Python editor double-click to add or remove a breakpoint for the current line, or right-click where the current line number is, and a pop-up menu appears as shown. Clicking the Add Breakpoint menu item in the menu adds a breakpoint to the current line of code. You can also click the Disable Breakpoint menu item to disable the breakpoint for the current line. If the current line already has a breakpoint, the Remove Breakpoint menu item appears in the menu, and clicking the menu item deletes the breakpoint for the current line.

Add a breakpoint after the Python editor as shown in. Where line 2nd sets a breakpoint.

(2) Run the Python program in debug mode

Running in Eclipse is divided into two ways: Release and Debug, which is release and Debug. If you just observe the performance of the program, you can choose the release method, if you want to debug the program, you need to use the debug mode.

The Debug button is shown on the left side of the Eclipse toolbar, 1-54 (with a small reptile button on the left of the release button).

Now click the Debug button, you will run the Python program, if the Python program does not set any breakpoints, the debug and release mode of operation is the same effect, will output the results of the operation. However, by running the program in debug mode, Eclipse enters the debug perspective (Perspective), where the perspective is where multiple related views are put together in a certain location and size to facilitate work. The debug perspective is a debugging-related view that is put together for debugging purposes.

If there is a breakpoint in the Python program, the program is paused when the program executes to the breakpoint, and the breakpoint line is displayed with a green background, as shown in.

(3) Observation of debugging information

The main purpose of debugging is to show the data in the program, that is to say, debugging is the data in the program. So, with the Python debugger, you can observe the changes in the data in a Python program in a variety of ways. For example, since n = 20 is in front of M = 30, after an interrupt at m = 30, n = 20 must have been executed, so you can place the mouse over the n = 20 statement, and the data type and current value of the variable n will be displayed in the pop-up window, as shown in.

You can also see how variable values change in the variables view in the upper-right corner, as shown in.

(4) Tracking Debug Program

Another important feature of debugging is the ability to track programs step-into (individual jumps) and step over (single-hop). Step into can be traced inside the function, and step over does not follow the inside of the function. These two functions can be achieved by clicking on the two buttons on the top left side, as shown in.

Now click on the "Step Over" button and the Python code area M = 30 Line of code is executed, and the green background runs to the next line, as shown in.

1.5 Combat and practice
    1. Try modifying the example of section 1.3 to make the program output the value of n * M.

    2. Try to write a Python program, using "*, + 、-、/" Four in the arithmetic character to enclose two lines of string, and Eclipse's console view output as shown in the results.

First knowledge of Python

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.