[Python entry] 1-basic concepts of programming

------Python introductory teaching:

 

I spent the summer vacation at my grandfather’s house. I didn’t have internet or computer, so I took the iPad and read a Python introductory book. Then I fell in love with it. I wrote this series to expand myself and stay safe. Knowledge (by the way, one of my sisters is learning this recently, so it's pure entertainment), laymen can have some fun through this, students can watch, and the great god can help correct.

Start of text:

All programming languages ​​are composed of the following three simple modes: sequential execution, selection, and looping. If there is a fourth one, it is nesting.

Briefly explain these three situations:

----Sequential execution: open the refrigerator, stuff the elephant in, close the refrigerator door; this is a sequential execution

----Choose execution: Should you open the refrigerator door first or pick up the elephant first? So there are the following two situations:

      --①Open the refrigerator door, pick up the elephant, stuff the elephant in, close the refrigerator door

      --② Pick up the elephant, open the refrigerator door, stuff the elephant in, close the refrigerator door

           Note that when you "open the door" or "pick up the elephant", you are faced with two choices, that is to say, there can be two situations at this time.

----Circulation: Your mother asked you to put 100 elephants into the refrigerator, so you have to do this

      --Open the refrigerator door, tuck the first elephant in, put the second... tuck the hundredth elephant in, close the refrigerator door

           At this time, instead of the simple process of opening, putting in, and closing the door, you have been performing this process, which is a cycle.

Nesting: wait a minute, don't worry, first understand the most basics.

The above scenario can be established if you replace it with any situation in your life. For example, when you go to school, you open the door, get in the car, go to school, play for a day, and go home after school; this is a sequence, and you can think about other scenarios.

When the great language artists were creating languages, they also thought of the refrigerator and elephant problems, so they began to think, how to express it? So there are a variety of representation methods, each language may be different, but the same effect, the next is a brief introduction to Python representation methods.

 

Before that, let me say: Python is a magical language. Why is it magical? In the competition of various languages, the survival of the fittest is like a soldier fighting, and Python is armed to the teeth, so it is not easy to understand. I will give you an example in Chinese:

------------------------------------------------------------

Today i go

    Akari  

Go home 

  I know Xiaohong

      So we are together

Play mud today super

   Happy class

--------------------------------------------------------------

The above is a paragraph, saying that I went to Xiaoming’s house to play Xiaohong and we played in the mud together, but did you feel tired when I said that? Also very unsightly? So our teacher asked us to be more beautiful when typing the code, so we should say this sentence (denote the following example as ②, and the above paragraph as ①):

--------------------------------------------------------------

Today I went to Xiaoming's house to play

Who knew Xiaohong was there

    So we play mud together

Super happy today

--------------------------------------------------------------

In this way, it’s very clear, right? This is the beauty of the programming language. If you want to rely on programming for a good income in the future, it is best to develop a good coding style. Of course, this is only for other languages. , "Alignment" and "Indentation" are just for aesthetics, but you have to learn Python is different, if you do not align, you may have no food in the future, how about it, think about it, do you think that Python is used by others? Is the unused thing really "armed" to the teeth?

Okay, then let's introduce sequential execution and selective execution, or use natural language as an example (the next small Python class will start to introduce the source code, don't worry)

---------------------------------------------

I open the refrigerator door

I put the elephant in

I closed the refrigerator door

----------------------------------------------

The above is a sequential execution. Next, let's look at an alternative execution to find the difference:

-------------------------------------

If (I want to open the door first):

------Open the refrigerator door

------ Pick up the elephant

otherwise:      

------ Pick up the elephant  

------Open the refrigerator door

Put the elephant in the refrigerator

Close the refrigerator door

----------------------------------------

Here we must first judge whether to open the door first or pick up the elephant first, and then if the door is opened first, open the door, then the elephant, otherwise open the door after picking the elephant. At this time, pay attention to the following "put the elephant" "Enter the refrigerator" and "Close the door" are equal to "If" and "Otherwise", so at this time, if or otherwise, the elephant must be put in and the refrigerator door is closed.

 

Let me explain here, because Python uses strict indentation to control the end of each statement and the nesting relationship between statements, so here if-otherwise-zoom in-close the door is level , That is to say, after the last level statement is executed, the next level statement must be executed (but here "if && otherwise" is the relationship between two options, so only one of the two level statements is executed), and the door is opened , Take the elephant is included in the if and otherwise, so "open the door to take the elephant" belongs to the if and otherwise, which means that at this time, in addition to facing the choice, it will be after the execution of the result of the choice is completed. Continue to execute the level-level statement down; and to control whether the statement is level-level is to see whether the two statements are aligned before. Because of the indentation that we often use in writing Python programs, a space bar is not obvious. The space bar is troublesome, so we use tab (which is next to the letter'Q') instead of spaces. One tab is about four spaces.

Well, the last is the loop statement, which is very simple, let's look at the example directly:

-----------------------------------------

①I opened the door to see if there were any elephants on the ground:

②-----Yes:

③----------- Put an elephant in the refrigerator;

④-----------If there are elephants, return to the second step;

⑤-----No:

⑥-----------Close the door;

-------------------------------------------

In this example, you are a big elephant breeder. You want to put all a hundred elephants in the refrigerator. You have to see if there are any more elephants each time you put one. If there is one, continue to put it. After playing it a hundred times, I finally found that there were no elephants. Close the door!

Here we come to understand this small program:

"I open the door and see if there is an elephant on the ground" there is no parallel sentence

"Yes" and "No" level, this is a choice statement (a fork in the program execution)

"Put an elephant in" & "return to the first step if there is an elephant" is a level sentence (although "close the door" is also the option "no", so it is not considered together), At this time, you will execute in the following order:

①----②-③-④---②-③-④---②-③-④---②-③-④…………-②-③-④---⑤-⑥ -End

In the execution process above, steps ②③④ were executed one hundred times in this order, so we use loop statements to solve them.

For example, in a Python statement, if you want the three words "I Love You" to appear on the program after running, you need to call a statement: print("I Love You")

Where print represents the output (you can understand it this way), let us assume a situation:

You are a university hard* single code farmer. One day your long-standing goddess sent a circle of friends and said: "Whoever can type a hundred "I love you" on the computer as soon as possible, I will fall in love with whomever" At this time, when you are not studying well and you are still typing 100 print ("I love you") sentences into the computer, the second generation Gao Fushuai of the next class knows the following input method because of studying hard:

for i in range(100): ------- for looping 100 times

       print("I love you") ------- Used to output "I love you"

Of course, it is possible for a single person to defeat the second-generation official with super fast and flexible fingers, so what if the goddess sends 10 million times 1,000 times? The official second generation only needs to change the number, but you have to cramp.

Obviously, the original designer of Python obviously considered this problem, and he couldn't bear the power and wealth (he is a great man, and I hope you don't make a joke, here is just to make everyone remember more clearly) invented the loop statement!

 

Okay, that’s it for this time. In the next Python course, I will start to come into contact with some simple source code and other different statements but with the same or similar functions (for example, there are about three to four statements that can implement loops) .

 

Bye has a Bye~~~~

Guess you like

Origin blog.csdn.net/Hsk_03/article/details/106046043