Calculate the sum of a list in python [duplicate] - python

This question already has answers here:
sum of squares in a list in one line?
(5 answers)
Closed 1 year ago.
I try to make this calculation quite simple 1^2 + 2^2 + 3^2 + 6^2 = 12
But I can't find the right function.
I tried to do it with sum
av = [1,2,3,6]
print(sum(av)**2)
But the result is not good because it makes (1+2+3+6)^2 = 144
Someone knows the solution to this problem please ?

Try this:
print(sum([i**2 for i in av]))
I also note that in your original formula:
1^2 + 2^2 + 3^2 + 6^2
Is NOT equal to 12... Have you explained your expectation right?
1 + 4 + 9 + 36 = 50
If you are looking to calculate the average of the square terms, then (taking advantage of Pavel's point below)...
print(sum(i**2 for i in av)/len(av))

Related

Converts a large integer (or a string representation of an integer) to a friendly text representation like 1000000 to 1M or 1000000000 to 1B [duplicate]

This question already has answers here:
Formatting long numbers as strings
(13 answers)
Closed 9 months ago.
Converts a large integer (or a string representation of an integer) to a friendly text representation like 1000000 to 1M or 1000000000 to 1B so it can show that way on the graph, I am pretty much new to data science, I look everywhere for possible way to do it I can't, i am sure the answer will help someone else too. Thanks!
def friendly_text(i):
if i >= 1000000000000:
return str(i / 1000000000000) + 'T'
if i >= 1000000000:
return str(i / 1000000000) + 'B'
if i >= 1000000:
return str(i / 1000000) + 'M'
if i >= 1000:
return str(i / 1000) + 'K'
print(friendly_text(2555))
print(friendly_text(241555))
print(friendly_text(241535555))
print(friendly_text(2415533347615))
print(friendly_text(2415537537355355515))
# will print:
# 2.555K
# 241.555K
# 241.535555M
# 2.415533347615T
# 2415537.5373553555T
The humanize library does a pretty good job of this.
https://python-humanize.readthedocs.io/en/latest/number/
For example
intword("1000000")
returns
1.0 million

Python Math Giving Incorrect Output [duplicate]

This question already has answers here:
Python and Powers Math
(3 answers)
Closed last year.
Python is being weird again. When I put 5 * (40 ^ 2) + 50 * 40 + 100 it returns 2310. But on a calculator its 10100. I don't know why Python is making this mistake nor how to fix it. Anyone got any ideas?
If you will write
print( 5 * 40 ** 2 + 50 * 40 + 100 )
you will get the expected result.
10100
Try this instead:
5 * (40 ** 2) + 50 * 40 + 100

Alternate way of Squaring a Number

I came up with this technique a few years ago. It seems to be working fine.
Input: a number
Output: Its' square
x=int(input("Enter the number whose square you wish to find out: ")) #decimal input invalid
last_digit=x%10
#We will use formula Sn= (n/2)*(2a+ (n-1)d) of AP
a=100 + last_digit*20 #100,20 are fixed values
d=200 #200 is a fixed value
n=(x-last_digit)/10
final_answer=(n/2)*(2*a + (n-1)*d) + last_digit**2 #These calculations are easier than x*x for a vvlarge x
#normal multiplication is d*d digits, but this is d*(d-1) or d*(d-2) digits
print("My answer: " ,format(final_answer,'f'))
print("Actual answer: ", x**2)
I have written comments to indicate what Im doing at each step
-> How does this work? Like seriously? I got this by observing some patterns and generalising them
-> This code was meant to work only for 3-digit numbers, but it works for all numbers. How?
By expanding/substitution, my 'derivation' is as follows:-
NOTE: L=last_digit
n = (x-L)/10 #The same thing as n=x//10
a = 100 + 20L
d = 200
Our final answer is:-
=> (n/2) * (2a + (n-1)d ) + L^2
Substituting values for the variables,
=> [(x-L)/20] * [200 + 40L + [(x-L)/10]*200 - 200] + L^2
Taking the 20 in [(x-L)/20] and taking it to the RHS of * sign,
=> (x-L) * [10 + 2L + x - L -10] + L^2
=> (x-L)*(x+L) + L^2
=> x^2 - L^2 + L^2
=> x^2
Your code is not giving correct output for 10 to 19 and
it's only give correct output when x//10 is multiple of 2 because of this expression (n/2)*(2*a + (n-1)*d) + last_digit**2 has n/2.
and for rest of test cases it is giving approximate answer.
and Expand the terms and You'll end up with x^2((2*last_digit/x) + 1) and now it's obvious why those magic numbers were giving correct output .
Your algorithm is failing for large numbers. I tried it only for integers and here is the list of some integers where the result differed-
94906267
94906269
94906271
And so on...
An interesting thing to note here is all the numbers which are creating the problem are odd.

Rounding by modulus in python

Apologies if this was asked before, I can't seem to find an answer in regards to arbitrary multiples.
I would like a function that rounds a number by multiples.
E.g, 17.4 would be rounded in multiples of 5 to 15, where 17.6 would result in 20.
This is what I came up with:
def value_2_rounded_multiple(value, multiple=1):
return round(value / multiple) * multiple
Is this reasonable, or is there a better approach?
This is some auxiliary code for testing:
multiple = 5
shift = -0.1
val_ = (multiple / 2) + shift + 3 * multiple
print("{} becomes {}".format(val_, value_2_rounded_multiple(val_, multiple=multiple)))

Python finding n as 101 instead of 100 [duplicate]

This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
Python loops are finding n as 101 instead of 100, I am getting the average of 5050 as 50 instead of 50.50, What could be the problem? How should I go through it? Here is my function.
def sum_and_average(n):
total = 0
for i in range(1, n+1):
total += i
average = float(total/n)
print "the sum is %d and the average is %f" %(total, average)
sum_and_average(100)
It returns:
the sum is 5050 and the average is 50.000000
Do float(total) / n.
In Python 2 when one of the arguments is float, the calculation will be carried out in float.
But doing float(total/n) won't work, since total/n has already been calculated in integers, and floating the result is already too late.
To get the average you want this:
average = float(total)/n
Some examples:
>>> float(101/2)
50.0
>>> 101/2
50
>>> float(101)/2
50.5

Categories

Resources