The last lesson has taught you to install Python's interpreter, so this lesson we can formally write code
Description: In the following code demonstration, I will mostly use the Python interaction to demonstrate the input and output of the code, noting that ">>>" is followed by the input code, and no other flag represents the output
In accordance with past practice, first of all, we still write a Hello world program
#-*-coding:utf-8-*-#Author = susmote print ("Hello World")
According to the above procedure, we simply make an explanation
Character encoding
The first one to explain is the Python encoding format, which is encoded by ASCII by default when Python's code is loaded.
In our Python program, we do not have to deal with Chinese, but we know that ASCII does not support Chinese, so we need to add in the first line of the Python file in the code format Declaration.
For the Chinese encoding, we must mention the 1980 gb2312, when the earliest computer Chinese code, only then gb2312 supported Chinese characters only included 7,445 characters, including 6,763 characters and 682 other symbols
GB2312 support of Chinese characters too little, so in 1995 and out of the new Chinese character extension specification gbk1.0,gbk1.0 a total of 21,886 symbols, divided into Chinese characters and graphic symbol area, the Chinese character area consists of 21,003 characters. In the back of the Chinese character encoding in the continuous upgrading, but also support a lot of Chinese minority text, such as Uyghur, Tibetan, although the current PC support Chinese encoding format has been very full, but for embedded devices, such as mobile phones, MP3, MP4 generally only support GB2312.
It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode
Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more
UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved in 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes.
To learn more about character encoding, please check the Internet
It is important to note that Python3 default support is Utf-8, so it is generally not necessary to beware of coding errors, but in order to be compatible with more platforms, it is recommended to include a declaration code in some projects
Comments
And then the following line is a comment that explains the name of the code author, which you can also develop the habit of stating your personal code
There are two kinds of annotation formats in Python, one-line comments and multiple-line comments
Single-line Comment: # comment Content
# This is when the line comment
Multiline comment: ' The content of the comment '
"This is a multiline comment Teksi official website www.susmote.com"
Output with input in the output Hello world we use the print function
When using the print function in Python2, it is very different from the Python3.
Python2 in the output can be without parentheses
>>> print "Hello" Hello >>> i = 2 >>> print I 2
In Python3, however, the parentheses are mandatory, and no parentheses are added to the error.
>>> print ("Hello") Hello >>> i = 1 >>> print (i) 1
In addition to the output, Python certainly has input functions
In terms of input, Python2 and Python3 also have a bit different
Input function can add parameters as a prompt
Python2 input data, with raw_input () and input () two functions
- Raw_input () treats all inputs as strings, returning string types
- Input () can only receive input from a number, and can also enter an expression that returns the type of the number entered (int, float)
>>> name = raw_input ("Please enter your name: ") Please enter your name: susmote >>> type (name)
Input string Return string
>>> age = raw_input ("Please enter your Age: ") Please enter your ages: + >>> type <type ' str ' >
Enter a numeric value to return a string
>>> result = Raw_input ("Please enter an arithmetic expression: ") Enter an arithmetic expression: 2*50 >>> Print Result 2*50
A string is returned after an expression is entered
>>> age = Input ("Please enter your Age: ") Please enter your ages: + >>> type <type ' int ' >
Input only supports entering numbers
>>> result = input ("Please enter an arithmetic expression: ") Enter an arithmetic expression: 2*50 >>> print result 100
Input can evaluate an expression
Tip: the type () function can return data types
The input () function in Python3 receives a standard input data, returns a string type, and has only one input in Python3, removing the raw_input.
>>> name = input ("Input your Name") input your namesusmote >>> name = input ("Please enter your name:")
Please enter your name: Susmote >>> Type (name)
>>> age = Input ("Please enter your Age:") Please enter your ages: + >>> type <class ' str ' >
>>> result = input ("Please enter an expression:") Enter an expression: 2*50 >>> Print (result) 2*50
Of course Python also provides a way to easily hide the entered password
We only need to import this module before use
Import Getpass
Then use the Getpass.getpass () method
>>> passwd = Getpass.getpass ("Please enter your password: ") Please enter your password: >>> print (passwd) 123456
Default is not displayed on input, this is a bit like Linux user password input
In this section we learned about Python's encoding format, how to annotate it in Python code, and the input and output of Python through a Hello World program.
03-First script and input/output _python programming path