跟着tutorial学python3.3

'''
# =============================================================================
#      FileName: test.py
#          Desc: 
#        Author: Anduo
#         Email: [email protected]
#      HomePage: http://anduo.iteye.com
#       Version: 0.0.1
#    LastChange: 2013-05-15 20:47:32
#       History: python version is 3.3
# =============================================================================
'''
radius = 10
pi = 3.14
area = pi*radius**2
print("the area is", area)

# 3.1.2 Strings examples
'spam eggs' # print 'spam eggs'
'doesn\'t'  # print "doesn't"
' "Yes," he said .' # print ' "Yes," he said . '

hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
Note that whitespace at the beginning of the line is\
significant."
print(hello)

word = 'Hello' + 'A'
print(word) # result is 'HelpA'

'<' + word * 5 + '>'
# '<HelpAHelpAHelpAHelpAHelpA>'

'str' 'ing'
# 'string'
'str'.strip() + 'ing'
# 'string'

# +---+---+---+---+---+
# | H | e | l | p | A |
# +---+---+---+---+---+
# 0   1   2   3   4   5
#-5  -4  -3  -2  -1  

# 3.1.4  lists
a = ['spam', 'eggs', 100, 1234]

# 3.2 first steps towards programming
a, b = 0, 1
while b < 10:
    print(b, end=',')
    a, b = b, a+b

# 4.1 if statements
x = int(input('Please enter an integer:'))
if x < 0:
     x = 0
     print('Negative changed to zero')
elif x == 0:
     print('zero')
elif x == 1:
     print('Single')
else:
     print('More')
#end

# 4.2 for statements
words = ['cat','window','defenestrate']
for w in words:
    print(w, len(w))

for w in words[:]: # Loop over a slice copy of the entire list.
    if len(w) > 6:
        words.insert(0,w)

# 4.3 The range() Function
for i in range(5):
    print(i)

range(5, 10)
#5 through 9
range(0, 10, 3)
#0, 3, 6, 9
range(-10, -100, -30)
#-10, -40, -70

a = ['Mary','had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print(i,a[i])

# 4.4 break and continue Statements,and else Clauses on Loops
for n in range(2, 10):
    for x in range(2,n):
        if n % x == 0:
            print(n,'equals',x,'*',n//x)
            break
        else:
            # loop fell through without finding a factor
            print(n,'is a prime number')

fom num in range(2, 10):
    if num % 2 == 0:
        print("Found an even number", num)
        continue
    print("Found a number", num)

# 4.5 pass statements
while True:
    pass # Busy-wait for keyboard interrupt (Ctrl + C)

# This is commonly used for creating minimal classes:
class MyEmptyClass:
    pass

def initlog(*args):
    pass # Remember to implement this!

# 4.6 Defining Function
def fib(n): # write Fibonacci series up to n
    """Print a Fibonacci series up to n."""
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b
    print()

fib(2000)

def fib2(n):# return Fibonacci series up to n
    """result a list containing the Fibonacci series up to n."""
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a+b
    return result

# 4.7
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise IOError('refusenik user')
        print(complaint)


def concat(*args, sep="/"):
    return sep.join(args)

4.7.5 Lambda Forms

def make_incrementor(n):
    return lambda x: x + n

#--------------------------------#
# 笔记                           #
# by Anduo 2013-05-13            #
#--------------------------------#
#跟着【tutorial.pdf】学了前四个章节,
#python的优势在于快速的脚本开发,真的太快了。
#真要有python环境,写写脚本,马上可以看到效果.
#一个Fibonacci 数列 几行代码就搞定了。真的很好。
def fib(n): # write Fibonacci series up to n
    """Print a Fibonacci series up to n."""
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a + b
    print()

 

你可能感兴趣的:(python3)