Second bonus of the weekend!

Hee hee, the second bonus for the weekend.

Okay, let's start with a joke today: a long, long time ago, the princess fell in love with a programmer A, but the king wanted to betroth the princess to another nobleman, and set the wedding date 100 days later. The princess and A agreed to run away together on the wedding day. Then, A is very nervous, so he counts the day today. However, when A counted to ninety-nine, the princess was married. Do you think the princess deceived A, of course, no! So whose fault is this? A! Because A starts counting from zero when counting the number of days, when he counts to ninety-nine, um, yes, the princess is married.

What we can learn from this story: In programming languages, the serial number of the first number is 0. In any case, as long as the computer starts counting from the beginning, it is 0, 1, 2...  

 

Ok, today's popular science is here, let's talk about today's content:

index:

When you are reading a book, is there an index in the first few pages of the table of contents, telling you which page the corresponding content is on? Yes, in computer language, there is also a corresponding index, the function is similar to that of a book. Let's first look at a piece of code:

greeting = 'Hello'
greeting[0]

In this code, greeting is the variable name used to store the string hello. In other words, greeting is equal to hello, you can understand it this way. Then greeting[0] means to take the first word in the string. That is, H. In the same way, if you want to take the second number, replace 0 with 1, and the third is 2. Is not it simple? Have you ever wondered what if the number inside the parentheses is negative? For example like this:

greeting[-1]

That's right, count in the opposite direction at this time. -1 means the first word in the opposite direction, which is o. In the same way, -2 is the second word l.

Then we have other index representation methods, let's write the code first:

fourth = input()[3 ]
 #Press Enter after input, then enter a few words at will, such as 2018, then press Enter 
# and then enter fourth, press Enter 
fourth #The
 number 8 will appear in the run box

 

We mentioned earlier that input() is used to indicate input, that is, to allow you to enter the 2018, and then the 3 following the input indicates the fourth word in the string you entered, yes fourth is used for Store this fourth number, and then when you enter fourth, the number stored in it will appear in the run box, just like the 8.

Today's content will be discussed here first, it may be a bit complicated, please understand it well.

 

(If the pictures in the article are infringing, please contact the author to delete them)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325399840&siteId=291194637