“Coding is an art and thus many different painters will use different strokes.”
=====================
目录:
一、基本概念
二、基础语法
三、面向对象编程
四、递归函数
五、build-in函数
六、正则表达式
七、数据分析相关modules
八、单元测试
九、编程工具
十、书籍和编程网站
=====================
一、基本概念
1、An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.
2、 Pseudocode is a notation that is more precise than English but generally not as precise as a programming language.
3、data structure:an organization of data for the purpose of making it easier to use.
4、A program is an algorithm expressed in a programming language.
5、docstrings: If the first thing after the function header is a string (some tools insist that it must be a triple-quoted string), it is called a docstring and gets special treatment in Python and in some of the programming tools.One way to retrieve this information is to use the interactive interpreter and enter the expression <function_name>.__doc__, which will retrieve the docstring for the function
6、Type annotations are an optional aspect of documenting functions. Still, type annotations are an important tool to increase the readability of your code, and you should use them in your programs. Although type annotations are ignored by the Python interpreter, there are tools such as mypy that can analyse your code containing type annotations and flag potential problems.
7、The Time Complexity is the amount of time taken by an algorithm to run, as a function of the length of the input.
8、Big O notation measures an upper bound on the asymptotic growth, i.e. n increases to +∞, often called order of growth. Big O is NOT a measure of time itself, It describes how time grows based on the size of the input( usually denoted as n).
a) O(1): constant time
b) O(n): linear time
c) O(n2): quadratic time
d) O(log n): logarithmic time
=====================
二、基础语法
1、keywords
2、order of precedence
https://docs.python.org/3/reference/expressions.html#operator-precedence
3、Tuple 用(),list用[], dictionary和set用{}
a)immutable data type: a data type which cannot be modified. Assignments to elements or slices of immutable types cause a runtime error.
b)mutable data type: a data type which can be modified. All mutable types are compound types. Lists and dictionaries are mutable data types; strings and tuples are not.
4、list comprehension :
[expression for item in iterable if condition] or
[<transformer_expression> for <loop_var> in <sequence> if <filtration_expression>]
Example:
numbers = [1, 2, 3, 4, 5]
even_numbers = [x for x in numbers if x % 2 == 0]
5、lambda expression. The syntax of a lambda expression is the word lambda followed by parameter names, separated by commas but not inside (parentheses), followed by a colon and then an expression. lambda expression: {lambda arguments: expression} yields a function object.
=====================
三、面向对象编程
1、object:A compound data type that is often used to model a thing or concept in the real world. It bundles together the data and the operations that are relevant for that kind of data. Instance and object are used interchangeably.
2、object-oriented programming:A powerful style of programming in which data and the operations that manipulate it are organized into classes and methods.
3、object-oriented language:A language that provides features, such as user-defined classes and inheritance, that facilitate object-oriented programming.
4、class:A user-defined compound type. A class can also be thought of as a template for the objects that are instances of it.
5、Object-oriented programming involves four key ideas: encapsulation, information hiding, inheritance, and polymorphism.
a)Encapsulation is the idea that a class can package some data together with the methods that manipulate the data. This is a powerful capability, and the chief idea that distinguishes OOP from structured programming.
b)Information Hiding promotes quality code by allowing objects to protect their state against direct manipulation by code using the object. Python, like many languages, provides mechanisms to achieve information hiding.
c)Inheritance and polymorphism are mechanisms that help to enable code reuse and contract-based programming.
6、instance:An object whose type is of some class. The words instance and object are used interchangeably.
7、instance variable: A variable that stores a value associated with the instance. The instance variables together store the state of an instance.
8、Class variables:Class variables are the variables which the values are shared among all instance of the same Class.
9、The __init__ method, often referred to as the constructor, is automatically called whenever a new instance of is created.
10、The __str__ method is responsible for returning a string representation as defined by the class creator.
11、Python has a module called abc that allows us to implement abstract classes
12、We use the decorator @abstractmethod to specify the abstract methods in Python.
=====================
四、递归函数
1、Recursion involves a function calling itself.Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller subproblems until you get to a small enough problem that it can be solved trivially.
a)A recursive algorithm must have a base case.
b)A recursive algorithm must change its state and move toward the base case.
c)A recursive algorithm must call itself, recursively.
2、base case:A branch of the conditional statement in a recursive function that does not give rise to further recursive calls.
3、divide-and-conquer paradigm:The technique of breaking down a problem into small manageable chunks of code is known as divide-and-conquer.
a)Divide the problem (instance) into sub-problems of sizes that are fractions of the original problem size
b)Conquer the sub-problems by solving them recursively
c) Combine sub-problem solution to produce the final solution for the original problem
4、divide-and-conquer strategy to build a recursive implementation for Merge sorting algorithm:
a)Problem: Sort the given list.
b)Sub-problem: Sort a sub-list with a length smaller than the original list.
c) Base case: The length of a sub-list is less than two elements (i.e. with only one element left), and it is considered sorted.
d) Divide: Identify the middle point of the list. This can be done by adding the indices of the first and last elements and dividing them by two (and rounding it off to the nearest integer).
e)Conquer: Sort the smaller sub-lists that have been created.
f) Combine: Merge the sorted sub-lists into one complete sorted list.
5、divide-and-conquer strategy to build a recursive implementation for quick sort algorithm:
a)Problem: Sort the given list.
b)Sub-problem: Sort the sublists with length smaller than the original list.
c)Base case: As like merge sort, the base case is the length of a sublist is less than two elements (i.e. with one element left in the sublist). Also, note that it is possible in Quick Sort to have a sublist of zero length (this is not possible in Merge Sort). A sublist with zero-length will occur when all the elements from the previous partitioning process are either greater than or less than the pivot value.
d)Divide: This occurs by selecting one element in the list, known as the 'pivot' element. Conveniently, the first element in the list is considered as the pivot. All the other elements are relocated in the list such that the elements to the left of the list are less than the pivot value and the elements to the right are greater than the value of the pivot. This process is known as 'partitioning'.
e)Conquer: The partitioning procedure is recursively performed on the smaller sublists by rearranging the elements of the smaller sublists based upon the pivot value chosen.
f)Combine: The combine step is generally so trivial that it does nothing. This is because by recursively sorting the elements based upon the pivot element, we would have sorted the entire list.
=====================
五、build-in函数
1、 build-in functions
https://docs.python.org/3/library/functions.html#sorted
2、sorted():builds a new sorted list from an iterable.
https://docs.python.org/3/howto/sorting.html#sortinghowto
3、 map() takes two arguments, a function and a sequence. The function is the mapper that transforms items. It is automatically applied to each item in the sequence. example:
"
numbers_list= [2, 5, 9]
numbers_4_times=list(map((lambda value: 4*value),numbers_list))
"
4、filter() takes two arguments, a function and a sequence. The function takes one item and returns True if the item should be included in the output. It is automatically called for each item in the sequence. You don’t have to initialize an accumulator or iterate with a for loop.
5、open(file, mode):Opening a file creates a file handle as a reference to the file to be dealt with.
=====================
六、正则表达式
1、 Basic Regular Expression Syntax
a)'.': matches any single character (letter, digit, everything except new line character)
b)'\': escapes special characters
c)'*': matches 0 or more repetitions of the preceding regular expression
d)'+': matches 1 or more repetitions of the preceding regular expression
e)'?': matches 0 or 1 repetition of the preceding regular expression
f){m}: matches m copies of the preceding regular expression
g){m, n}: matches from m to n copies of the preceding regular expression
h)[] : used to indicate a set of characters: e.g., [Cc]at will match ‘Cat’, or ‘cat’.
i)'|': A|B matches either A or B
j)'^', '$': match the start or end of the text string
k)[^]: used to indicate a set of characters NOT to match: e.g., [^Cc]at will match ‘hat’ and ‘@at’ but not ‘cat’ or ‘Cat’.
2、use the regex101 or Regexper websites to practice and test out your regex.
a)https://regex101.com/
b)https://regexper.com/
3、checking json file on a website
https://jsonformatter.org/
4、Re module document:
https://docs.python.org/3/library/re.html#regular-expression-syntax
=====================
七、数据分析相关modules
1、module:A file containing Python definitions and statements intended for use in other Python programs. The contents of a module are made available to the other program by using the import statement.
2、standard library:A collection of modules that are part of the normal installation of Python.
3、NumPy's documentation.
https://numpy.org/devdocs/user/index.html
4、pandas documentation.
https://pandas.pydata.org/docs/getting_started/index.html
https://pandas.pydata.org/Pandas_Cheat_Sheet.pdf
5、Matplotlib documentation.
https://matplotlib.org/stable/plot_types/index.html
6、SciPy documentation
SciPy.org (SciPy developer 2021), SciPy's tutorial (ScyPi Community, n.d) and the SciPy (ScyPi community n.d.)
7、To make a copy of a nested list that has more than two levels, then we recommend using the copy module. In the copy module there is a method called deepcopy.
=====================
八、单元测试
1、How to Avoid Debugging:understand the Problem, start small and keep improving It
2、Usually, test cases are written to cover the following three types of cases: valid cases ,invalid cases,boundary cases.
3、unittest — Unit testing framework
https://docs.python.org/3/library/unittest.html
example:
"
import unittest
class TestForSum(unittest.TestCase):
def test_sum(self):
self.assertEqual(1+2, 3)
if __name__ == '__main__':
unittest.main()
"
4、running the tests with unittest will return three possible outcomes:
a)OK: all tests passed successfully.
b)FAIL: there is an assertion error raised by the test.
c) ERROR: there is an error other than the assertion error.
5、There are two categories of exception to be dealt with in Python programs: built-in exceptions and user-defined exceptions.
6、Some of the most important types of error in Python are as following:
a)SyntaxError: This type of error occurs because of the error in the syntax of your program. For example, missing out a colon (:) after the if-statement is an example of the syntax error (also known as parse error).
b)NameError: This type of error occurs when you try to use a value before initialising it or you attempt to use a package without first importing it.
c)TypeError: This type of error occurs when you try to use incompatible data types within a single statement or passing arguments of the wrong type to a function. For example, adding a string and an integer is an example of type error; passing a list to a function that is expecting an integer argument is another example of type error.
d)ValueError: This type of error occurs when a function receives an argument with the correct data type but with an inappropriate value. For example, passing a negative number to the built-in function, math .sqrt(), will result in a value error.
e)Run-time errors:These errors occur while the program is executing and this implies certain bugs or issues that a developer had anticipated but was unable to do anything about it. We have mentioned some of the run-time errors earlier. Insufficient memory for running a program can also trigger a run-time error. Generally, an exception handler ensures that this type of error is caught and appropriately dealt with. Value errors (ValueError) and type errors (TypeError) in Python are the types of run-time errors. Note that name errors (NameError) in Python could be a run-time error if that was the result of a program attempting to use undefined variables, but a name error could also be a syntax error if a variable is accessed in some part of the program was indeed misspelled.
f)Logic errors: Logic errors are generally the consequences of incorrect implementation of the program's logic (i.e. what the program is supposed to do). Unlike syntax and run-time errors, a program with logic errors may run completely without crashing but it might not behave in the way that it is intended to perform. These errors can only be eliminated by running suitable test cases with known outputs.
(g)semantic error, also called a logic error. If there is a semantic error in your program, it will run successfully in the sense that the computer will not generate any error messages.
=====================
九、编程工具
1、VSS code:通用的代码编辑器
https://code.visualstudio.com/Download
https://code.visualstudio.com/docs/python/environments
a)Install the Python extension for VS Code from the Visual Studio Marketplace.
b)select a Python 3 interpreter by opening the Command Palette (Control+Shift+P), start typing the Python: Select Interpreter command to search, then select the command.
2、Anaconda:用于数据科学和机器学习的大型Python发行版,包含了大量的科学计算包和工具。
https://www.anaconda.com/download/success
3、调试工具,Visual Debugger, and AI Tutor for Python, Java, C, C++, and JavaScript。
https://pythontutor.com/
4、python解释器:如果安装了Anaconda,不需要单独下载安装。
https://www.python.org/downloads/
5、Git: (2.44.0) 64-bit version of Git for Windows,安装后不用打开GUI和shell,在visual studio code中调用。
https://git-scm.com/download/win
=====================
十、书籍和编程网站
1、Foundations of Python Programming
https://runestone.academy/ns/books/published/fopp/index.html
2、Problem Solving with Algorithms and Data Structures using Python
https://runestone.academy/ns/books/published/pythonds/index.html
3、Data Structures Tutorial
https://www.geeksforgeeks.org/data-structures/
4、Python 3.12.3 documentation
https://docs.python.org/3/
https://docs.python.org/3.10/tutorial/index.html
5、Online code challenge
https://projecteuler.net/
https://www.codechef.com/
https://www.codewars.com/