Recommended a period of time the egg pain, always feel that the fall is not very good, then picked up the previous has been wanted to learn and did not learn python, to record the study notes, but also to urge and review.
Learn the video version of the 2016 latest Python Development Fundamentals Course-2.0 version on 51cto, because the video tutorial will feel more clear than reading, but we need to find a variety of information to supplement it.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------
One, Python version
The chronology is not released, Python now has two large versions, 2.0 and 3.0. 3.0 is an upgraded version of 2.0, simplifying some 2.0 unnecessary content, but because the syntax and 2.0 are different in some places, it is not backwards compatible. This makes upgrading from 2.0 to 3.0 very difficult.
The latest version of 2.0 is 2.7, officially supported to 2020 years.
Second, the variable 1, the Interpreter
The first line of the script everyone knows to specify the interpreter to invoke, Python has two ways of writing
# !/usr/bin/python # !/usr/bin/env python
The first is the general designation of Python, the second way, such as Python is upgraded from 2.0 to 3.0, the location will be from/usr/bin to/usr/local/bin, so that in the first way, the path error can not find the interpreter. The second way to find the Python directory from the environment settings is more secure.
2. Variables
Variable names can only be any combination of letters, numbers, and underscores, and the first word of a variable cannot be a number, and the keyword cannot be declared as a variable.
Name=input ("aaa") name=raw_input ("AAA") name=int (input (" AAA "))
The first one is a 3.0 screen input, the second is a 2.7 screen input notation.
The third is the way to convert strings into numbers.
There are several ways to display variables:
Name =input ("Name:") Age=input ("Age :") Job=input ("Job:")Print("inofmation of []:"+ name +"\nname:[]"+ name +"\nage:[]"+ Age +"\njob[]"+ Job)#show variable plus sign each time it re-opens up memory space, more station memoryPrint("Informationg of%s:\nname:%s\nage:%s\njob:%s"% (Name,name,age,job))#There are a few% (S is a string, D is a number, F is a decimal point) write several variablesmsg=" "inofmation of%s name%s age%s job%s" "% (Name,name,age,job)#Three quotation marks can be displayed in the paragraph, and the same as printed, while the paragraph hand three quotation marks in the range of the gaze, single and double quotation marks the same effect
3. If judgment
A="2"ifa=="1": Print("111")elifa=="2": Print("222")Else: Print(" the")
If the judgment is not much different from the other usage, it is important to note that the indent of the paragraph is mandatory, and Python is judged by the paragraph indent to end.
4. Cyclic 4.1 while loop
While true: true loops, infinite loops, to determine if true is true. Note case.
While a=b: Determines whether a is equal to B, satisfies the condition to continue the loop, and does not meet then jumps out of the loop. If the variable is not assigned a value then the error will be assigned in advance.
N=-1Luck=13Guess_count=0#While luck! = N and Guess_count < 3: whileGuess_count < 3: N=int (Input ("Enter a number:")) #print (n) #print (Luck) ifLuck = =N:Print("OK") Break elifn >luck:Print("big.") elifN <luck:Print("it's small.")Guess_count+=1Else: Print("when the above conditions do not meet the end of the loop, the column will be printed, and if the loop is not exited normally, it will not execute")
PS: In the python3.0 can be directly input Chinese, 2.7 can not be directly input, need to encode processing.
4.2. For loop
for in range (3):
Enter the number of loops in the range directly.
Third, data type 1, digital
Numbers are divided into three types, int integers, float floating point, long length integer.
Long integer type is a long integer, in 3.0 the integer number is automatically converted to long integer, do not use special care.
Floating point is the decimal point, which accounts for 8 bytes, where 52 bits represent the bottom bit, 11 bits represent digits, and the remaining 1 bits represent the symbol.
Type (a)
This command allows you to see the number types.
2. String
Name=aprint("I am%s")% name
%s represents a string,%d represents an integer, and%f represents a decimal.
The commonly used functions of strings are removal of blanks, splits, lengths, indexes, and slices.
2.1, remove the blank
Name =input (" name: "
Strip in parentheses can be customized to remove the content, the default is blank.
2.2. List
A list can store multiple information that is used to invoke an element of the list individually, starting with the index value of 0.
Dir () can view commands, where double underscores are private commands.
Append Append,count statistics,extend extension, indexindex, insertInsert,pop delete one, removedelete Specify one,reverse invert, Sort sorts
2.2.1, append append
Add an element to the last item by default
2.2.2, index indexes
Finds the index value of an element within a list
2.2.3, Count statistics
Count the number of elements in a list
2.2.4, insert Insertion
Inserts the specified element at the specified index location
2.2.5, pop Delete
Delete last Element
2.2.6, remove
Delete the specified element
2.2.7, Reverse reversal
Invert index values
2.2.8, sort sorting
Sort by number, special character, number start, alphabetical order
2.2.9, Cycle
Extracts the number of repetitions of an element to be deleted in a corresponding number of cycles
2.3. Slicing
Split the list, and finally the first bit of the index value, ignoring the tail
Interval tangent
Reverse Tangent
String extension, put B into a
Each letter of the string is split separately as an element into
Judging if 4 in List A, just print ddd
3, Ganso
Name_list= ("1", "2", "3")
Changes the list's [] to (), read-only list, only the statistics slices, and can be converted between Ganso and lists by command.
Lits (a) tuple (a)
4. Boolean value
1 is true and 0 is false. True is True, False is false.
& and Arithmetic, two of them are 1, 1.
| or operation, one for 1 is 1
^ Non-arithmetic, different 1
Iv. operation of files
(Do not say I am lazy to use directly ... )
First, 3.0 no file, changed to open. Secondly, in fact, I am full of confusion, the feeling is not used to see it is difficult to remember .... Wait for more practice and then fill it up.
f = open ("test.log","w") f.write ("111 \ n") # creates a new file that overwrites the old file, and if not W is a then append f.write ("111\n " ) F.close ()
f = open ("test.log","R") for in F: Print (line,) F.close ()
Beginner's Python learning note 1