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

  • SEARCH
  • Home
  • 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 8348879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:49:15+00:00 2026-06-09T07:49:15+00:00

I have recently been experimenting with python generators a bit, and I came across

  • 0

I have recently been experimenting with python generators a bit, and I came across the following curious behaviour, and I am curious to understand why this happens and what is going on:

def generating_test(n): 
    for a in range(n): 
        yield "a squared is %s" % a*a # Notice instead of a**2 we have written a*a

for asquare in generating_test(3): 
    print asquare 

Output:

a squared is 1
a squared is 2a squared is 2

Versus the following script which generates the expected output:

def generating_test(n): 
    for a in range(n): 
        yield "a squared is %s" % a**2 # we use the correct a**2 here

for asquare in generating_test(3): 
    print asquare 

Output:

a squared is 0
a squared is 1
a squared is 4
  • 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-09T07:49:16+00:00Added an answer on June 9, 2026 at 7:49 am

    This doesn’t have anything to do with generators:

    >>> a = 2
    >>> "a squared is %s" % a
    'a squared is 2'
    >>> ("a squared is %s" % a)*a
    'a squared is 2a squared is 2'
    >>> "a squared is %s" % a*a
    'a squared is 2a squared is 2'
    >>> "a squared is %s" % (a*a)
    'a squared is 4'
    

    The % op is performed before the multiplication, using the string and the first a as arguments. Your a**2 works because the ** op with a and 2 as arguments is evaluated before the %.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been experimenting with naming abstract classes and interfaces in a more
I have been experimenting recently with Silverlight, RIA Services, and Entity Framework using .NET
I have been experimenting with the devkitARM toolchain for NDS homebrew recently. Something I
This question might be a bit long and specific, but I have been attempting
Recently I have been experimenting with Firebreath and developed a plugin in order to
I've been experimenting with cronjobs recently and have it setup like so: crontab: SHELL=/bin/sh
I've recently become familiar with Reflection , and have been experimenting with it, especially
I own a server running whm / cpanel, and I have recently been experimenting
Recently I have been experimenting with node.js and I find its event-driven nature very
I'm new to server-side development and have been experimenting with the Play framework recently.

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.