Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 27th, 2003, 03:27 PM
NightStalkerX6 NightStalkerX6 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 NightStalkerX6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Fibonacci sequence example has be slightly confused

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
... print b
... a, b = b, a+b
...
1
1
2
3
5
8


--------------------

This doesn't entirely make sense to me and I was wonderfing if someone could help explain it. I've just got started in programming and I don't understand how a & b are being assigned completely different variables and how it would make it calculate itself in a continuous loop if it weren't for the while b < 10:
thanks,
NightStalkerX6

Reply With Quote
  #2  
Old September 27th, 2003, 08:34 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hey Night,

First you need to understand that anything after a # up untill the end of the line is a comment, so just forget about them .

The program first assigns the value 0 to a and 1 to b, next it starts a while loop which will continue untill b is not less than 10. Every time the loop runs though it prints the value of b then assignes that value to a and gives b the value of a + b

In theory the loop would keep going, adding a and b and printing the output, anyway i hope that's cleared things up for you

Welcome to Python, have fun!

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old September 30th, 2003, 03:50 PM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 6
wassup NightStalker,

it helps to understand how a and b are assigned different values if you re-write the code like this:

Code:
a=0
b=1  #separate assignments

while (b < 10):
  print b
  a = b
  b = a + b


i'm not sure if that's exactly what you were asking... if you look through the tutorials for python though, you could easily understand the concept of the while loop

Reply With Quote
  #4  
Old October 29th, 2003, 11:35 AM
cvchen cvchen is offline
Hi, I'm Calvin
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: LosAngeles, SanDiego, Houston
Posts: 50 cvchen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 7 sec
Reputation Power: 6
oops, i goofed

in a statement such as this:
a,b = b,a+b

...python evaluates all expressions (ie b and a+b) on the right hand side before assigning any values to a or b. the previous example that i gave trying to clarify the assignments was incorrect, so sorry!

it's more like this:
Code:
a=0
b=1 #separate assignments
while(b<10):
  A = b
  B = a+b #eval before assigning
  a=A
  b=B #assign after evaluating


sorry yall.

Reply With Quote
  #5  
Old September 13th, 2005, 09:17 PM
hamzazuberi hamzazuberi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 1 hamzazuberi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 4 sec
Reputation Power: 0
This is the simplest way to do it

Variables a, b, c, d

a = 1
b = 0
c = 1

While c < n # number of repetitions

c = c + 1

Print a

d = b
b = a
a = b + d

Loop

Reply With Quote
  #6  
Old September 13th, 2005, 10:46 PM
lw22's Avatar
lw22 lw22 is offline
Caress me down
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Pennsylvania
Posts: 282 lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level)lw22 User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 5 Days 12 h 2 m
Reputation Power: 240
this is off topic but what is the number called if you divide 2 numbers of the fib sequence together? like 89/55 gets you close to some irrational number. as you move down the fib sequence you get closer and closer to the irrational number.

it has something to do with pentagons too... and other shapes like a dodecahedran.
__________________
so you think you can tell heaven from hell

Reply With Quote
  #7  
Old September 13th, 2005, 11:47 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Click here for more information.
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,713 Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 11 h 27 m 13 sec
Reputation Power: 1179
It's called the Golden Ratio.
Comments on this post
lw22 agrees: thanks
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Fibonacci sequence example has be slightly confused


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT