how does list x operates if(x % i) == 0
so i'm learning coding for a few weeks now and just got over something that i really cant explain by my self.
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']:
for i in range(2, 7):
letters = len(word)
if (letters % i) == 0:
print(i, word)
when we define variable letter = len(word) this literally means letter = 5 right? If it do so and we go over to the if condition it says: (letter % i) == 0 for me what this means is when we start the second for-loop (5 % 2) == 0 (1) == 0 I know thats actually not right cause word in that case is a variable and not a list. But if i set letter so the position of each word like ox= len(0) that doesn't makes sense to me either thats how i am thinking about this. unfortunately shell is has given me this
2 ox
3 cat
2 lion
4 lion
5 tiger
2 bobcat
3 bobcat
6 bobcat
I really can't figure out how this loop works but i really want to understand. I'ver tried several things but nothing worked in a sense of logic.
I hope you can help a beginner out :) P.S. sorry for my bad english haha
python
add a comment |
so i'm learning coding for a few weeks now and just got over something that i really cant explain by my self.
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']:
for i in range(2, 7):
letters = len(word)
if (letters % i) == 0:
print(i, word)
when we define variable letter = len(word) this literally means letter = 5 right? If it do so and we go over to the if condition it says: (letter % i) == 0 for me what this means is when we start the second for-loop (5 % 2) == 0 (1) == 0 I know thats actually not right cause word in that case is a variable and not a list. But if i set letter so the position of each word like ox= len(0) that doesn't makes sense to me either thats how i am thinking about this. unfortunately shell is has given me this
2 ox
3 cat
2 lion
4 lion
5 tiger
2 bobcat
3 bobcat
6 bobcat
I really can't figure out how this loop works but i really want to understand. I'ver tried several things but nothing worked in a sense of logic.
I hope you can help a beginner out :) P.S. sorry for my bad english haha
python
Indentations matter in Python. please fix
– Vineeth Sai
Nov 22 '18 at 6:04
%is the modulo operator. Just search for 'python modulo operator'
– Red Cricket
Nov 22 '18 at 6:08
@RedCricket He seems to understand that, he knows that(5 % 2)is1.
– Barmar
Nov 22 '18 at 6:10
1
I don't understand the question.wordis a variable whose value is a string. Solen(word)is the length of the string.
– Barmar
Nov 22 '18 at 6:11
Tryfor word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']: print(word)and you'll see whatwordis (hint: it's not the list).
– Matthias
Nov 22 '18 at 9:12
add a comment |
so i'm learning coding for a few weeks now and just got over something that i really cant explain by my self.
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']:
for i in range(2, 7):
letters = len(word)
if (letters % i) == 0:
print(i, word)
when we define variable letter = len(word) this literally means letter = 5 right? If it do so and we go over to the if condition it says: (letter % i) == 0 for me what this means is when we start the second for-loop (5 % 2) == 0 (1) == 0 I know thats actually not right cause word in that case is a variable and not a list. But if i set letter so the position of each word like ox= len(0) that doesn't makes sense to me either thats how i am thinking about this. unfortunately shell is has given me this
2 ox
3 cat
2 lion
4 lion
5 tiger
2 bobcat
3 bobcat
6 bobcat
I really can't figure out how this loop works but i really want to understand. I'ver tried several things but nothing worked in a sense of logic.
I hope you can help a beginner out :) P.S. sorry for my bad english haha
python
so i'm learning coding for a few weeks now and just got over something that i really cant explain by my self.
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']:
for i in range(2, 7):
letters = len(word)
if (letters % i) == 0:
print(i, word)
when we define variable letter = len(word) this literally means letter = 5 right? If it do so and we go over to the if condition it says: (letter % i) == 0 for me what this means is when we start the second for-loop (5 % 2) == 0 (1) == 0 I know thats actually not right cause word in that case is a variable and not a list. But if i set letter so the position of each word like ox= len(0) that doesn't makes sense to me either thats how i am thinking about this. unfortunately shell is has given me this
2 ox
3 cat
2 lion
4 lion
5 tiger
2 bobcat
3 bobcat
6 bobcat
I really can't figure out how this loop works but i really want to understand. I'ver tried several things but nothing worked in a sense of logic.
I hope you can help a beginner out :) P.S. sorry for my bad english haha
python
python
edited Nov 22 '18 at 6:08
Barmar
434k36260363
434k36260363
asked Nov 22 '18 at 6:03
SiddharthaSiddhartha
1
1
Indentations matter in Python. please fix
– Vineeth Sai
Nov 22 '18 at 6:04
%is the modulo operator. Just search for 'python modulo operator'
– Red Cricket
Nov 22 '18 at 6:08
@RedCricket He seems to understand that, he knows that(5 % 2)is1.
– Barmar
Nov 22 '18 at 6:10
1
I don't understand the question.wordis a variable whose value is a string. Solen(word)is the length of the string.
– Barmar
Nov 22 '18 at 6:11
Tryfor word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']: print(word)and you'll see whatwordis (hint: it's not the list).
– Matthias
Nov 22 '18 at 9:12
add a comment |
Indentations matter in Python. please fix
– Vineeth Sai
Nov 22 '18 at 6:04
%is the modulo operator. Just search for 'python modulo operator'
– Red Cricket
Nov 22 '18 at 6:08
@RedCricket He seems to understand that, he knows that(5 % 2)is1.
– Barmar
Nov 22 '18 at 6:10
1
I don't understand the question.wordis a variable whose value is a string. Solen(word)is the length of the string.
– Barmar
Nov 22 '18 at 6:11
Tryfor word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']: print(word)and you'll see whatwordis (hint: it's not the list).
– Matthias
Nov 22 '18 at 9:12
Indentations matter in Python. please fix
– Vineeth Sai
Nov 22 '18 at 6:04
Indentations matter in Python. please fix
– Vineeth Sai
Nov 22 '18 at 6:04
% is the modulo operator. Just search for 'python modulo operator'– Red Cricket
Nov 22 '18 at 6:08
% is the modulo operator. Just search for 'python modulo operator'– Red Cricket
Nov 22 '18 at 6:08
@RedCricket He seems to understand that, he knows that
(5 % 2) is 1.– Barmar
Nov 22 '18 at 6:10
@RedCricket He seems to understand that, he knows that
(5 % 2) is 1.– Barmar
Nov 22 '18 at 6:10
1
1
I don't understand the question.
word is a variable whose value is a string. So len(word) is the length of the string.– Barmar
Nov 22 '18 at 6:11
I don't understand the question.
word is a variable whose value is a string. So len(word) is the length of the string.– Barmar
Nov 22 '18 at 6:11
Try
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']: print(word) and you'll see what word is (hint: it's not the list).– Matthias
Nov 22 '18 at 9:12
Try
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']: print(word) and you'll see what word is (hint: it's not the list).– Matthias
Nov 22 '18 at 9:12
add a comment |
1 Answer
1
active
oldest
votes
To start, len(word) is not necessarily 5, it's the length of the current word. So, first it is 2, then 3, then 4, then 5, then 6. You've made it so the program only prints when (letters % i) == 0. For the words of length 2, that only happens (in your range of 2 to 7) when i == 2. For words of length 3, this happens when i == 3. However, for words of length 4, this happens when i == 2 and also i == 4. You can see this by looking at the values of i printed out.
Does that make sense? If you'd like me to clarify anything, just leave a comment :)
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424759%2fhow-does-list-x-operates-ifx-i-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To start, len(word) is not necessarily 5, it's the length of the current word. So, first it is 2, then 3, then 4, then 5, then 6. You've made it so the program only prints when (letters % i) == 0. For the words of length 2, that only happens (in your range of 2 to 7) when i == 2. For words of length 3, this happens when i == 3. However, for words of length 4, this happens when i == 2 and also i == 4. You can see this by looking at the values of i printed out.
Does that make sense? If you'd like me to clarify anything, just leave a comment :)
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
add a comment |
To start, len(word) is not necessarily 5, it's the length of the current word. So, first it is 2, then 3, then 4, then 5, then 6. You've made it so the program only prints when (letters % i) == 0. For the words of length 2, that only happens (in your range of 2 to 7) when i == 2. For words of length 3, this happens when i == 3. However, for words of length 4, this happens when i == 2 and also i == 4. You can see this by looking at the values of i printed out.
Does that make sense? If you'd like me to clarify anything, just leave a comment :)
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
add a comment |
To start, len(word) is not necessarily 5, it's the length of the current word. So, first it is 2, then 3, then 4, then 5, then 6. You've made it so the program only prints when (letters % i) == 0. For the words of length 2, that only happens (in your range of 2 to 7) when i == 2. For words of length 3, this happens when i == 3. However, for words of length 4, this happens when i == 2 and also i == 4. You can see this by looking at the values of i printed out.
Does that make sense? If you'd like me to clarify anything, just leave a comment :)
To start, len(word) is not necessarily 5, it's the length of the current word. So, first it is 2, then 3, then 4, then 5, then 6. You've made it so the program only prints when (letters % i) == 0. For the words of length 2, that only happens (in your range of 2 to 7) when i == 2. For words of length 3, this happens when i == 3. However, for words of length 4, this happens when i == 2 and also i == 4. You can see this by looking at the values of i printed out.
Does that make sense? If you'd like me to clarify anything, just leave a comment :)
answered Nov 22 '18 at 6:11
Unsolved CypherUnsolved Cypher
510316
510316
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
add a comment |
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
Alright, thanks. Really needed that clarification. Thank you :) 🤙
– Siddhartha
Nov 22 '18 at 12:47
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
No problem. Welcome to Stack Overflow! If this resolved your question, you can mark the answer as accepted if you like :)
– Unsolved Cypher
Nov 23 '18 at 4:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424759%2fhow-does-list-x-operates-ifx-i-0%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Indentations matter in Python. please fix
– Vineeth Sai
Nov 22 '18 at 6:04
%is the modulo operator. Just search for 'python modulo operator'– Red Cricket
Nov 22 '18 at 6:08
@RedCricket He seems to understand that, he knows that
(5 % 2)is1.– Barmar
Nov 22 '18 at 6:10
1
I don't understand the question.
wordis a variable whose value is a string. Solen(word)is the length of the string.– Barmar
Nov 22 '18 at 6:11
Try
for word in ['ox', 'cat', 'lion', 'tiger', 'bobcat']: print(word)and you'll see whatwordis (hint: it's not the list).– Matthias
Nov 22 '18 at 9:12