Skip to content

Latest commit

 

History

History
94 lines (69 loc) · 1.59 KB

File metadata and controls

94 lines (69 loc) · 1.59 KB

Video 12: python part 5 - while

Introduction

count = 0
while count < 5:
    print count
    count = count + 1
    
count = 0
while count < 10:
    print count
    count += 3
    
for count in range(0,10,3):
    print count

Code

mydaemon.py

#!/usr/bin/env python

import time # use sleep

count = 0
running = True
while running:
    print count
    count += 1
    time.sleep( 0.2 )

    if count > 20:
        running = False

print 'Cleaning up and exiting'

data.dat:

# header 1
# This file contains sample junk data
1 2 3
123 45 12
123 432 88

readdata.py

#!/usr/bin/env python

infile = open('data.dat', 'r')

line = infile.readline()
while line[0] == '#':
    line = infile.readline()
    # Skip comments
    pass

for line in infile:
    print line

readdata2.py:

#!/usr/bin/env python

def loaddata():
    for line in open('data.dat'):
        if '#' in line:
            continue
        print line