Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Python How to Program (With CD-ROM)    (ISBN: 0130923613)


 

 List Price: $80.00
 Our Price: $80.00
 Used Price: $48.50

 Release Date: 04 February, 2002
 Manufacturer: Prentice Hall (Paperback)
 Sales Rank: 203,285

 Author: Harvey M. Deitel, Paul J. Deitel, Jonathan P. Liperi, B. A. Wiedermann, Jonathan P. Liperi









More Info

 For Loop Example 2002-03-14 03:01:04
  python 
Category: source:python:general
Description: Shows how to use for loops using python.
Platform: unix
Author: mind
Viewed: 4692
Rating: 3.2/5 (15 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
for.py
#!/usr/local/bin/python
#
# for example by mind@metalshell.com
#
# an example on for loops
#
# 03/12/2002
#
# http://www.metalshell.com
#

# the range() function lays out a range between integers.
# range(0,10) would look like: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# for each num in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# go through each element in range()
for num in range(0, 10):
  # print each number
  print num


# heres an example on finding duplicate strings using for.

# define our sequence of strings
names = ['John', 'Mark', 'Henry', 'Mark', 'Marie']

# this will sort all the strings in our sequence
names.sort()

# define our strings and integers for our for statement
dup_count = 0
prev = names[0]
del names[0]

for name in names:
  if prev == name:
    dup_count = dup_count + 1
    print "Duplicate found:", name
  prev = name

if dup_count == 0:
  print "Done! no duplicates were found"
else:
  print "Total duplicates found:", dup_count
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?