• python学习笔记


    http://www.imooc.com/video/3370

    http://www.imooc.com/learn/317

    # -*- coding: utf-8 -*-
    print 'hello world'
    print 45678+0x12fd2
    
    x1 = 1
    d = 3
    n = 100
    x100 = x1+(n-1)*d
    s = (x1+x100)*n/2
    print s
    
    print u'你好'
    
    print u'''静夜思
    
    床前明月光,
    疑是地上霜。
    举头望明月,
    低头思故乡。'''
    
    print '''静夜思
    
    床前明月光,
    疑是地上霜。
    举头望明月,
    低头思故乡。'''
    
    print 2.5 + 10.0/ 4
    
    
    sum = 0
    x = 1
    while x<100:
        sum = (1 + 99)*50/2
        x = x + 1
    print sum
    
    sum = 0
    x = 1
    while x<100:
        sum = sum + x
        x = x + 2
    print sum
    
    sum = 0
    x = 1
    n = 1
    while True:
        sum = sum + x
        x = 2 * x
        n = n+1
        if n>20:
            break
    print sum
    
    sum = 0
    x = 0
    while True:
        x = x + 1
        if x > 100:
            break
        if x % 2 == 1:
            sum = sum + x
    print sum
    
    for x in [ '1','2','3','4','5','6','7','8' ]:
        for y in [ '2','3','4','5','6','7','8','9' ]:
            if x < y:
                print x + y
                
    for x in range(1,9):
        for y in range(x+1,10):
            print str(x) + str(y)
            
    d = {
        'Adam': 95,
        'Lisa': 85,
        'Bart': 59
    }
    for (key, value) in d.items():
        print("%s: %s" % (key, value))
        
        d = {
        'Adam': 95,
        'Lisa': 85,
        'Bart': 59
    }
    print 'Adam: 95' 
    print 'Lisa: 85'
    print 'Bart: 59'
    
    d = {
        'Adam': 95,
        'Lisa': 85,
        'Bart': 59
    }
    for foo in d:
        print foo + ":" + str(d.get(foo))
        
    s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
    for foo in s:
        if 'adam' == foo.lower() or 'bart' == foo.lower():
            print 'true'
            
    s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
    for x in s:
        print x[0] + ':' + str(x[1])
        
    s = set(['Adam', 'Lisa', 'Paul'])
    L = ['Adam', 'Lisa', 'Bart', 'Paul']
    for l in L:
        if l in s:
            s.remove(l)
        else:
            s.add(l)
    print s
    
    L = [x*x for x in range(1,101)]
    print sum(L)
    
    def square_of_sum(L):
        sum = 0
        for l in L:
            sum = sum +l*l
        return sum
    print square_of_sum([1, 2, 3, 4, 5])
    print square_of_sum([-5, 0, 5, 15, 25])
    
    import math
    
    def quadratic_equation(a, b, c):
        x = b * b - 4 * a * c
        if x < 0:
            return None
        elif x == 0:
            return -b / (2 *a)
        else:
            return ((math.sqrt(x) - b ) / (2 * a)) , ((-math.sqrt(x) - b ) / (2 * a))
    
    print quadratic_equation(2, 3, 0)
    print quadratic_equation(1, -6, 5)
    
    def fact(n):
        if n==1:
            return 1
        return n * fact(n - 1)
    
    fact(5)
    # -*- conding=utf-8 -*-
    
    import math
    
    def add(x, y, f):
        return f(x) + f(y)
    
    print add(25, 9, math.sqrt)
    
    
    #def format_name(s):
    #    return s[0].upper()+s[1:].lower()
    
    #print map(format_name, ['adam', 'LISA', 'barT'])
    
    
    def format_name(s):
        return s.capitalize()
    
    print map(format_name, ['adam', 'LISA', 'barT'])
    
    def prod(x, y):
        return x*y
    
    print reduce(prod, [2, 4, 5, 7, 12])
    
    
    import math
    
    def is_sqr(x):
        return math.sqrt(x) % 1 == 0
    
    print filter(is_sqr, range(1, 101))
    
    
    def cmp_ignore_case(s1, s2):
        if s1.lower() > s2.lower():
            return 1
        elif s1.lower() < s2.lower():
            return -1
        else:
            return 0
    
    print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
    
    
    def calc_prod(lst):
        def g(x,y):
            return x*y
        def lazy_prod():
            return reduce(prod,lst)
        return lazy_prod
    
    f = calc_prod([1, 2, 3, 4])
    print f()
    
    #用debug调试解析他们的例子
    def count():
        fs = []
        for i in range(1, 4):
            def f():
                 return i*i
            fs.append(f)
        return fs
    
    f1, f2, f3 = count()    #这一步进入循环 i=1,2,3 分别为f1,f2,f3函数开辟内存空间
    print f1()  #而到了这一步,不进入上面的循环了,他直接计算了,用的是最后一个i=3的值计算
    print f2()  #同上
    print f3()  #同上
    
    #def count():
    #    fs = []
    #    for i in range(1, 4):
    #        def f(m = i):
    #            return m*m
    #        fs.append(f)
    #    return fs
    
    #f1, f2, f3 = count()
    #print f1(), f2(), f3()
    # -*- coding=utf-8 -*-
    '''
    Created on 2017年6月19日
    
    @author: ScvQ
    '''
    def fact(n):
        if n==1:
            return 1
        return n * fact(n - 1)
    
    print fact(5)
    
    def greet(t='world.'):
        if t == 'world.':
            return 'hello,'+t
        else:
            return 'hello,xxx.'
    print greet()
    print greet('Bart')
    
    def average(*args):
        sum = 0
        return (sum + i for i in args)/args.length    
    
    #print average()
    #print average(1, 2)
    #print average(1, 2, 2, 3, 4)
    
    L = range(1, 101)
    
    print L[:10]
    print L[2:100:3]
    print L[4:50:5]
    
    L = ['a','b','c','d']
    print L[-2:]
    print L[:-2]
    print L[-3:-1]
    
    L = range(1, 101)
    print L[-10:]
    print L[4::5][10:]
    
    def firstCharUpper(s):
        return s[0].upper()+s[1:]
    
    print firstCharUpper('hello')
    print firstCharUpper('sunday')
    print firstCharUpper('september')
    
    L = ['Adam', 'Lisa', 'Bart', 'Paul']
    for index, name in enumerate(L):
        print index+1, '-', name
        
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
    
    sum = 0.0
    for v in d.itervalues():
        sum = sum + v
    print sum/len(d)
    
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
    
    sum = 0.0
    for k, v in d.items():
        sum = sum + v
        print k, ':', v
    print 'average', ':', sum/len(d)
    
    print [x*(x+1) for x in range(1,101,2)]
    
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()]
    print '<table>'
    print '<tr><th>Name</th><th>Score</th><tr>'
    print '
    '.join(tds)
    print '</table>'
    
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    def generate_tr(name, score):
        if(score<60):
            return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
        else:
            return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
    
    tds = [generate_tr(name, score) for name, score in d.iteritems()]
    print '<table border="1">'
    print '<tr><th>Name</th><th>Score</th><tr>'
    print '
    '.join(tds)
    print '</table>'
    
    def toUppers(L):
        return [x.upper() for x in L if isinstance(x,str)]
    
    print toUppers(['Hello', 'world', 101])
    
    print [ m + n + o for m in '123456789' for n in '0123456789' for o in '123456789' if m == o]
    
    print [ 100*m+10*n+o for m in range(1,10) for n in range(10) for o in range(1,10) if m == o]
    # -*- coding=utf-8 -*-
    '''
    Created on 2017年6月19日
    
    @author: ScvQ
    '''
    def fact(n):
        if n==1:
            return 1
        return n * fact(n - 1)
    
    print fact(5)
    
    def greet(t='world.'):
        if t == 'world.':
            return 'hello,'+t
        else:
            return 'hello,xxx.'
    print greet()
    print greet('Bart')
    
    def average(*args):
        sum = 0
        return (sum + i for i in args)/args.length    
    
    #print average()
    #print average(1, 2)
    #print average(1, 2, 2, 3, 4)
    
    L = range(1, 101)
    
    print L[:10]
    print L[2:100:3]
    print L[4:50:5]
    
    L = ['a','b','c','d']
    print L[-2:]
    print L[:-2]
    print L[-3:-1]
    
    L = range(1, 101)
    print L[-10:]
    print L[4::5][10:]
    
    def firstCharUpper(s):
        return s[0].upper()+s[1:]
    
    print firstCharUpper('hello')
    print firstCharUpper('sunday')
    print firstCharUpper('september')
    
    L = ['Adam', 'Lisa', 'Bart', 'Paul']
    for index, name in enumerate(L):
        print index+1, '-', name
        
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
    
    sum = 0.0
    for v in d.itervalues():
        sum = sum + v
    print sum/len(d)
    
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59, 'Paul': 74 }
    
    sum = 0.0
    for k, v in d.items():
        sum = sum + v
        print k, ':', v
    print 'average', ':', sum/len(d)
    
    print [x*(x+1) for x in range(1,101,2)]
    
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()]
    print '<table>'
    print '<tr><th>Name</th><th>Score</th><tr>'
    print '
    '.join(tds)
    print '</table>'
    
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    def generate_tr(name, score):
        if(score<60):
            return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score)
        else:
            return '<tr><td>%s</td><td>%s</td></tr>' % (name, score)
    
    tds = [generate_tr(name, score) for name, score in d.iteritems()]
    print '<table border="1">'
    print '<tr><th>Name</th><th>Score</th><tr>'
    print '
    '.join(tds)
    print '</table>'
    
    def toUppers(L):
        return [x.upper() for x in L if isinstance(x,str)]
    
    print toUppers(['Hello', 'world', 101])
    
    print [ m + n + o for m in '123456789' for n in '0123456789' for o in '123456789' if m == o]
    
    print [ 100*m+10*n+o for m in range(1,10) for n in range(10) for o in range(1,10) if m == o
  • 相关阅读:
    Action<T>和Func<T>委托
    异步委托学习笔记
    .Net身份验证里边的几个基本概念(转)
    线程和进程
    WebClient类的使用
    关于NHibernate的更新和读取操作,及碰见的一点问题
    ASP.NET MVC 中将数据从View传递到控制器中的三种方法(表单数据绑定)
    LINQ标准查询操作符学习笔记
    C#3.0中的扩展方法
    NHibernate中的一对多映射
  • 原文地址:https://www.cnblogs.com/ScvQ/p/7048846.html
Copyright © 2020-2023  润新知