What does sum(1 for c in sentence if c.isupper())) mean in non programming terms
I need to count the amount of upper-case letters in a user entered sentence.
When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper()))
.
I used it and it works but I also need to explain the code to my teacher.
How would I go about doing this?
python string python-3.x command
add a comment |
I need to count the amount of upper-case letters in a user entered sentence.
When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper()))
.
I used it and it works but I also need to explain the code to my teacher.
How would I go about doing this?
python string python-3.x command
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 at 14:04
You could also usesum(c.isupper() for c in sentence)
. This works becauseTrue
has a numeric value of 1 andFalse
has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
– Patrick Haugh
Nov 19 '18 at 14:18
1
So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 '18 at 14:20
add a comment |
I need to count the amount of upper-case letters in a user entered sentence.
When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper()))
.
I used it and it works but I also need to explain the code to my teacher.
How would I go about doing this?
python string python-3.x command
I need to count the amount of upper-case letters in a user entered sentence.
When I search Google for a solution, I came across the command sum(1 for c in sentence if c.isupper()))
.
I used it and it works but I also need to explain the code to my teacher.
How would I go about doing this?
python string python-3.x command
python string python-3.x command
edited Nov 19 '18 at 14:15
J100
951413
951413
asked Nov 19 '18 at 13:54
DavidP75
61
61
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 at 14:04
You could also usesum(c.isupper() for c in sentence)
. This works becauseTrue
has a numeric value of 1 andFalse
has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
– Patrick Haugh
Nov 19 '18 at 14:18
1
So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 '18 at 14:20
add a comment |
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 at 14:04
You could also usesum(c.isupper() for c in sentence)
. This works becauseTrue
has a numeric value of 1 andFalse
has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.
– Patrick Haugh
Nov 19 '18 at 14:18
1
So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 '18 at 14:20
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 at 14:04
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 at 14:04
You could also use
sum(c.isupper() for c in sentence)
. This works because True
has a numeric value of 1 and False
has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.– Patrick Haugh
Nov 19 '18 at 14:18
You could also use
sum(c.isupper() for c in sentence)
. This works because True
has a numeric value of 1 and False
has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.– Patrick Haugh
Nov 19 '18 at 14:18
1
1
So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 '18 at 14:20
So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 '18 at 14:20
add a comment |
5 Answers
5
active
oldest
votes
So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").
Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 '18 at 14:09
add a comment |
Try rewriting in this way, instead of using list inclusions:
count =0
for c in sentence:
if c.isupper():
count+=1
print(count)
That way, there won't be much to explain!
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, andprint
needs parentheses.
– RoadRunner
Nov 19 '18 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
add a comment |
Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.
sentence = "Hi My Name is"
a = 0
for c in sentence:
if c.isupper():
a = a + 1
else:
pass
print(a, " capital letters")
b = sum(1 for c in sentence if c.isupper())
print(b, " capital letters")
~
add a comment |
Try to decomposite "sum(1 for c in sentence if c.isupper())":
1) 1 for c in sentence if c.isupper()
It means: "generate 1 if symbol if c is uppercase".
Also, note that (1 for c in sentence if c.isupper()) is a generator
But because it placed as a function positional argument it can be placed there without parentheses.
2) sum(...) -- summarize all "1" generated from step 1)
sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol
add a comment |
sum(1 for i in iterable )
will add 1 for each member of iterable. look at this :
sum(1 for i in [True,True,True])
will return 3
sum(10 for i in [True,True,True])
will return 30
so for each iteration sum will add the number to itself.
the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.
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%2f53376146%2fwhat-does-sum1-for-c-in-sentence-if-c-isupper-mean-in-non-programming-terms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").
Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 '18 at 14:09
add a comment |
So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").
Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 '18 at 14:09
add a comment |
So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").
Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.
So for each "c in sentence" (i.e., step through each letter in sentence, making each equal to c for the duration of the loop), substitute in a 1, ("1 for c in sentence"), but only if "c" (the current character) is an uppercase character ("if c.isupper()").
Then, sum (add together) the total number of 1's that were produced (one for each upper case character in the sentence), resulting in the total count of upper case characters.
answered Nov 19 '18 at 13:58
OperatorOverload
409312
409312
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 '18 at 14:09
add a comment |
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 '18 at 14:09
Nice. I would also add that
sum()
in the OPs case is a generator expression.– RoadRunner
Nov 19 '18 at 14:09
Nice. I would also add that
sum()
in the OPs case is a generator expression.– RoadRunner
Nov 19 '18 at 14:09
add a comment |
Try rewriting in this way, instead of using list inclusions:
count =0
for c in sentence:
if c.isupper():
count+=1
print(count)
That way, there won't be much to explain!
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, andprint
needs parentheses.
– RoadRunner
Nov 19 '18 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
add a comment |
Try rewriting in this way, instead of using list inclusions:
count =0
for c in sentence:
if c.isupper():
count+=1
print(count)
That way, there won't be much to explain!
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, andprint
needs parentheses.
– RoadRunner
Nov 19 '18 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
add a comment |
Try rewriting in this way, instead of using list inclusions:
count =0
for c in sentence:
if c.isupper():
count+=1
print(count)
That way, there won't be much to explain!
Try rewriting in this way, instead of using list inclusions:
count =0
for c in sentence:
if c.isupper():
count+=1
print(count)
That way, there won't be much to explain!
edited Nov 19 '18 at 14:11
answered Nov 19 '18 at 14:00
Matina G
50619
50619
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, andprint
needs parentheses.
– RoadRunner
Nov 19 '18 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
add a comment |
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, andprint
needs parentheses.
– RoadRunner
Nov 19 '18 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and
print
needs parentheses.– RoadRunner
Nov 19 '18 at 14:11
Can't go wrong with a simple loop. Also note that it seems that the OP is using Python 3, and
print
needs parentheses.– RoadRunner
Nov 19 '18 at 14:11
1
1
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
Thanks, fixed it
– Matina G
Nov 19 '18 at 14:12
add a comment |
Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.
sentence = "Hi My Name is"
a = 0
for c in sentence:
if c.isupper():
a = a + 1
else:
pass
print(a, " capital letters")
b = sum(1 for c in sentence if c.isupper())
print(b, " capital letters")
~
add a comment |
Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.
sentence = "Hi My Name is"
a = 0
for c in sentence:
if c.isupper():
a = a + 1
else:
pass
print(a, " capital letters")
b = sum(1 for c in sentence if c.isupper())
print(b, " capital letters")
~
add a comment |
Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.
sentence = "Hi My Name is"
a = 0
for c in sentence:
if c.isupper():
a = a + 1
else:
pass
print(a, " capital letters")
b = sum(1 for c in sentence if c.isupper())
print(b, " capital letters")
~
Your code is basically a loop that is placed inside the sum function. The loop checks every letter in turn and adds a 1 to the total if it finds a capital letter. I.e if the isupper() function returns True.
sentence = "Hi My Name is"
a = 0
for c in sentence:
if c.isupper():
a = a + 1
else:
pass
print(a, " capital letters")
b = sum(1 for c in sentence if c.isupper())
print(b, " capital letters")
~
answered Nov 19 '18 at 14:22
jandor
12
12
add a comment |
add a comment |
Try to decomposite "sum(1 for c in sentence if c.isupper())":
1) 1 for c in sentence if c.isupper()
It means: "generate 1 if symbol if c is uppercase".
Also, note that (1 for c in sentence if c.isupper()) is a generator
But because it placed as a function positional argument it can be placed there without parentheses.
2) sum(...) -- summarize all "1" generated from step 1)
sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol
add a comment |
Try to decomposite "sum(1 for c in sentence if c.isupper())":
1) 1 for c in sentence if c.isupper()
It means: "generate 1 if symbol if c is uppercase".
Also, note that (1 for c in sentence if c.isupper()) is a generator
But because it placed as a function positional argument it can be placed there without parentheses.
2) sum(...) -- summarize all "1" generated from step 1)
sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol
add a comment |
Try to decomposite "sum(1 for c in sentence if c.isupper())":
1) 1 for c in sentence if c.isupper()
It means: "generate 1 if symbol if c is uppercase".
Also, note that (1 for c in sentence if c.isupper()) is a generator
But because it placed as a function positional argument it can be placed there without parentheses.
2) sum(...) -- summarize all "1" generated from step 1)
sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol
Try to decomposite "sum(1 for c in sentence if c.isupper())":
1) 1 for c in sentence if c.isupper()
It means: "generate 1 if symbol if c is uppercase".
Also, note that (1 for c in sentence if c.isupper()) is a generator
But because it placed as a function positional argument it can be placed there without parentheses.
2) sum(...) -- summarize all "1" generated from step 1)
sum expect iterable as an argument: list, tuple, generator or another object that support iterator protocol
edited Nov 19 '18 at 14:29
answered Nov 19 '18 at 14:24
Vovk Donets
465
465
add a comment |
add a comment |
sum(1 for i in iterable )
will add 1 for each member of iterable. look at this :
sum(1 for i in [True,True,True])
will return 3
sum(10 for i in [True,True,True])
will return 30
so for each iteration sum will add the number to itself.
the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.
add a comment |
sum(1 for i in iterable )
will add 1 for each member of iterable. look at this :
sum(1 for i in [True,True,True])
will return 3
sum(10 for i in [True,True,True])
will return 30
so for each iteration sum will add the number to itself.
the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.
add a comment |
sum(1 for i in iterable )
will add 1 for each member of iterable. look at this :
sum(1 for i in [True,True,True])
will return 3
sum(10 for i in [True,True,True])
will return 30
so for each iteration sum will add the number to itself.
the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.
sum(1 for i in iterable )
will add 1 for each member of iterable. look at this :
sum(1 for i in [True,True,True])
will return 3
sum(10 for i in [True,True,True])
will return 30
so for each iteration sum will add the number to itself.
the "for c in sentence if c.isupper()" return a list of "True"s so for any character that is uppercase sum will add 1.
answered Nov 19 '18 at 14:31
Ali Kargar
1444
1444
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53376146%2fwhat-does-sum1-for-c-in-sentence-if-c-isupper-mean-in-non-programming-terms%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
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 '18 at 14:04
You could also use
sum(c.isupper() for c in sentence)
. This works becauseTrue
has a numeric value of 1 andFalse
has a numeric value of 0. So you convert the characters into ones and zeroes based on whether or not they are capital, then sum that sequence.– Patrick Haugh
Nov 19 '18 at 14:18
1
So you have an assignment, for which you did not write the code yourself, and you are asking for help convincing your teacher you did write it yourself?
– chepner
Nov 19 '18 at 14:20