Ref:vamei Source: Http://www.cnblogs.com/vamei
1. Variables do not need to be declared
>>> A = 10
>>> Print a
>>> print Type (a)
Out:10
<type ' int ' >
2. Retrieving variable names
If you want a variable to store different data, you can directly assign a new value
>>> A = 1.3//assigns a double to a, before which a is an int
>>> print A, type (a)
out:1.3 <type ' float ' >
3. Basic data type:
A = ten # int
A = 1.3 # float
A = true # True, False
A = ' Hello ' # string can also be ' "" double quotes
A = fractions. Fraction (A, b) # Score
A = character
A = plural
Summary
Variables do not need to be declared, they do not need to be deleted and can be recycled directly.
Type (): Query data type
Integer, floating-point, Truth, String
Sequence (sequence) is a combination of a set of ordered objects
A sequence can contain one or more elements, or it can be an empty
There are two types of sequences
Tuple: Fixed value table, tuple elements cannot be changed
List: Each element of the table can be changed
>>> S1 = {2, 1.3, ' love ', 5.6, 9, B, False} # S1 is a tuple
>>> s2 = {True, 5, ' Smile '} # s2 is a list
>>> print S1, type (S1)
>>> print s2, type (s2)
>>> s3 = [1, [3,4,5]]
>>> s3 = []
The subscript of the sequence element starts with 0 :
>>>print S1[0]
>>>print S2[2]
>>>print S3[1][2]
Because the elements of the list can be changed, you can assign a value to an element of list:
>>>S2[1] = 3.0
>>>print S2
If you do this to a tuple, you get an error message.
So, you can see that the reference to the sequence is by S[<INT>] implementation, int is subscript
Range reference: Basic style [lower limit: Upper limit: Step length]
>>>print S1[:5] # from start to subscript 4 (the elements of subscript 5 are not included)
>>>print s1[2:] # from Subscript 2 to the last
>>>print S1[0:5:2] # from subscript 0 to subscript 4 (subscript 5 not included), take one element every 2 (the element labeled 0,2,4)
>>>print S1[2:0:-1] # from subscript 2 to subscript 1
As can be seen from the above, the upper limit is not included in the range reference when the upper limit is indicated.
Trailing element reference
>>>print S1[-1] # The last element of a sequence
>>>print S1[-3] # third element of sequence countdown
Similarly, if s1[0:-1], then the last element will not be referenced (again, excluding the upper bound element itself)
String is a tuple, (individual elements cannot be changed)
A string is a special tuple, so you can perform related operations on tuples.
>>>str = ' abcdef '
>>>print Str[2:4]
Summary
Tuple element immutable, List element variable
Sequence of references s[2], S[1:8:2]
A string is a tuple
Judge
Judging whether it's true or false, return to True/false
>>>print 5==6 # =, equal
>>>print 8.0!=8.0 #! =, Unequal
>>>print 3<3, 3<=3 # <, less than; <=, less than or equal to
>>>print 4>5, 4>=0 #, greater than; >=, greater than or equal to
>>>print 5 in [1,3,5] # 5 is an element of list [1,3,5]
(also is, are not, etc., temporarily not in depth)
Logical operations
Operations between the True/false
>>>print true and True, True and False # and, "and" operations, both are true
>>>print true or False # or, "or" operation, one of which is true is true
>>>print not True # not, "non" operation, negation
You can do some exercises with the previous section, such as:
>>>print 5==6 or 3>=3
Summary
Math +,-, *,/, * *,%
Judge = =,! =,, >=, <, <=, in
Logical AND, or, not
Indent in
Use: and indent to express affiliation
The most distinctive feature of Python is the code that is labeled as a block in indentation. I will use the if selection structure below for an example. If followed by the condition, if the condition is true, then a block of code that belongs to the IF is executed
If i > 0: x = 1 y = 2
In Python, the parentheses around the i > 0 are removed, the semicolon is stripped at the end of each statement, and the curly brackets of the block disappear.
Come out more if ... After: (colon), there is the indentation of x = 1 and y = 2 preceded by four spaces. By indenting, Python recognizes that both statements are subordinate to the IF.
The reason Python is designed purely for the sake of the program.
This is a four-space indent to indicate how the affiliation is written, and you'll see it later. Forced indentation enhances the readability of the program
Summary
The colon after the IF statement
The affiliation is represented by the indentation of four spaces and cannot be indented in Python
If < conditions 1>:
Statement
Elif < conditions 2>:
Statement
Elif < conditions 3>:
Statement
Else
Statement
For loop
For a in [3,4.4, ' life ']: print a
For a in range: print a**2//* * means power
While loop
While I <: print i i = i + 1
Break the Loop
For I in range (10):
Continue Print I
For I in range: if i = = 2: Break Print I
Function
First, we'll define a function to illustrate the function.
Def square_sum (A, B): c = a**2 + b**2 return c
The function is to find the sum of squares of two numbers.
First, Def, this keyword informs Python: I'm defining a function. Square_sum is the function name.
A in parentheses, B is the parameter of the function, and is the input to the function. Arguments can have more than one or none at all (but the parentheses remain).
We have seen in loops and choices the affiliation of the colon and the indented representation.
c = a**2 + b**2 # This sentence is an operation inside a function
Return C # Returns the value of C, which is the function of the output. Python's functions allow for no return value, i.e. not returning .
Return can return multiple values , separated by commas. is equivalent to returning a tuple (a fixed value table).
Return A,b,c # equals return (A,B,C)
The function is defined first by using the
Parameters for the base data type: value passing
Table as parameter: pointer passing
Python 2 data types, sequences, operations, indents and selections, loops, functions