# It's a question from Python if

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

``````a = input()
b = input()
cnt = 0
n = 0
if a[n:n + len(b)] == b:
``````

It's a grammar question, but I don't know the meaning of the last if question Did you slice a?

python if문

2022-09-20 10:42

There are sometimes people who complain of hardships like you.

I don't understand the meaning of the code.

This would not be an appeal to the code itself. I'm not saying that I'm not sure about the function of the code or the behavior of the code or the possible. Then what is it? Maybe this means that you don't know the relationship between the place where the code is placed and the code.

So let's look at the periphery of the code. If you can reveal the relationship, you can easily postpone the meaning of the code (in difficult words, meaning, purpose).

What we can observe is that `a`, `b`, `cnt` and `n` are determined, and then `a[n:n + len(b)] == b` appears ("I don't know what it means"). I don't know anything more than that. This is a very high-context additional piece of information that says, "Grammar Problem," and the comment that says, "Did you slice a?" unfortunately, nothing tells us about their relationship.

Only the ones that the code in question refers to will be able to uncover the relationship. If you read the code again, `a` was referenced, `n` was referenced, and `b` was referenced. All three of them were set before this code. `a = input()`, `b = input()`, and `n = 0`. And `cnt` didn't appear in the code, so I think it's okay to ignore it at this point.

But let's think about `a` and `b` and the surroundings of each of them. Both are `input()`. If you think about it like this, wouldn't `b` be unnecessary? However, `b` was declared, values were assigned, referenced in other codes, and even referred to something clearly different from `a`. The code `b = input()` has a very special relationship with the code around it. So here we can assume that the code `b = input()` has some meaning.

If you think so, the same goes for `a`. If you omit the middle a little bit, you can think of `a` as having contextual significance as 'first input' and `b` as 'second input'. So far, is there a logical leap, an irreversible inference, something like, "I really can't think of anything like that?"

Now, then we can rewrite the initial code like this.

``````# Still, the original script had an excessive indented if, so the indentation was corrected here.
if first input [0:0 + length (second input)] == second input:
``````

I think I can understand the meaning of this code from here. In Python, you can "slice" a portion of a string with the `string [x:y]` syntax when the string is given. If so, the left-hand side of the if in this code is to get some of the first input string, from the letter of index zero to the letter of index zero plus the length of the second input string. And on the right, there's nothing more to explain.

And from now on, it's purely my gut feeling, but it's like trying to figure out if the first input string is a string that starts with the second input string. Is this reasoning valid? What made you think so? If the above interpretation is correct, what and what is that if comparing with? What significance or purpose does it have? Why did you think so?

In fact, what we've done so far is what we call static analysis". Although it's a very primitive form. A very complex codebase is a daunting task to leave to your computer through good IDE plug-ins, but a simple script can also do it. You can do it, too. In the world of programming, every problem is in code and the cause is in code. So if you read the damn code, look around the code, take notes in your notes if you need to, think and reason enough, the rest is just a matter of time.

So now do it yourself.

PS. When you post Python questions from now on, please don't write "This is a Python question." "The song started" "The song is over." It's kind of like that.

2022-09-20 10:42

## If you have any answers or tips

Popular Tags
python x 4520
android x 1589
java x 1455
javascript x 1363
c x 881
php x 682
html x 629
swift x 586
ios x 538
mysql x 397