1. Interpreter and content encoding two lines of code on the head of a python file: #!/usr/bon/env pythonspecify what interpreter to use to run the script and where the interpreter is located #-*-coding:utf-8-*-Specifies that the file is encoded as Utf-8 Supplement (code):Assci: Used primarily to display modern English and other Western European languages, which can only be represented by a maximum of 8 bits (one byte), that is: 2**8 = 256, so the ASCII code can represent a maximum of 256 symbols. GBK: Mainly used for displaying Chinese characters, which can only be represented by a maximum of 16 bits (two bytes), and 2**16=65536, so GBK code can only represent a maximum of 65,536 symbols. Unicode: is a character encoding that is used on a computer. Unicode is created to address the limitations of traditional character encoding schemes, which set a uniform and unique binary encoding for each character in each language, stipulating that existing characters and symbols are represented by at least 16 bits (2 bytes), i.e. 2 **16 = 65536. Note: Here is a minimum of 2 bytes, possibly more. UTF-8: Is the compression and optimization of Unicode encoding, it no longer uses a minimum of 2 bytes, but all the characters and symbols are categorized: the contents of the ASCII code is saved with 1 bytes, the characters in Europe are saved with 2 bytes, the characters in East Asia are saved in 3 bytes ... Note: for py2.7 headers you need to add code #-*-coding:utf-8-*-for py3.x, the default Utf-8 2.pyc File when you execute Python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode generated after the Python interpreter was compiled. PS: Code is compiled to generate bytecode, and bytecode can be obtained by decompile. 3. Notes when the line stares: # is annotated contentMultiline Comment: "" "Annotated Content" "" 4. Variables (1.) Variable: A variable is a pronoun that refers to the data in memoryvariables are the amount of changes that can be changed--to refer to different data (2.) Rules for variable definitions:
-
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of a variable name cannot be a number
- The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
(3.) Assignment of variables:
5. Enter
1 #!/usr/bin/env Pyhton2 #-*-coding:utf-8-*-3 4 ImportGetpass5 6 #assign user-entered values to Usrname7 #py2.7--raw_input ()8 #py3---input ()9Usrname=raw_input ("Please input username:")Ten #The Getpass method in the Getpass module is that the password entered is not visible, and the Getpass module should be imported here OnePwd=getpass.getpass ("Please input passwd:") A - #Output Print Content - Print(username) the Print(PWD) - - #python syntax indentationinput
6. Process Control
1 #if...else ...2 #User Login Verification3User=raw_input ("Please enter user name:")4Pwd=getpass.getpass ("Please enter your password:")5 ifuser=="Tom" andpwd==" the":6 Print("Yes")7 Else:8 Print("Try again")9 Ten #If...elif...elif...else One #print content based on user input AInp=raw_input (">>>") - ifinp=="1": - Print('111') the elifinp=="2": - Print('222') - elifinp=="3": - Print('333') + Else: - Print('444')if...else ...
7.while Cycle
1 #Break to exit all loops2 #continue is used to exit the current loop and continue the next loop3 #Output 1,2,3,4,5,6,8,9,104Start=15 whileTrue:6 if(start==7):7Start+=18 Continue9 Print(start)Ten if(start==10): One Break AStart+=1 -Time.sleep (0.2)
while
Python-02-python Getting Started