Exercise 1: A Good First Program

来源:互联网 发布:激光雕刻软件 编辑:程序博客网 时间:2024/05/21 05:41

The first program among programming language for beginner is Hello World program. You can print a string such as Hello World in this programming language.

Hello World in python is very simple.

All code is

print "Hello World"
Then put the code into a single file named whatever you want, such as ex1.py.

Notice the file extension py, it's not required but recommended.
Now that you complete a python source code file, you can run it in the terminal like this

python ex1.py
You can see the output Hello World.

If you have an error like this

$ python ex1.py   File "ex1.py", line 1    print "Hello World.                      ^SyntaxError: EOL while scanning string literal
It's import that you can read this since you will be making many of these mistakes.Let's look at this line-by-line.
  1. Here we ran our command in the terminal to run the ex1.py script.
  2. Python then tells us that the file ex1.py has an error on line 1.
  3. It then prints this line for us.
  4. Then it puts a ^ (caret) character to point at where the problem is.Notice the missing" (double-quote) character?
  5. Finally, it prints out a "SyntaxError" and tells us something about what might be the error. Usually these are very cryptic, but if you copy that text into a search engine, you will find someone else who's had that error and you can probably figure out how to fix it.

Warning

If you are from another country, and you get errors about ASCII encodings, thenput this at the top of your python scripts:

# -- coding: utf-8 --

It will fix them so that you can use unicode UTF-8 in your scripts without a problem.


原创粉丝点击