Home arrow Python arrow Page 4 - Python: Input and Variables

For Loop - Python

In our last article we covered the history of Python, the print command, working with strings and variables, and how to comment your code. In this episode we will learn how to retrieve input from users, work with variables, and if you are lucky, Loops.

TABLE OF CONTENTS:
  1. Python: Input and Variables
  2. Beating...I Mean Asking the User for Information
  3. Loops
  4. For Loop
By: James Payne
Rating: starstarstarstarstar / 8
November 19, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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...



 
 
>>> More Python Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: