Skip to content

The First Python Program

Before officially writing your first Python program, let's review what Command-Line Mode and Python Interactive Mode are.

Command-Line Mode

In Windows, select "Command Prompt" from the Start menu to enter Command-Line Mode. The prompt will look similar to C:\>:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> _                                                  │
│                                                        │
└────────────────────────────────────────────────────────┘

Python Interactive Mode

In Command-Line Mode, type the command python to see an output similar to the following, which indicates that you've entered Python Interactive Mode with the prompt >>>:

┌────────────────────────────────────────────────────────┐
│Command Prompt - python                           - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> python                                             │
│Python 3.x ... on win32                                 │
│Type "help", ... for more information.                  │
│>>> _                                                   │
│                                                        │
└────────────────────────────────────────────────────────┘

In Python Interactive Mode, type exit() and press Enter to exit back to Command-Line Mode:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    - □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> python                                             │
│Python 3.x ... on win32                                 │
│Type "help", ... for more information.                  │
│>>> exit()                                              │
│                                                        │
│C:\> _                                                  │
│                                                        │
└────────────────────────────────────────────────────────┘

Alternatively, you can select the Python (command line) menu item from the Start menu to directly enter Python Interactive Mode. However, typing exit() in this mode will close the window directly without returning to Command-Line Mode.

Now that you know how to start and exit Python Interactive Mode, we can officially begin writing Python code.

Before writing code, never use "copy" and "paste" to transfer code from a webpage to your computer. Programming also requires a sense of touch; you need to type the code letter by letter yourself. During the coding process, beginners often make mistakes: incorrect spelling, wrong case, mixing Chinese and English punctuation, or mixing spaces and Tab keys. Therefore, you need to carefully check and compare to quickly master how to write programs.

type.jpg

Writing Your First Python Code

In the Interactive Mode prompt >>>, directly enter the code and press Enter to immediately see the execution result. Now, try typing 100 + 200 to see if the calculation result is 300:

python
>>> 100 + 200
300

Very simple, right? Any valid mathematical calculation can be computed.

To make Python print specific text, you can use the print() function and enclose the text you want to print in single or double quotes, but you cannot mix single and double quotes:

python
>>> print('hello, world')
hello, world

This text enclosed in single or double quotes is called a string in programming, and you will encounter it frequently in the future.

Finally, use exit() to exit Python. Your first Python program is complete! The only downside is that it wasn't saved, so you'll need to retype the code the next time you run it.

Command-Line Mode vs. Python Interactive Mode

Please distinguish between Command-Line Mode and Python Interactive Mode.

In Command-Line Mode, you can either enter Python Interactive Mode by executing python or run a .py file by executing python hello.py.

Executing a .py file can only be done in Command-Line Mode. If you type the command python hello.py and encounter the following error:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    _ □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> python hello.py                                    │
│python: can't open file 'hello.py': [Errno 2] No such   │
│file or directory                                       │
│                                                        │
└────────────────────────────────────────────────────────┘

The error message No such file or directory indicates that hello.py cannot be found in the current directory. You must first change the current directory to the one where hello.py is located before executing it:

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    _ □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> cd work                                            │
│                                                        │
│C:\work> python hello.py                                │
│Hello, world!                                           │
│                                                        │
└────────────────────────────────────────────────────────┘

In Windows, to switch to another drive, for example, the D: drive, you need to type D::

┌────────────────────────────────────────────────────────┐
│Command Prompt                                    _ □ x │
├────────────────────────────────────────────────────────┤
│Microsoft Windows [Version 10.0.0]                      │
│(c) 2015 Microsoft Corporation. All rights reserved.    │
│                                                        │
│C:\> D:                                                 │
│                                                        │
│D:\> cd work                                            │
│                                                        │
│D:\work>                                                │
│                                                        │
└────────────────────────────────────────────────────────┘

At the D:\> prompt, continue using the cd command to switch to the work directory, and then you can execute python hello.py normally.

Additionally, running a .py file in Command-Line Mode is different from directly running Python code in the Interactive Mode. The Python Interactive Environment automatically prints the result of each line of Python code, but directly running Python code does not.

For example, in Python Interactive Mode, typing:

python
>>> 100 + 200 + 300
600

will immediately display the result 600.

However, if you write a calc.py file with the following content:

100 + 200 + 300

and execute it in Command-Line Mode:

C:\work>python calc.py

you will see no output.

This is normal. To output the result, you must explicitly use print(). Modify calc.py as follows:

python
print(100 + 200 + 300)

Then, executing it will display the result:

C:\work>python calc.py
600

Finally, in Python Interactive Mode, each line of code is entered and executed one by one, whereas running a .py file in Command-Line Mode executes all the code in the file at once. Thus, Python Interactive Mode is primarily for debugging Python code and is convenient for beginners to learn. It is not an environment for officially running Python code!

Example: Exponentiation and Syntax Errors

In Python Interactive Mode, entering 2**10 will give you:

>>> 2**10
1024

If you encounter a SyntaxError, it means there is a syntax error in the Python code you entered. A common syntax error is using Chinese punctuation, such as Chinese parentheses and :

python
>>> print'hello'
  File "<stdin>", line 1
    print'hello'
         ^
SyntaxError: invalid character '(' (U+FF08)

Or using Chinese quotation marks and :

python
>>> print(“hello”)
  File "<stdin>", line 1
    print(“hello”)
          ^
SyntaxError: invalid character '“' (U+201C)

When errors occur, be sure to read the error messages carefully. For the above SyntaxError, the interpreter clearly indicates that the error is due to an unrecognized character: invalid character '“'.

Summary

  • In Python Interactive Mode, you can directly input code, execute it, and immediately see the results.
  • In Command-Line Mode, you can directly run .py files.
The First Python Program has loaded