I. Getting started with Python
1.1 Python Chinese encoding
Before learning Python's Chinese encoding, we need to know what encoding [default: Ascill]:
- Coding is the use of pre-defined methods to digitally encode text, numbers, or other objects, or to convert the information into a prescribed electrical pulse signal. Encoding is the process of converting information from one form or format to another.
- However we need to understand the character encoding as follows:
ASCII code: United States Standard Information Interchange code, mainly used to display English and European languages; it can only be represented by a maximum of 8 bits (one byte), that is: 2**8 = 256, so the ASCII code can represent up to 256 symbols. It is obvious 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. That is: Unicode
Unicode code: Also known as the Universal Code, the purpose is to solve 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 although some characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536.
Utf-8: Is the compression and optimization of Unicode encoding, he 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 European characters are saved with 2 bytes, the Chinese character is saved in 3 bytes.
- There is a difference between the Chinese encoding between python2.x and 3.x: it is necessary to declare or specify character encoding in the python2.x series environment, however python3.x does not need to declare character encoding.
- The Chinese code is as follows:
GB2312: GB code, was born in 1980, contains more than 7,000 Chinese characters
gbk:1995 year, a total of more than 20,000 Chinese characters included
GB18030: Is the 2000 to replace GBK1.0 and the birth of the official national standards, but also included the language of minority languages
1.2 python and shell of the first Hello Word program:
1 # /usr/bin/env python 2 3 Print ("HelloWorld") 4 5 >>:hello World
python
1 # 23"Hello World"45#> >:hello World
Shell
1.3 python comments:
#!/usr/bin/env python#-*-coding:utf-8-*-# The first comment #号代表单行注释 "' Comment content bar" Insert the content you want to annotate (or you can use "" " "Note: The contents of a multiline comment can be used as a variable pass-through parameter in the appropriate format
1.4 python variables:
What is a variable? In the literal sense it is the quantity that can be changed! Nima!! Too far-fetched, simply put it: used to specify different data types, these variables can store integers, decimals or characters, and then the program is called a piece of memory space.
Note: 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,from,global,if, Import,in,is,lambda,not,or,pass,print,raise,return,try,while,with,yield
1.5 python and Shell user input:
1.5.1 python user input:
#!/usr/bin/env python#-*-coding:utf-8-*-#用户前台输入并赋值给变量namename = input ("What ' s your name?") #打印出前台输入的信息print ("Welcome To "+ name +" BJ ")
1.5.2 Shell user input:
#/bin/bash #用户前台输入并赋值给Uoread-P "What's your name" Uo #给变量name赋值name = "Welcome to Beijing" # Print out information and variables entered in the foreground echo $Uo $name
1.6 Flow control if .... Elif .... Else:
1.6.1 python. If....else Statements
#!/usr/bin/env python#-*-coding:utf-8-*-#前台输入. Note the symbol (symbol: = = "") when writing the If...else statement . A variable must be added with a space a = input ("age_a:") b = Input ("Age_b:") if a = = "Ten" and b = = "C": Print (A, b) Else: print ("Gun ...")
python. if....elif....else# guess the age of the game begins
#设置变量user_age = 30# user input age. Information entered in Python in the background defaults to the str string type user_input = Int (input ("Please enter the old:")) #if条件判断if User_ Input = = User_age: print ("Welcome to the user Ipuut Correct") elif user_input < user_age: print ("Errors, Please enter the age. "," the correct ages are: ", user_age) else: print (" idiot ")
1.6.2 Shell. If....else Statements
#注意: You cannot add a space after a variable in the shell. There must be a space before and after the comparison, and it needs to be written in []. Then the conclusion cannot be followed directly after the []. The following #!/bin/basha=10b=20if [$a = = $b]then echo "This is A and B:" $a $belse echo "Gun ..." echo "A and B:" $a $BFI
1.7 For Loop:
#简单粗暴的打印十次 # Note: You need to use rangefor I in range when printing a number: print ("Loop:", i) #以下是循环打印字符串. First define a list names = ["Zhang San", "John Doe", " Pock "] for name in names: print (name)
Requirements one: or the above program, but encountered less than 5 cycle times will not go, jump directly into the next cycle
For I in range: if I < 5: #大于5. Use the symbol > continue #不往下走了 under Modify to go directly to the next loop print ("Loop:", i)
1.8 While loop:
#死循环. Print and show print count = 0while True: Print ("Where there is no grass, why do you love a bird ...", count) count + = 1
Requirement One: Quit when you cycle to 100 times
Count = 0while True: print ("The end of the earth where there is no grass, why do you love a bird ...", count) count +=1 if Count = =: Print ("I'm going to turn into a playboy big Bob.") Break
1.9 Data operations:
Data operations are divided into seven categories: arithmetic operations, comparison operations, assignment operations, logical operations, member operations, identity operations, bitwise operations, and operator precedence
Arithmetic operations:
| symbol |
description |
instance |
| + |
Plus-two objects added |
A + b output results: |
| - |
minus-two subtraction |
A- b output -10 |
| * |
Multiply-two number of times |
A * b output result x |
| / |
Divide-two number division |
A/b output results 0.5 |
| % |
modulo-Returns the remainder of division |
B% A output result 0 |
| * |
Power-Returns the Y power of a and nbsp , &NB Sp |
a**b is 10 20. Output result 100000000000000000000 |
| // |
Take integer-Returns the integral part of the quotient |
9//4 Result output 2 9.0//4.0 result output 2.0 |
comparison operation:
| Symbol |
Describe |
Instance |
| == |
equals-compares two numbers for equality |
A = = B returns false |
| != |
Not equal to-compare two numbers are not equal |
A! = B returns True |
| <> |
Not equal to-compare two numbers are not equal |
A <> B returns true |
| > |
Greater than-returns whether a is greater than B |
A > B returns false |
| < |
Less than-returns whether a is less than B. Returning 0 means true 1 is false. With false and true equivalent |
A < b returns True |
| >= |
Greater than or equal-returns whether a is greater than or equal to B |
A >= B returns false |
| <= |
Less than or equal-returns whether a is less than or equal to B |
A <= b returns True |
Assignment operation:
| Symbol |
Describe |
Instance |
| = |
Simple assignment operator |
c = A+b assigns the value of A+b to C |
| += |
Addition assignment operator |
c + = a equals c = C + A |
| -= |
Subtraction assignment operator |
c-= a equals c = c-a |
| *= |
Multiplication assignment operator |
C *= a equals c = c * A |
| /= |
Division assignment operator |
C/= a equals c = c/a |
| %= |
Modulo assignment operator |
C%= a equals c = c% A |
| **= |
Power assignment operator |
C **= a equals c = c * * A |
| //= |
Take the divisible assignment operator |
C//= a equals c = c//A |
Logical operation:
| Symbol |
Describe |
Instance |
| and |
Boolean "and"-if x is False,x and Y returns false. Otherwise it returns the calculation of Y |
(A and B). Returns True |
| Or |
Boolean "and"-if x is true, it returns true, otherwise it returns the computed value of y |
(A or B). Returns True |
| Not |
Boolean "and"-if x is true, returns False if X is False, it returns true |
Not (A and B) returns false |
Member Operations:
| Symbol |
Describe |
Instance |
| Inch |
Returns True if a value is found in the specified sequence. False otherwise |
X is in the y sequence. If x returns true in the Y sequence |
| Not in |
Returns True if no value is found in the specified sequence. False otherwise |
X is not in the Y sequence. If x does not return true in the Y sequence |
Identity operation:
| Symbol |
Describe |
Instance |
| Is |
Determine if two identifiers refer to the same object |
X is Y. If ID (x) equals ID (y) is returns result 1 |
| is not |
Determine if two identifiers are referenced from a different object |
X is not Y. If the ID (x) does not equal the ID (Y). is not returns the result 1 |
Bit operations:
| Symbol |
Describe |
Instance |
| & |
Bitwise-AND operator |
(A & B) output result: binary interpretation: 0000 1100 |
| | |
Bitwise OR operator |
(A | b) output result 61: Binary interpretation: 0011 1101 |
| ^ |
Bitwise XOR OR operator |
(a^b) Output 49: Binary interpretation: 0011 0001 |
| ~ |
Bitwise inverse operator |
(~a) Output-61: Binary Interpretation: 1100 0011 |
| << |
Left move operator |
A << 2 output result 240: binary interpretation: 1111 0000 |
| >> |
Right Move operator |
A >> 2 output result 15: binary interpretation: 0000 1111 |
Operator Precedence:
| Symbol |
Describe |
| ** |
Index (highest priority) |
| ~ + - |
The bitwise rollover. Unary Plus and minus (the last two methods are called [email protected] and [email protected]) |
| * / % // |
Multiplication modulo and divide |
| + - |
Addition subtraction |
| >><< |
Left and right shift operators |
| & |
Bit ' and ' |
| ^ | |
Bitwise operators |
| <=<>>= |
Comparison operators |
| <>==!= |
equals operator |
| = %= /= //= -= += *= **= |
Assignment operators |
| Is isn't |
Identity operator |
| In No in |
Member operators |
| Not OR and |
logical operators |
Python Automation Development _ Basic syntax