I draw an analogy between human language and Python language. When human learn how to speak, they start with the vocabulary and syntax to make up sentences; while in Python, we start to learn the keywords and syntax to make up statements.Human use sentences to make story, but to be a good storyteller, they should use a mixed approach of organizing sentences in sequence, by conditions and with repetitions. The same approach applies to write program that we can implement the statements in sequence, by conditions and with repetitions.
For any programming languages, we should touch the Data Structure first. In Python, thereare two generic types, namely, element-type and container-type. In the former, we have integer, floating point, Boolean and NoneType, and they store a value, while in the latter, we have string, tuple, list, dictionary and set, and they store a collection of values in various formats. As you can see, round, square, and curly bracket shave distinct meanings when it comes to the container-type data. Beyond that, there is a bunch of Collections designed for specialized purposes.
So far everything we have seen has only consisted of sequential execution, but the world is more complicated than that. Control Flow is where the rubber really meets the road in programming. Without it, a program is simply a list of statements that can only be sequentially executed. With it, you can execute certain code blocks conditionally and/or repeatedly. In a real world, we need “if statement” to perform decision-making that allows for conditional execution of a group of statements based on the value of an expression, and we need “for loop”and “while loop” to perform definiteand indefinite iterations.
When the program is “out of control”, weneed to identify the errors first and handle the exceptions, and hopefully regain the control.
So far everything we have been using are simple and single-use code blocks. To reuse the codes, we need to consider Functions. A function should do one thing only and do it really well. We have two ways to create functions, using def keyword for normal functions, and lambda keyword for anonymous functions. Functions are first-class citizens in Python. You can assign them to variables, store them in data structures, pass them as arguments to other functions, and even return them as values from other functions.
So far we have designed program around functions or blocks of statements which manipulate data. This is called the procedure programming. There is another way of organizing your program which is to combine data and functionality and wrap it inside something called an object. This is called the Object-Oriented Programming, or OOP for short.
Classes and objects are thetwo main aspects of OOP. A class creates a new type where objects are instancesof the class.
Objects can store data using ordinary variables that belong to the class, called as fields. Objects can also have functionality by using functions that belong the class, called methods.
This terminology is important because it helps us to differentiate between functions and variables which are independent and those which belong to a class or object. Collectively, the fields and methods are referred to as the attributes of that class. Four of the benefits of OOP are encapsulation, inheritance, polymorphism and composition.
Till now, all essential topics have been covered. Here are some advanced topics.
String Formatting are to beautify output string given a pattern, Regular Expressions are used to match and extract string given a pattern, and all we need to learn is how to represent the pattern.
Comprehensions are just for loops and if statements over a collection but expressed in a more compactsyntax.
Iterator is a stateful object that will produce the next value when called. Iterable is a stateless object that can be looped with.Iterators are always iterables, but iterables are not always iterators.
Generator is iterator. Two ways of creating generator are generator functions and generator expressions. Normal functions use return statement, while generator functions useyield statement.
List comprehensions use square brackets, while generator expressions use round brackets. A list can be iterated multiple times, a generator is single use. A list is a collection of values, whilea generator is a recipe for producing values.
Decorator wraps a function,modifying its behavior without modifying itself. It is a higher order function that takes function as argument and return another function.