3. First Python program

Source: Internet
Author: User

The first step in learning any language is to write a ' Hello World ', which is a tradition for programmers. But before writing, there are a few questions to note.

First of all, Python is a scripting language, and the scripting language is characterized by the following: The code we write will be compiled by the interpreter before it is executed. But when our program is running on top of the operating system, the system is not so smart that it can automatically identify which interpreter we want to use to interpret our code (Windows is associated with executing the program with a suffix, so it doesn't have to be declared, but our code runs more on Linux, So the declaration of the interpreter is necessary), so we have to declare what our interpreter is.

# !/usr/bin/python

In the first line of the file, write this code, when the file is executed, the system will go to /usr/bin/python to find the interpreter, and then use it to explain our code. However, as I mentioned in the first introduction to Python installation, the system comes with Python and the path of our source-installed Python is different, and we do not guarantee that our program runs in the system, Python is installed here.

Therefore, compatibility is better with this method:

# !/usr/bin/env python

If a Linux-based homecoming knows that Env is a calling environment variable in Linux, this code means looking for python in the system's environment variables and using it to interpret the code. This allows for a higher level of compatibility, no matter what method is installed, can be found only in the system environment variable.

With the above foundation, you can start to write ' Hello World ', but as a Chinese programmer, I also want to write ' Hello, what's going on? '

If it is in 3.x, then you can start directly. However, if it is 2.x, then it is necessary to declare the character set.

The concept of character sets can be a look at other related articles, as a lazy productivity programmer, or not to create repetitive wheels. Here are some summaries:

1.python2.x By default is the ACSII code, this encoding is not supported in Chinese

2. In order to support all the text in the world, and the birth of Unicode, and in order to compress Unicode in the display of English space, and the birth of Utf-8, which is also our common character set.

In fact, the use of utf-8 is always enough, I am here to list the GBK is because many people in Windows CMD inside the use of Chinese, even if the character set is declared, but still garbled.

At this point, pay attention to a problem, although you are using Utf-8, but the cmd interactive window is not to Utf-8 to display, the details look at the picture:

Its use is GBK, and you output the character is used is utf-8, with GBK to interpret utf-8 of course will appear garbled. So a lot of time to pay attention to the display terminal with what encoding.

This time to display the Chinese, modify the displayed characters, if not, then download an IDE is good, also have to pay attention to the IDE's display coding problem, with what the IDE to look at personal habits, try on a good, I use is pycharm, specifically no more said.

Tell me why you want to declare the character set, and then how to declare it, in fact, like the declaration interpreter, you can write in the second line of the file:

# -*-coding:utf-8-*-

Here's the line:

# Coding:utf-8

The lowercase utf is also OK, there are some other wording, such as: change to = number, personal habits in the first form.

After the interpreter declaration and the character set declaration, we came to the same starting hand of Python:

# !/usr/bin/env Pyhton # -*-coding:utf-8-*-

Well, after learning the starting hand, you can start writing ' Hello World '.

# !/usr/bin/env Pyhton # -*-coding:utf-8-*- Print " Hello World "
" Hello, World "

Explanation: Print is a python keyword, its role is to display something behind it to the terminal, the professional term called "print", you can print a variety of data types, such as strings, numbers, Yuan Zu, dictionaries, and so on, specifically what will be said later.

As we mentioned in the new 3.x feature in the first article, the Print keyword is replaced by the print () method, so the 3.x is written like this:

# !/usr/bin/env Pyhton # -*-coding:utf-8-*- Print ("helloWorld")
Print (" Hello, World ")

In 2.7, both of these methods are supported, which is one of the reasons why 2.7 is called an over-version.

Well, the first Python program is finished, it's that simple! Well...... Well, it's easy.

3. First Python 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.