Python language learning _2 basic grammar

In the last Python course we mentioned three basic types. Do you remember which ones? Give everyone three seconds to think about.....1...2...3, yes they are sequence, selection and loop, then in this chapter we will talk about how to use Python implements the above three situations.

Before entering the study formally, let’s introduce a simple function called: print() , this is an output function, the main function is to display the content in parentheses on the console, for example, I output p rint("Hello Python!" ) , Hello Python will be displayed on the console !

To make it easier for everyone to distinguish, all the following codes will be bolded, and the output results will be bolded and  italicized .

enter the theme:

The first is sequential execution. The sequential execution is very simple. In our previous article, we said that Python distinguishes each different execution level by indentation, so if you want to execute sequentially, you only need to align all the statements, just say it. If you don't understand, let us give an example.

①>>>print(“Hello -”)

②>>>print("World!")

③>>> print("Slightly omitted")

The execution status of the above three lines of code is: first execute code ①, and then find that the second line of code is the same as the beginning of the first line, so execute ② and ③.

So the output result is:

>>>Hello -

>>>World!

>>> Slightly

Here is a unique feature of Python: all programming languages ​​in the world will have output functions, although the names may be different, such as " printf() " in C language and System.out.print() in Java language. , Almost most languages ​​have a feature that when you don't mark the output function, the output content will not wrap. Let me give an example in C language:

【chestnut】----------------------------------------------- ------------------

>>> printf("Hello");

>>>printf("世界");

The execution effect at this time is:

>>> Hello World

【chestnut】----------------------------------------------- ------------------

Therefore, you can see a feature of the C language. Although the printf function wraps, the output content does not wrap. The effect of Java is also like this (but Java also has an output function that can perform line wrap, which is very similar to the one that does not wrap. Make more introductions.

So it can be seen that the characteristic of the Python language is that the output function wraps and the output content will wrap. This often causes a lot of inconvenience, but it is not without a solution, that is, ending=""

【chestnut】----------------------------------------------- ------------------

>>>print("Hello ",ending="")

>>>print("World!")

【chestnut】----------------------------------------------- ------------------

The output effect is: Hello World! In the same way, if you add special content, such as alphanumeric or symbols, in the double quotes in ending="" , it will also output the same, you can try it.

ok The above is the sequential execution. Next, let’s look at the selection execution. There are many ways to choose. Let’s first introduce the simplest one, if...else...

【chestnut】----------------------------------------------- ------------------

>>>a == 1

>>>if  a=1:

>>>       print("a是1");

>>>else:

>>>       print("a is not 1");

Here we first look at the indentation. First, the first, second, and fourth lines are aligned, and the third and fifth lines are not aligned, so the execution result should be:

① Assign a value of 1 to variable a

②Judge whether a is equal to 1, if it is equal to 1, then perform ③, if it is not equal to ①, skip to else, and perform ⑤.

So the execution result here should be:

>>>a是1

The above is a simple choice. Of course, we still have some situations in our lives that are not one or two, so we add a thing between if and else, called elif, and the operation is as follows:

>>>if a==1:

>>>      print("a是1")

>>>elif a == 2 :

>>>       print("a是2")

>>>else:

>>>         print("a is neither 1 nor 2")

Here we first introduce a simple choice of if else, and some more complicated ones. After we fully understand if else, we will introduce it. Next, we will introduce a kind of loop structure, which is when the boy confessed to the goddess. , The goddess asked him to knock 1,000 "I love you" stories, let's continue to see:

I will introduce one of my most commonly used loop methods. I will talk about the others later. I would like to emphasize that this chapter mainly requires you to understand some basic syntax and concepts, and you don’t need to learn more; there are many ways to implement loops in python. Here we mainly introduce the for method

>>>for i in range(100):

>>>      print("I love you")

This is what the guy who likes the goddess should do. I have to say that the topic of the goddess is really simple but he is not sure. Let us analyze these two lines of code. First look at the indentation, where print is after for , So for is one level higher than print. The order of implementation here should be to execute for first. If for meets the conditions, execute print, and exit the program if it does not (or if there is code below or continue to execute downwards).

So the execution of this line of code is:

>>>I love you

>>>I love you

>>>I love you

.

.

>>>I love you

A total of one hundred executions.

The number in the range refers to the number of executions, and i will gradually increase from 0 until the number of executions is reached, it will jump out of the loop and the loop will end.

It's not early, I'll stop here today, see you tomorrow! !

 

 

 

 

Guess you like

Origin blog.csdn.net/Hsk_03/article/details/106165705
Recommended