2nd. How Python Runs the program

Source: Internet
Author: User

2nd Chapter

How Python Runs the program

?

This chapter and the next chapter will take a quick look at the execution of the program-how you start the code, and how Python runs it. In this chapter, we'll look at how the Python interpreter executes the program. The 3rd chapter will show you how to build your own program and run it.

?

The startup details of a program are inherently relevant to a particular platform, and some of these chapters may not apply to the platform you're working on, so high-level readers should skip over the parts that are irrelevant to their intended use. Similarly, readers who have used similar tools in the past and expect fast-forward language may want to put some of these chapters on the shelf for future reference. For others, let's take a short look at how Python will run our code before learning how to write code.

?

Introducing the Python Interpreter

?

So far, I've mostly talked about python as a programming language. However, as far as the current implementation is concerned, it is also a package called an interpreter. An interpreter is a class of programs that are used to execute other programs. When you write a Python program, the Python interpreter reads your program and executes the instructions it contains. In fact, the interpreter is a layer of software logic between your code and the computer hardware of your machine.

?

When the Python package is installed on your machine, it generates several components-at least an interpreter and a support library. Depending on how you use it, the Python interpreter may behave as an executable or a set of libraries linked to another program. Depending on the kind of Python you run, the interpreter itself can be implemented as a C program, a set of Java classes, or something else. Whatever form you take, the Python code you write must be executed by the interpreter. And in order for the Python interpreter to execute your code, you must install the Python interpreter on your computer.

?

Python's installation details vary by platform and are covered in Appendix a more thoroughly. In short terms:

?

    • Windows users get and run a self-installing executable file to install Python on their machine. Simply double-click and follow the prompts to select Yes or next.
    • Linux and Mac OS x users may have preinstalled a usable python--on their computers today it is a standard component on these platforms.
    • Some Linux and Mac OS x users (and most UNIX users) compile Python from a full-scale source bundle of Python.
    • Linux users can also find RPM files, and Mac OS x users can find a variety of Mac-specific installation packages.
    • Other platforms have installation technologies associated with those platforms. For example, Python can be used for mobile phones, tablets, game consoles, and ipods, but the installation details vary widely.

?

Python itself can be obtained from the download page of its primary site http://www.python.org. It can also be found through a variety of other distribution channels. Keep in mind that you should always check to see if it already exists before you install Python. If you're working on Windows 7 and earlier versions of Windows, you'll usually find python,2-1 in the Start menu, and we'll discuss the menu options shown here in the next chapter. On Unix and Linux, Python may exist in your/usr directory tree.

?

Figure 2-1 Python is presented in your Start menu when you install Python in Windows 7 and earlier versions of Windows. This differs depending on the version of Python, but idle always launches a development GUI, and Python starts a simple interactive session. There are also standard manuals and the Pydoc file engine (Module Docs). For more information on Windows 8 and other platforms, see Chapter 3rd and Appendix A.

?

Since installation details are so relevant to a particular platform, here we will pause to discuss the rest of the story. For more details on the installation process, refer to Appendix A. Given the purpose of this chapter and the next chapter, I'll assume that your python is in place and ready to move on.

?

Execution of the program

?

What it means to write and run a Python script depends on whether you are looking at these tasks as a programmer's point of view or as a Python interpreter. Both views present important ideas for Python programming (perspective).

Programmer's Perspective (view)

?

In its simplest form, a Python program is just a text file that contains a Python statement. For example, the following file, named Script0.py, is one of the simplest Python scripts I can think of, but it is a legitimate, full-featured Python program:

Print (' Hello World ')

Print (2 * * 100)

?

This file contains two Python report prints, just prints a string (the text in quotation marks) and the result of a numeric expression (2 to 100) to the output stream. Don't worry about this code syntax but for this chapter, we're only interested in 28 | Chapter 2: How Python Runs the program and lets it run. I will explain why your print statement can increase the power of 2 by 100 in Python without overflow in the next part of the book.

?

You can create such a file as any text editor you like. By convention, the Python program file name ends. PY; Technically, the naming scheme is the only file that is "imported" with the word "import" explicitly in the next chapter but most Python files require a consistent py name.

?

When you enter these statements into a text file, you must tell Python to execute the file that runs all the report files from top to bottom, one after another. You will see in the next chapter that you can launch the Python program file Shell command line, click on the icon in the integrated development environment, and with other standard techniques. If everything goes well when you execute the file, you'll see a print report on your computer somewhere on your computer that shows the results by default, usually in the same window as you do when you run the program:

2nd. How Python Runs the program

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.