How to think like a computer scientist Original English Version 2
Forgive me for being easy to understand. You don't need to explain it too much and list some things you need to pay attention.
1 Programming path
Errors in the program: syntax errors, runtime errors, and Semantic Errors
2 variables, expressions, and statements
2.1 types and values
Different values belong to different types. The type is a set that defines the value range.
In python, a single line of vectors separated by commas (,) is interpreted as tuple.
Print (1,000,000): the output is 1 0 0. The print function interprets the comma as a separator.
T = 1,000,000 print (t) its output is (, 0)
2.3 keywords
Variables cannot be keywords. You can use print (keyword. kwlist) to view the currently defined keywords in Python.
2.4 statement
A statement is a command that the interpreter can execute, such as a value assignment statement.
2.5 expression
An expression is a combination of variables, values, and operators, such as 1 + 1.
2.6 operators and operands
The operator represents the symbols of a specific computation, for example, + ,-
Note: After python3, The raw_input () function is deleted, and the input () function replaces raw_input (). The former input () function can be implemented by eval (input ().
3 Function
4 Conditions
5. Functions with return values
Function Type in pthon
6 Traversal
More than 6.1 assignments
A = 5
B = A // ID (A) = ID (B)
B = 3 // ID (! = ID (B)
6.9 encapsulation and Generalization
Encapsulation refers to placing code blocks in functions. generalization refers to extracting special functions to better cope with general situations.
6.13 advantages of functions
A good function name makes the code easier to read. Divides long code into functions for debugging. Well-defined general functions can be reused.
7 string
You can use = to compare strings.
The string module defines some constants of the string, such as string. printable and string. ascii_lowcase. You can use help (string) to view details.
Ascii_letters = 'authorization' <br/> ascii_lowercase = 'authorization' <br/> ascii_uppercase = 'authorization' <br/> digits = '2016' <br/> hexdigits = 'authorization' '<br/> octdigits = '000000' <br/> printable = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu... <br/> punctuation = '! "# $ % &/'() * +,-./:; <=>? @ [//] ^ _ '{| }~ '<Br/> whitespace ='/T/N/R/x0b/x0c'
String formatting: "<format>" % (<values>)
9 list
9.8 delete a list element
Del list1 [Slice]
9.10 objects and values
Str1 = '000000' <br/> str2 = '000000' <br/> print ID (str1) = ID (str2) # True <br/> lst1 = [1, 2, 3, 4, 5] <br/> lst2 = [1, 2, 3, 4, 5] <br/> print ID (lst1) = ID (lst2) # False <br/> lst1 = lst2 <br/> print ID (lst1) = ID (lst2) # True
In this example, str1 and str2 reference the object '123', which is called stri as the identifier. On the surface, the values of two string objects are equal. The ID () shows that the two objects are actually one object. But the lst1 and lst2 identifiers are different. They reference two different lst objects, and the two objects only have the same value. Here we can see how python uses mutable objects and immutable objects.
After the value assignment operation, we use an alias. lst1 and lst2 point to the same object, and any change can be reflected by the other. If we want to copy a list, we can use the following method: lst3 = lst1 [:]
9.13 list Traversal
You can use the in method or enumerate () to return an (index, value) pair for traversal.
9.14 list as a parameter
List is passed as a reference when it is used as a function parameter. If the list is modified inside the function, the caller can see these changes. At the same time, if a list is returned, it is also a reference, that is, the list created inside the function can be used outside. In fact, both the input and return parameters of functions in Python are referenced. After the function returns, although the variables are unavailable, the objects referenced by this variable are retained.
11 recursion and exceptions
11.1 tuples
A comma-separated sequence can be enclosed. Therefore, the tuple of a single element needs to be like this (5,), otherwise the interpreter considers this as an integer.
Multivariate value assignment A, B, C = C, B,
11.4 pure functions and modifiers
Def insert_in_mid_lst (Val, LST): <br/> mid = Len (LST)/2 <br/> lst [mid: mid] = [Val] <br/> lst = [2.5,] <br/> insert_in_mid_lst (, LST) <br/> Print lst <br/> def insert_in_mid_tup (Val, Tup): <br/> mid = Len (Tup)/2 <br/> return Tup [: mid] + (Val,) + Tup [mid:] <br/> tup1 = 2.5, 3,4 <br/> Print insert_in_mid_tup (, tup1) <br/> def encap (Val, SEQ): <br/> If (type (SEQ) = type (''): <br/> return STR (VAL) <br/> Elif type (SEQ) = type ([]): <br/> return [Val] <br/> Elif type (SEQ) = type (1,): <br/> return (Val,) <br/> else: <br/> raise typeerror, 'Type error' <br/> def insert_in_mid (Val, SEQ): <br/> mid = Len (SEQ)/2 <br/> return seq [: mid] + encap (Val, SEQ) + seq [mid:] <br/> Print insert_in_mid (2, (1, 2, 4) <br/>
11.5 recursive Data Structure
For example, the list and tuple elements can still be list and tuple. the built-in sum () cannot process nested sequences and must be implemented by yourself.
11.7 exceptions
For exception usage, see the code in 11.4.
11.8 tail recursion
If the function returns its own call at the end, it is called tail recursion, and Python does not recommend tail recursion.
12 dictionaries
12.2 dictionary member Method
Keys () returns the list of all keywords in the dictionary, values () returns the list of all values, items () returns the list of all entries, and has_key () determines whether the keys are in the dictionary.
12.3 dictionary Replication
Use the member method copy () to obtain a copy of the dictionary.
12.4 Sparse Matrix Representation
Matrix = [[0, 0, 0, 1, 0], <br/> [0, 0, 0, 0], <br/> [0, 2, 0, 0, 0], <br/> [0, 0, 0, 0], <br/> [0, 0, 0, 3, 0] <br/> smatrix = {(): 1, (): 2, (): 3} <br/> Print smatrix [()] <br/> Print smatrix. get (4, 3), smatrix. get (0, 0), 0)
For a sparse matrix, the list method causes the majority of positions to be 0, which wastes space. Therefore, you only need to record the positions and values not 0 points. You can use dictionaries to simulate the storage of sparse matrices. Because the dictionary throws a keyerror when accessing a key that does not exist, the dictionary provides the get (Key, [Val]) member method. If the key does not exist, it returns Val or none.
12.6 big integer
Python provides Integers of the long type to support any large integer. To create a large integer, you only need to use long () to convert it, or add the suffix L after the number.
Class 13 and objects
13.1 attributes
In python, classes and objects can both have attributes. objects can be dynamically added to new attributes after classes are instantiated. Of course, you can add these attributes in advance in each class. In the definition body of a class, if a data attribute is not cited by self,
13.6 Similarity
Equal =, but not vice versa. = Is used to compare whether two references are the same. If you want to only check whether the values are equal, you must use depth comparison to compare whether the attributes of an object are equal.
13.10 copy Module
Copy () provides a method to copy simple objects, while deepcopy () provides a method to copy objects containing other objects.
15 types and Methods
15.8 Operator Overloading
The operator overload method provided by python is that you can declare functions such as _ add _ () ,__ sub _ () ,__ Mul, when +-* is called, these operators call the _ x _ () operation on the class.
16 set objects