Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:49:45+00:00 2026-06-10T04:49:45+00:00

Possible Duplicate: The Python yield keyword explained Can someone explain to me what the

  • 0

Possible Duplicate:
The Python yield keyword explained

Can someone explain to me what the yield statement actually does in this bit of code here:

 def fibonacci():
     a, b = 0, 1
     while True:
         yield a
         a, b = b, a+b

for number in fibonacci(): # Use the generator as an iterator; print number

What I understand so far is, we are defining a function finonacci(), with no parameters?
inside the function we are defining a and b equal to 0 and 1, next, while this is true, we are yielding a. What is this actually doing? Furthermore, while yielding a? a is now equal to b, while b is now equal to a + b.

Next question, for number in fibonacci(), does this mean for every number in the function or what? I’m equally stumped on what yield and ‘for number’ are actually doing. Obviously I am aware that it means for every number in fibonacci() print number. Am I actually defining number without knowing it?

Thanks, sorry if I’m not clear. BTW, it’s for project Euler, if I knew how to program well this would be a breeze but I’m trying to learn this on the fly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:49:46+00:00Added an answer on June 10, 2026 at 4:49 am

    Using yield makes the function a generator.
    The generator will continue to yield the a variable on each loop, waiting until the generator’s next() method is called to continue on to the next loop iteration.

    Or, until you return or StopIteration is raised.

    Slightly modified to show use of StopIteration:

    >>> def fib():
    ...     a = 0
    ...     b = 1
    ...     while True:
    ...         yield a
    ...         a = b
    ...         b += a
    ...         if a > 100:
    ...             raise StopIteration
    ...
    >>>
    >>> for value in fib():
    ...     print value
    ...
    0
    1
    2
    4
    8
    16
    32
    64
    >>>
    
    >>> # assign the resulting object to 'generator'
    >>> generator = fib()
    >>> generator.next()
    0
    >>> generator.next()
    1
    >>> for value in generator:
    ...     print value
    ...
    2
    4
    8
    16
    32
    64
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: The Python yield keyword explained Okay, I've probably phrased the question badly
Possible Duplicate: What does @ mean in Python? Here is the code I don't
Possible Duplicate: Python or Ruby Interpreter on iOS I just discovered this apps pypad
Possible Duplicate: [python]: path between two nodes Can anyone point me to some resources
Possible Duplicate: Learn Python the Hard Way Exercise 17 Extra Question(S) In this exercise
Possible Duplicate: Python - '>>' operator What does the >> operator means in Python?
Possible Duplicate: Python float - str - float weirdness Python float division does not
Possible Duplicate: Call Python From PHP And Get Return Code probably a very stupid
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python Ternary Operator Does Python have an equivalent of the ternary operator?:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.