Hello world in a nutshell
Several forms of python code
Assembly language and compiled language and interpreted language
We can divide programming languages into three categories. One is assembly language. We know that computers can only recognize binary codes, so our programs must compile into binary codes before they can run. In order to make it easier for people to remember these binary codes, we use some symbols. To represent these binary codes, for example, use "ADD" to mean "1010100010001110", and "SUB" to mean "0011000001100110". This is what we call assembly language. Assembly language is unlikely to be encountered when we are programming, because low-level code engineers(Low-level does not mean that their engineers is naive in skills. Just the opposite. Because assembly language is very difficult to manage and requires a lot of programming skills.) have solved it for you. The other is a compiled programming language. Compiled programming languages such as C and C++ and Java work by first using a compiler to convert the code into assembly language and then run it. It is the main form of the relatively early programming languages. The last type is interpreted programming languages, such as our commonly used matlab or R or python. Interpreted programming languages use an interpreter to convert the code into a compiled programming language code, then use the compiler of a compiled programming language to compile the code. These codes are generally more powerful. One line of commands can do things that have to be done in dozens or even tens of thousands of lines in a compiled programming language, which is very convenient, but the efficiency will be reduced accordingly.
Process-oriented language and object-oriented language
Process-oriented programming
Process-oriented languages appeared earlier and simpler. For example, the following piece of code is a process-oriented language:
The result of the above program is:
Process-oriented language is executed from the beginning to the end. This is in line with human thinking habits. We need to follow the steps to do a thing. When we do all the steps, we finished a thing.
Process-oriented language is very effective for some simple things, such as making an automatic watering circuit with a single-chip microcomputer chip.
This circuit consists of a soil moisture sensor, a microcontroller and a relay (used to control the water pump).
When the soil moisture detector finds that the soil moisture is insufficient, the microcontroller will order the water pump to supply water. When the water is supplied, the soil moisture detector will continue to monitor the soil moisture. If the soil is already moist, stop the water pump and continue to detect whether the soil is moist. If the soil dries out again, continue to supply water. This continues to be tested again and again. We can complete the task of automatic watering.
The following code can complete this process.
Object-Oriented Programming
Object-oriented programming appeared later than process-oriented programming, because object-oriented programming can handle more complex tasks. Similarly, it becomes more difficult to convert these codes into binary assembly codes which can be read by machines.
Using the example above, we have an automatic watering system, but this time the automatic watering system is carried out in a botanical garden. Imagine a greenhouse with hundreds of plants in it, and every plant needs a Independent automatic watering system. If I use a supercomputer to control all the water pumps and soil moisture sensors in this greenhouse, how do we do it?
If our greenhouse contains 3 plants, then we need to write these much of the code, what if our greenhouse contains thousands of plants? Then our code will become extremely redundant.
To avoid this problem, we can introduce object-oriented programming. In other words, we program each automatic watering module as an object.
In this example, I encapsulated each automatic watering device into a class, and then called the function of creating the class, we successfully created 1000 automatic watering devices! Then we can make these automatic watering devices work.
And all of this we only used a few lines of code.
The true charm of object-oriented programming is not here. Let's change an example:
We have different professions in life, such as policeman, soldier, doctor, nurse, taxi driver, truck driver, chef, student, painter and so on.
As a human being, each person has many attributes, such as eating, sleeping, hobbies, height, weight, gender. At the same time, each of us has different attributes due to different occupations. For example, painters need to paint, cooks need to cook, the police need to catch the thief, the firefighters need to put out the fire, and the students need to take an exam. At this time, if you use process-oriented programming, it will become extremely cumbersome, while using object-oriented programming will become very simple.
We can make a template (class) called human, which contains different attributes such as height, weight, gender, and then create a template called painter, which contains the popularity, type of painting, the name of the exhibition that we participated in, etc., and the other template is called student. Including grade, educational background, whether you have participated in an internship, test scores, etc. If we need to create a 24-year-old male student learning computer, we only need to combine humans and students to assign these attributes to this object. If we need to build a 43-year-old firefighter, we can do the same. A class is a template, and we can apply this template to build a lot of things we need.
When we are creating a complex system (such as designing an Amazon website or designing a computer operating system), we need to figure out what design patterns we need. This is a difficult science. I will create one chapter and talks about it later.
In our lives, there are actually many object-oriented designs. For example, for a shopping website, we have thousands of users, and everyone has their own wish list, shopping cart, and payment method. Another example is the army. We have the navy, the army and the air force. For another example, our city has countless traffic lights. Some are designed for pedestrians, some are designed for bicycles, some are designed for motor vehicles, and some are designed for trains.
Object-oriented programming is so widespread that some languages are inherently designed for object-oriented programming, such as Java:
You will find that there must be classes to write java. If there are no classes, then you cannot write java programs. In other words, Java was born to write large programs and cooperate with multiple people, which is destined to not be suitable for all occasions.
Several ways of writing Hello World in Python
We have just talked about some knowledge about programming languages, so let's use this knowledge of programming languages.
Python has been a good language from the beginning of its design, because you can use python to write process-oriented programs or you can use python to write object-oriented programs. Python is also scriptable, so you can run python commands in real time. The flexibility of python is destined to be used in various occasions.
Interactive
For example, you can run Hello World in the python interpreter in real time:
Scripted
You can create a file called hello_world.py to run hello world, Type in:
The output result is:
Using Main function
There is also a main function similar to the C language in python, you can write it like this, which will make the code very concise and easy to understand:
The output result is:
Functional
You can create a function to print Hello World:
The output result is also:
Object-oriented
You can use the object-oriented method to print Hello World (it's overkill):
The output result is also:
After so many examples, do you feel the power of python?
Statistics
Start time of this page: December 17, 2021
Completion time of this page: December 17, 2021
Last updated