Question

Hi! I'm learning Python and I'm struggling with the following problem. I'm trying to learn Generators,...

Hi!

I'm learning Python and I'm struggling with the following problem. I'm trying to learn Generators, specifically. What would be a pythonic way to perform a calculation from 1 - 300 to list all numbers divisible by 6 and 10 using a list comprehension or generator expression?:

>>>nums = numbers()
>>>iter(nums) == nums
True
>>>next(nums) == 30
True
>>>next(nums) == 60
True
>>>next(nums)
90
...etc.

Here is what I have so far:
def numbers():
i = [x for x in range(1, 301) if x % 6 == 0 and x % 10 == 0]
yield i

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def numbers():
    for x in range(1, 301):
        if x % 6 == 0 and x % 10 == 0:
            yield x


nums = numbers()
print(iter(nums) == nums)
print(next(nums) == 30)
print(next(nums) == 60)
print(next(nums))

Add a comment
Know the answer?
Add Answer to:
Hi! I'm learning Python and I'm struggling with the following problem. I'm trying to learn Generators,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi I'm trying to write a code for a web server in python with flask. This...

    Hi I'm trying to write a code for a web server in python with flask. This is what I have so far from flask import Flask app = Flask(__name__) @app.route('/') #first endpoint i.e. "http://localhost/" def index(): return 'hello' #return data in string if __name__ == '__main__': app.run(debug=True) After running the code, I'm given a address to the web with the text hello. The problem is that this only works with my computer that is running the code. If I try...

  • Can someone fix this python program? I'm trying to do three things: Create a function that...

    Can someone fix this python program? I'm trying to do three things: Create a function that takes in a string as a parameter. Then create a dictionary of characters to integers, where the integer represents how many times the number of times the character appears in the word. Create a function that takes two lists as parameters and returns the elements they have in common of the two lists. Ask the user to create a list of numbers and keep...

  • I'm trying to model the velocity over time of a given time interval in python, but...

    I'm trying to model the velocity over time of a given time interval in python, but my code won't work. I need to solve the problem using the Euler method. import numpy as np from matplotlib import pyplot as plt def car(x0, v0, Cd, p, A, m, facc, T, dt): n = 0 t = np.arange(0, T, dt) x = np.array([x0]) v = np.array([v0]) while x[n] > 0: x = np.append(x, x[n]+v[n]*dt) v = np.append(v, v[n]+(facc/m-(1/(2*m))*Cd*A*p*v[n])*dt) t = np.append(t, t[n]+dt)...

  • Hi, This question is from python unit Q2. Booleans and testing (a) Define the following function....

    Hi, This question is from python unit Q2. Booleans and testing (a) Define the following function. 4 marks def valid(x, y, z): #valid takes three numbers and it returns True iff #at least one of them is positive and at most one of them is negative (b) Define the following function. 6 marks def test_valid(): #test_valid returns True iff valid is correct test_valid should perform a set of well-chosen tests on valid, with at least eight tests.

  • UPDATED: Hi, I have a problem in PYTHON I've been trying to solve. I'm given two csv files containing data in th...

    UPDATED: Hi, I have a problem in PYTHON I've been trying to solve. I'm given two csv files containing data in the following format of a micro car that moves around: Action, time, speed. (e.g. N,t,s= Move North for t seconds with speed s meters per second) One list is the expected results, the second list is the actual (recorded) results. The car can move N,S,E,W. The data entry will look like this: (CSV files) Expected: Actual: I am trying...

  • Python problem: How do you include the number that you are trying to process if it's...

    Python problem: How do you include the number that you are trying to process if it's the sentinel?  This is my solution won't include -999 in the process, but I am not sure how do this... # write a program that prints out the average of # the negative numbers in the list that appear before a -999 is detected. def main(): MyList = [ 23, -45, 6, -23, -9, 77, 54, -54, 21, -2, 8, -3, -23, 45, 93, -43,...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • I'm learning how to write loops in Python 3+ but need help in solving this function...

    I'm learning how to write loops in Python 3+ but need help in solving this function with another function: def babylonian_square_root(N, estimate, precision): new_estimate = (estimate + (N / estimate)) / 2 # I need to complete this using this function close_enough(). def close_enough(x, y, maximum_allowable_difference): ''' Returns True if x and y are within maximum_allowable_difference of each other. ''' # Note: "maximum" implies that we should use <= , but for this to work mathematically we need to use...

  • Hi, I'm trying to learn how to use the Arrhenius equation and I am very confused,...

    Hi, I'm trying to learn how to use the Arrhenius equation and I am very confused, can someone help? Thank you! The activation energy for the gas phase decomposition of cyclobutane is 262 kJ/mol. (CH2)42 C2H4 The rate constant for this reaction is 2.16×10-4 s-1 at 711 K. What is the rate constant at 749 K? Use the Arrhenius equation to determine Ea. Close Problem For the gas phase decomposition of cyclobutane, the rate constant is 8.84×10-4 s-1 at 734...

  • Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in...

    Greeting! Kindly help me to solve my finals in PYTHON, I don't have a knowledge in PYTHON, new student. Please, please, I'm begging, Kindly answers all the questions. I'm hoping to grant my request. Thanks in advanced. 1.) What is the output of the following snippet? l1 = [1,2] for v in range(2): l1.insert(-1,l1[v]) print(l1)        a.) [1, 2, 2, 2]        b.) [1, 1, 1, 2]        c.) [1, 2, 1, 2]        d.) [1,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT