How do i square, add and then square root all the items in a list? (python)

Watch
Announcements
Thread starter 1 week ago
#1
basically the title
0
1 week ago
#2
What have you tried so far?

number ** 2 -- this squares a number
sum(iterable) -- this sums the items in an iterable
number ** 0.5 -- this square roots a number
Last edited by Composure; 6 days ago
0
Thread starter 1 week ago
#3
(Original post by Composure)
What have you tried so far?

number ** 2 -- this squares a number
sum() -- this sums the items in an iterable
number ** 0.5 -- this square roots a number
I have however, how would i apply that to a tuple I.e. If i had (1,3,5) how would I square, add and square root all those numbers in one step?
0
1 week ago
#4
(Original post by Benjaminepalatty)
I have however, how would i apply that to a tuple I.e. If i had (1,3,5) how would I square, add and square root all those numbers in one step?
Uhh, you would do it in the exact same way...
Code:
>>> sum(n ** 2 for n in (1, 3, 5)) ** 0.5
5.916079783099616
0
Thread starter 1 week ago
#5
(Original post by Composure)
Uhh, you would do it in the exact same way...
Code:
>>> sum(n ** 2 for n in (1, 3, 5)) ** 0.5
5.916079783099616
how do i do that for a list that's inputted by the user?
0
1 week ago
#6
(Original post by Benjaminepalatty)
how do i do that for a list that's inputted by the user?
Code:
numbers = []
for _ in range(3):
numbers.append(int(input("Enter a number: ")))

print(sum(n ** 2 for n in numbers) ** 0.5)
0
Thread starter 1 week ago
#7
(Original post by Composure)
Code:
numbers = []
for _ in range(3):
numbers.append(int(input("Enter a number: ")))

print(sum(n ** 2 for n in numbers) ** 0.5)
Thank you for the help but i meant as in inputted via a positional argument i.e
def numbers(v1,v2)
......
0
1 week ago
#8
(Original post by Benjaminepalatty)
Thank you for the help but i meant as in inputted via a positional argument i.e
def numbers(v1,v2)
......
So you wanted to do all this to a list, then a tuple, then back to a list, then with user inputs except you didn't mean user inputs and actually meant inside a function using parameters...

What is stopping your from doing it with a function? The code is still the same.

What does the function call look like? Are you calling the function like this numbers(1, 3, 5)?
Last edited by Composure; 1 week ago
0
X

new posts
Back
to top
Latest
My Feed

Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Poll

Join the discussion

How would you describe the quality of the digital skills you're taught at school?

Excellent (18)
8.29%
Okay (64)
29.49%
A bit lacking (81)
37.33%
Not good at all (54)
24.88%