Comment

맨 위의 #!/usr/bin/python3 는 Python 설치 경로로, shell에서 파일을 그냥 실행할 수 있도록 함 ex) > Comment.py

#!/usr/bin/python3
# comments.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Group, LLC

def main():
    for n in primes(): # generate a list of prime numbers 
        if n > 100: break
        print(n)

def isprime(n):
    if n == 1: # one is never prime
        return False
    for x in range(2, n):
        if n % x == 0:
            return False
    else:
        return True

def primes(n = 1):
   while(True):
       if isprime(n): yield n   #yield makes this a generator
       n += 1 

if __name__ == "__main__": main()

fibonacci series

#!/usr/bin/python3

# simple fibonacci series
# the sum of two elements defines the next set
class Fibonacci():
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def series(self):
        while(True):
            yield(self.b)
            self.a, self.b = self.b, self.a + self.b

f = Fibonacci(0, 1)
for r in f.series():
    if r > 100: break    
    print(r, end=' ')

1 1 2 3 5 8 13 21 34 55 89

Jump Table

#!/usr/bin/python3
# jumptable.py by Bill Weinman [http://bw.org/]
# This is an exercise file from Python 3 Essential Training on lynda.com
# Copyright 2010 The BearHeart Group, LLC

class jumptable():
    jumptable = {}

    def set(self, k, v):
        self.jumptable[k] = v

    def go(self, index):
        if index in self.jumptable:
            self.jumptable[index]()
        elif 'default' in self.jumptable:
            self.jumptable['default']()
        else:
            raise RuntimeError('undefined jump: {}'.format(index))

def main():
    j = jumptable();
    j.set('one', one)
    j.set('two', two)
    j.set('three', three)
    j.set('default', default)

    try:
        j.go('one')
    except RuntimeError as e:
        print(e)

def one():
    print('This is the "one" function.')

def two():
    print('This is the "two" function.')

def three():
    print('This is the "three" function.')

def default():
    print('this is the default function.')

if __name__ == "__main__": main()

Argument

# c:/Python/tabto4.py
import sys

src = sys.argv[1]
dst = sys.argv[2]

print(src)
print(dst)

results matching ""

    No results matching ""