3. The first python program and the first python Program
To learn any language, you must first write 'Hello world', which is a programmer's tradition. But before writing, you should pay attention to several issues.
First, python is a scripting language, which features that the code we write will be compiled by the interpreter before execution. However, when our program runs on the operating system, the system is not so intelligent that it can automatically identify which interpreter we want to use to explain our code (windows associates the execution program with the extension name, so we do not need to declare it, but our code runs more on linux, so the interpreter declaration is required). Therefore, we must declare what our interpreter is.
#! /usr/bin/python
Write this code in the first line of the file. When the file is executed, the system will find the interpreter in/usr/bin/python and use it to explain our code. However, as I mentioned in the first article about python installation, the python paths in the system are different from those installed in the source code, but we cannot ensure that python is installed here in the system where we run the program.
Therefore, the following method provides better compatibility:
#! /usr/bin/env python
If you have a linux-based member who knows that env is used to call environment variables in linux, This code refers to finding python in the environment variables of the system and then using it to explain the code. In this way, higher compatibility can be achieved, no matter what method is installed, only in the system environment variables can be found.
At this time, some people have doubts, not to say # Are all comments followed, and comments are not executed. Why is this Code effective?
In fact, the interpreter Declaration is also a type of annotation, but it is quite special. Remember to use it like this, and you don't need to go too far. The following character set Declaration is also the same, there is nothing special about the two.
With the above foundation, you can start to write 'Hello world'. But as a Chinese programmer, I want to write 'hello, world'. What should I do?
If it is in 3. x, you can start it directly. However, if it is 2. x, You need to declare the character set.
The concept of character set can be to look at other related articles. As a programmer who promotes productivity with laziness, it is better not to duplicate the wheel. The following is a summary:
1. python2.x uses the acⅱ code by default. This encoding does not support Chinese characters.
2. Unicode was born to support all the texts in the world, while UTF-8 was also created to compress the space occupied by Unicode in english display, which is also our common character set.
In fact, UTF-8 is enough. I listed gbk separately here because many people use Chinese Characters in windows cmd, even if the character set is declared, it still displays garbled characters.
At this point, pay attention to a problem. Although you use UTF-8, the cmd interactive window is not displayed as UTF-8. For details, see the figure:
#-*-Coding: UTF-8 -*-
The following statement can also be used:
# coding: UTF-8
The lower-case utf statement can also be used. For example, if you replace "=" with "=", you may use the first method.
After the interpreter declaration and Character Set Declaration are completed, we get the same starting style of python:
#! /usr/bin/env pyhton# -*- coding:utf-8 -*-
Well, after learning the starting method, you can start to write 'Hello world.
#! /Usr/bin/env pyhton #-*-coding: UTF-8-*-print "hello world"
Print "Hello, world"
Explanation: print is a keyword of python. It is used to display things behind it to a terminal. It is called "print" and can print various data types, such as strings and numbers, yuanzu, Dictionary, etc.
As we mentioned in the new feature 3. x in the first article, the keyword "print" is replaced by the "print ()" method. Therefore, the keyword "3. x" should be written as follows:
#! /Usr/bin/env pyhton #-*-coding: UTF-8-*-print ("hello world ")
Print ("Hello, world ")
In MySQL 2.7, both methods are supported, which is also one of the reasons why MySQL 2.7 is called an excessive version.
Well, the first python program is finished. It's so easy! Hmm ...... Okay, that's easy.