Basic Introduction to Python---1

Source: Internet
Author: User

First, installation

Python is cross-platform and can run on Windows, Macs, and various Linux/unix systems. Write a Python program on Windows and put it on Linux to run it.

To start learning Python programming, you first have to install Python on your computer. Once installed, you will get the Python interpreter (which is responsible for running the Python program), a command-line interaction environment, and a simple integrated development environment.

Installing Python 3.7

Currently, Python has two versions, one is version 2.x, the other is 3.x, and these two versions are incompatible. Due to the increasing popularity of the 3.x version, our tutorials will be based on the latest Python version 3.7. Make sure that the Python version installed on your PC is up-to-date 3.7.x so that you can learn this tutorial without pain.

Install Python on your Mac

If you are using a Mac and the system is OS x>=10.9, the system comes with a python version of 2.7. Download Python 3.7 from the Python website;

Install Python on Windows

First, depending on your version of Windows (64-bit or 32-bit), download the 64-bit installer or 32-bit installer for Python 3.7 from the official Python website, and then run the downloaded EXE installation package:

Pay special attention to the hook Add Python 3.7 to PATH , then click "Install Now" to complete the installation.

Ii. Python Editor

When we write the Python code, we get a text file that contains the Python code for the .py extension. To run the code, you need the Python interpreter to execute the .py file.

Since the entire Python language is open source from spec to interpreter, it is theoretically possible for anyone to write a Python interpreter to execute Python code (which is difficult, of course) as long as the level is high enough. In fact, there are a number of Python interpreters.

CPython

When we downloaded and installed Python 3.x from the official Python website, we immediately got an official version of the interpreter: CPython. This interpreter was developed in C language, so called CPython. Running at the command line python is to start the CPython interpreter.

CPython is the most widely used Python interpreter. All the code for the tutorial is also executed under CPython.

IPython

Ipython is an interactive interpreter based on CPython, meaning that Ipython is only enhanced interactively, but the functionality and CPython of executing Python code are exactly the same. Like many domestic browsers although the appearance of different, but the kernel is actually called ie.

CPython >>> is used as a prompt, while Ipython is used In [序号]: as a prompt.

Python has a lot of interpreters, but the most widely used is cpython. All code is guaranteed to run under the CPython 3.x version. Be sure to install CPython locally (that is, the installer downloaded from the official Python website).

Iii. Example 1, Python interactive mode

In command-line mode python , you can see a bunch of text output similar to the following, and then enter the Python interactive mode, which is the prompt >>> .

  

Enter in Python interactive mode, exit() exit the Python interaction mode, and return to the command line mode:

You can also directly enter the python interactive mode by selecting the menu item directly from the Start menu, Python (command line) but exit() the input window will be closed directly and will not return to the command line mode.

In the interactive mode prompt >>> , enter the code directly, press ENTER, you can immediately get the code execution results. Now, try the input 100+200 and see if the result is 300:

Quite simply, any valid mathematical calculation can be calculated.

If you want Python to print out the specified text, you can use a print() function and enclose the text you want to print in single or double quotation marks, but you can't mix single and double quotes:

This text, enclosed in single or double quotation marks, is called a string in the program, and we will often encounter them in the future.

Finally, with the exit() exit Python, our first Python program is done! The only drawback is that it's not saved and you'll need to enter the code again the next time you run it.

2. Command line mode and Python interactive mode

In command-line mode, you can execute python into the Python interactive environment or python hello.py run a .py file.

Execution of a .py file can only be performed in command-line mode. If you hit a command python hello.py , you see the following error:

  

The error message No such file or directory indicates that this cannot be hello.py found in the current directory and must be switched to hello.py the directory where the current directory is located before it can be executed properly:

  

In addition, running files in command-line mode differs from running .py python code directly in a python interactive environment. The python interactive environment automatically prints out the results of each line of Python code, but running the Python code directly does not.

  

Finally, the code for the Python interaction pattern is to enter a line, execute a line, and run the file directly in command-line mode to .py execute all the code in the file at once. It can be seen that the Python interaction mode is mainly for debugging Python code, but also easy for beginners to learn, it is not a formal environment to run Python code!

3 Summary

In Python interactive mode, you can enter code directly, execute it, and get results immediately.

In command-line mode, you can run the .py file directly.

Basic Introduction to Python---1

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.