Python: Input and Variables - For Loop
(Page 4 of 4 )
The For Loop will loop for every value you have in a list. Here is a sample:
#!/usr/local/bin/python
for letter in “James”:
print letter
This code will loop until every letter of James is printed out, like so:
J
A
M
E
S
Pretty simple right? Let's involve the user more:
#!/usr/local/bin/python
user_name = raw_input(“Enter your name: “)
for letter in user_name:
print letter
If the users name is something like Javier, it will print out:
J
A
V
I
E
R
And that is it for loops in Python. There are no Do While loops as in other languages.
Conditional Statements
Sometimes in programming you need to ask certain questions and have the program respond accordingly. You can do so with conditional statements. Basically the way they work is this: "If this, then that." They get more complicated of course. You also can say "If this happens, do this, else do that." Or even "If this and That, or This or That, then do this else do that or that."
I hope that is confusing, because this is the end of the article, and I want to ensure you return to enlighten yourself when I write the next episode on Python Conditionals.
Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |