Learning Python Loops: From Counting to Exponents of 2

In summary: Tags: In summary, I am new to programming, and was trying to do a loop to count up to 100 by 2, but I ran into an issue with using the language. I was able to solve it by printing the value of 2 and 5 and 8 and so on, but I am not sure how to get to 4096. I was told that base should be multiplied upon each recursion, but I am still having trouble understanding how to do that.
  • #1
mr.me
49
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:
  • #5
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
 
  • #6
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.
 
  • #7
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.
 

Related to Learning Python Loops: From Counting to Exponents of 2

1. What are loops in Python and why are they important?

Loops in Python are a way to repeatedly execute a block of code. They are important because they allow for efficient and concise coding, especially when performing repetitive tasks or iterating through data.

2. How many types of loops are there in Python?

There are two types of loops in Python: for loops and while loops. For loops are used for iterating through a sequence, while while loops are used for executing a block of code as long as a condition is met.

3. How do you write a for loop in Python?

A for loop in Python is written using the for keyword, followed by a variable name, the in keyword, and a sequence or range of values. For example: for num in range(5): will execute the code block 5 times, with the variable num taking on the values 0, 1, 2, 3, and 4.

4. Can you explain the use of the range() function in loops?

The range() function in loops is used to generate a sequence of numbers. It takes in three parameters: the starting value, the ending value (not inclusive), and the step size. By default, the starting value is 0 and the step size is 1. For example, range(1, 10, 2) will generate the numbers 1, 3, 5, 7, and 9.

5. How do you write a while loop in Python?

A while loop in Python is written using the while keyword, followed by a condition. The code block will continue to execute as long as the condition is true. For example: while x < 10: will execute the code block as long as the variable x is less than 10.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
10
Views
768
  • Programming and Computer Science
Replies
4
Views
995
  • Programming and Computer Science
Replies
3
Views
386
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
754
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
537
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top