Learning Python Loops: From Counting to Exponents of 2

  • Context: Python 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    Loops Python
Click For Summary

Discussion Overview

The discussion revolves around using Python loops, specifically focusing on counting and generating powers of 2. Participants explore the correct syntax and logic for implementing a while loop to print powers of 2 up to 4096, addressing both coding errors and conceptual misunderstandings related to exponentiation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant shares their initial attempt at a loop to count by 2s and expresses confusion about generating powers of 2.
  • Another participant points out syntax errors in the provided code and suggests using print statements to clarify what values are being calculated.
  • A later reply acknowledges the confusion around exponentiation and requests guidance on how to express the desired sequence in Python.
  • One participant proposes a corrected version of the loop that successfully prints the powers of 2 by multiplying the base by 2 each iteration.
  • Another participant confirms that the proposed solution works and expresses understanding of how the multiplication relates to exponentiation.
  • Some participants share tips about manually tracing code execution to better understand variable states, emphasizing its value for beginners.

Areas of Agreement / Disagreement

Participants generally agree on the correct approach to generating powers of 2, with one solution being confirmed as effective. However, there is no consensus on the initial misunderstanding of the exponentiation process and its implementation in code.

Contextual Notes

Participants express varying levels of familiarity with Python syntax and concepts, leading to different interpretations of how to implement loops and exponentiation. Some misunderstandings about the mathematical operations involved remain unresolved.

Who May Find This Useful

New programmers learning Python, particularly those struggling with loops and exponentiation concepts.

mr.me
Messages
49
Reaction score
0
Hi everyone, I'm new to programming, studying python in school.

here's my first go at a loop to count up to 100 by 2, works fine

stop_count=100
count=2
while(count<=stop_count):
print count
count=count+2

But one of my practice problems is to use 'while' to
print 2 and all powers of 2 through and including 4096

I suppose I don't quite understand the math for a sequence of exponents or the correct syntax for the code, here's my try:

stop_pow=4096
base=2
while (base<=stop_pow):
print base
base= base**base+1

Which of course just printed the value of 2^3

Would someone please show me the correct code so I can understand my mistake and this type of operation.
 
Last edited:
Technology news on Phys.org
Please use [noparse]
Code:
[/noparse] tags when posting your code. This is especially important in Python, where

Code:
stop_pow=4096
base=2
while (base<=stop_pow):
print base
base= base**base+1

is syntactically incorrect, while

Code:
stop_pow=4096
base=2
while (base<=stop_pow):
    print base
base= base**base+1

and

Code:
stop_pow=4096
base=2
while (base<=stop_pow):
    print base
    base= base**base+1

are syntactically correct, but different.

Try to add "print base**base+1" to your loop to see what you are calculating. It is not what you think it is. And I don't see how it could print just 8, it has to start printing 2 and 5.
 
Thank-you
Well yes, sorry, it does print 2 and 5 and 8 and if I include
print base**base+1
it only prints five, indefinitely.

I see that I am raising a number (2) to itself and adding one, I am completely unaware of how to use the language to respectively raise 2 to all following integers until 4096 is reached, other than the part with the conditional statement that I assume is correct


Ultimately I don't understand how to say that I want it to return each power of 2 up until 4096, I understand what they are but not how to use python to say "I want 2 raised to (2+1) and 2 raised to (2+2) and to raised to (2+3)". Where should I place certain values in the function to create this progression?

I'm very, very new to this and need some help understanding the nomenclature, I'm not asking anyone to due my work for me, just show me the correct way here so that I can derive the correct way later.
 
what about
Code:
stop_pow = 4096
base = 2
# print the seq. 2, 4, 8, ..., 4096
while (base <= stop_pow):
    print base
    base = base*2
does it work?
 
Last edited:
Yes it indeed does work. Thank you very much boaz

Seeing that the "base" is multiplied upon each recursion makes sense as how it raises the power of the exponentiation
 
a simple trick that helped me out is taking a paper and like the computer going over each line and writing what is stored in the variables. it sounds odd and like a waste of time, but for a newbie is a life-saver.
 
boaz said:
a simple trick that helped me out is taking a paper and like the computer going over each line and writing what is stored in the variables. it sounds odd and like a waste of time, but for a newbie is a life-saver.
This is excellent advice, and is something that newbies should get in the habit of doing. You don't really understand the code you write unless you know exactly what it is doing.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K