The first Python program

Source: Internet
Author: User

The first Python program

Before we formally write the first Python program, let's review what is command-line mode and Python interaction mode.

Command-line mode

In the Windows Start menu, select "Command Prompt" and go to command-line mode with a prompt similar to the C:\> following:

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.

Knowing how to start and exit the Python interaction mode, we can formally start writing Python code.

In the interactive mode prompt >>> , enter the code directly, press ENTER, you can immediately get the code execution results. 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:

>>> print ('Hello,world') Hello, World

Finally, with the exit() exit Python, our first Python program is done!

command-line mode and Python interaction mode

Note the distinction between command-line mode and Python interaction 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.

For example, in the Python interactive environment, enter:

>>> 100 + 200 + 300600

You can see the results directly 600 .

However, write a calc.py file that reads as follows:

100 + 200 + 300

Then execute in command-line mode:

C:\work>python calc.py

found no output at all.

This is normal. To output the result, you must print() print it yourself. Make a calc.py makeover:

Print (100 + 200 + 300)
Then you can see the result:

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 pattern is primarily intended for debugging Python code.

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.

The first Python program

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.