Sum in range that meet criteria [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Trying to learn a new approach.It's the classical problem of finding the sum of all multiples of 3 and 5 below 'n'.
What I want is:
print ("The sum is: ", sum(range(1, 100)))
But with an if-statement for multiples of 3 / 5.My question:How can I include that if-statement for a one-line solution?
Thanks for your help. =)// Chris S.

This is the approach you may look into:
def thesum(the_numbers):
the_numbers = [i for i in the_numbers if i % 3 == 0 or i % 5 == 0]
return print(sum(the_numbers))
thesum(range(100))

Related

input function : python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
counttonum = 1
countnum = input("[•] Provide A Number :")
while counttonum < countnum:
print("[", counttonum, "] : Number = ", counttonum)
counttonum +=1
I was trying to make a counting tool that counts up to the provided number from the “input()” function.
For example:
providedNumberFromInput = 5
output = 1
2
3
4
5
And it’ll stop if the provided number is reached. Please help me.
You are very close to solution. Problem is that input() returns value as string so you will need to convert it. And also if you want to include entered number use <= instead of <
while counttonum <= int(countnum):

python: list of tuples search [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Need some help here
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
if the last 6 digits are located to return the first digit.
example if finds 10,11,29,30,36,47 return 2
You can use next similair to user's approach:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
to_find = [10,11,29,30,36,47]
print(next(n for n, *nums in num if nums == to_find))
2
You can use next with a conditional generator expression:
num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
search = 11
next(first for first, *rest in num if search in rest)
# 2

I am a python student, and I have a problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def thing():
_list = [1,1,5,6,8,3,4,6,10,23,5,12,67,3,25,2,6,5,4,3,2,1]
_list1 = [str(i) for i<=5 in lista]
return " ".join(_list1)
print(thing()))
I am new to this type of list managment, I am trying to put in _list1 only integers that are less then 5
so like the name of your function, it's created to calculate the sum of integers.
So basically, if your n=0 then your program will return 0 as result.
If not (else), it gonna give you a result of the calculation in return.
And here you want to print the interger sum of 451 which will give you the result of n % 10 + integer_sum(int(n / 10))

How can i put this into a list comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
write a function super_sum(A), that takes in an array of numbers A, and returns the sum of all the numbers after doubling the odd numbers and halving the even numbers.
heres my solution: how do solve this with a list comprehension
def super_sum(A):
new_sum=[]
total=0
for x in A:
if x%2==0:
x=x/2
elif x%2!=0:
x=x*2
new_sum.append(x)
return sum(new_sum)
print (super_sum([10,3,5]))
sum([x/2 if x%2==0 else x*2 for x in [10,3,5]])

Recursive sequence $x_n = \sqrt{2}$, $x_{n+1} = \sqrt{2x_n}$ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would I create a script it in python late the values from the recursive sequence:
$x_1 = \sqrt{2}$, $x_{n+1} = \sqrt{2x_n}$ http://www.sciweavers.org/upload/Tex2Img_1392861864/render.png
X = [sqrt(2)]
for i in range(1,10):
X.append(sqrt(2*X[i-1]))
Here is a slow solution. Assuming n is >=1
import math
def recursive(n):
if n = 1:
math.sqrt(2)
return math.sqrt(2*recursive(n-1))

Categories