What does sum(1 for c in sentence if c.isupper())) mean in non programming terms
up vote
1
down vote
favorite
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
New contributor
add a comment |
up vote
1
down vote
favorite
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
New contributor
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 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 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 at 14:20
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
New contributor
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
New contributor
New contributor
edited Nov 19 at 14:15
J100
854312
854312
New contributor
asked Nov 19 at 13:54
DavidP75
61
61
New contributor
New contributor
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 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 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 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 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 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 at 14:20
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 at 14:04
Be specific about what you don't understand. As it stands your question is too broad.
– kenlukas
Nov 19 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 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 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 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 at 14:20
add a comment |
5 Answers
5
active
oldest
votes
up vote
4
down vote
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 at 14:09
add a comment |
up vote
3
down vote
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 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 at 14:12
add a comment |
up vote
0
down vote
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")
~
New contributor
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
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 at 14:09
add a comment |
up vote
4
down vote
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 at 14:09
add a comment |
up vote
4
down vote
up vote
4
down vote
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 at 13:58
OperatorOverload
409312
409312
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 at 14:09
add a comment |
Nice. I would also add thatsum()
in the OPs case is a generator expression.
– RoadRunner
Nov 19 at 14:09
Nice. I would also add that
sum()
in the OPs case is a generator expression.– RoadRunner
Nov 19 at 14:09
Nice. I would also add that
sum()
in the OPs case is a generator expression.– RoadRunner
Nov 19 at 14:09
add a comment |
up vote
3
down vote
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 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 at 14:12
add a comment |
up vote
3
down vote
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 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 at 14:12
add a comment |
up vote
3
down vote
up vote
3
down vote
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 at 14:11
answered Nov 19 at 14:00
Matina G
37619
37619
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 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 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 at 14:11
1
Thanks, fixed it
– Matina G
Nov 19 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 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 at 14:11
1
1
Thanks, fixed it
– Matina G
Nov 19 at 14:12
Thanks, fixed it
– Matina G
Nov 19 at 14:12
add a comment |
up vote
0
down vote
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")
~
New contributor
add a comment |
up vote
0
down vote
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")
~
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
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")
~
New contributor
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")
~
New contributor
New contributor
answered Nov 19 at 14:22
jandor
12
12
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 14:29
answered Nov 19 at 14:24
Vovk Donets
465
465
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 14:31
Ali Kargar
1444
1444
add a comment |
add a comment |
DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.
DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.
DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.
DavidP75 is a new contributor. Be nice, and check out our Code of Conduct.
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 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 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 at 14:20