Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-01-2013, 07:05 PM   #1
Baseband Member
 
palermo's Avatar
 
Join Date: Jan 2013
Location: United States
Posts: 81
Default Python Question?

Why does this work? How does it associate "letter" with an individual letter? I've been reading "Think Python" and Google searching but am still having trouble understanding programming.

for letter in 'Python':
print 'Current Letter :', letter

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
__________________

palermo is offline   Reply With Quote
Old 05-01-2013, 07:21 PM   #2
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Python Question?

The for-in loop is kind of a special (and powerful) version of the for-loop.

Normally, to do what that does you'd need to do something like the following:

Code:
String python = "Python"
String[] letters = python.toArray()

for (int i = 0; i < letters.size(); i++) {
    print letters[i]
}
What the the for-in loop does is breaks the string into an irritable array for you so you don't need to worry about out of bounds errors or manually creating more temporary variables than needed. It's all done in the background for you after the code is compiled.
__________________

__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 05-01-2013, 07:33 PM   #3
Baseband Member
 
palermo's Avatar
 
Join Date: Jan 2013
Location: United States
Posts: 81
Default Re: Python Question?

Thank you.
palermo is offline   Reply With Quote
Old 05-01-2013, 09:49 PM   #4
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Python Question?

You're welcome
__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:38 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0