为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
初识Python_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

初识Python

廖雪峰 移动开发工程师
难度入门
时长 5小时 0分
  • d={}

    d[key] = value  // 更新插入字典

    查看全部
    0 采集 收起 来源:Python更新dict

    2018-10-27

  • s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
    for x in s:
     print '%s : %d' %x
     print x[0],":",x[1]

    查看全部
    0 采集 收起 来源:Python之 遍历set

    2018-10-27

  • d = {key:value}

    d.get(key)

    get[key]


    查看全部
    0 采集 收起 来源:Python之访问dict

    2018-10-27

  • #items()方法以列表返回一组(键:值)

    查看全部
  • 函数可以返回多值

    math包在引用sin cos时 先用import引用     

        import  math

    import math
    def move(x, y, step, angle):
        nx = x + step * math.cos(angle)
        ny = y - step * math.sin(angle)
        return nx, ny
    >>> x, y = move(100, 100, 60, math.pi / 6)
    >>> print x, y
    151.961524227 70.0

    但这两个返回值其实时一个tuple

    >>> r = move(100, 100, 60, math.pi / 6)
    >>> print r
    (151.96152422706632, 70.0)

    但是,在语法上,返回一个tuple可以省略括号,而多个变量可以同时接收一个tuple,按位置赋给对应的值,所以,Python的函数返回多值其实就是返回一个tuple,但写起来更方便。

    查看全部
  • >>> [m + n for m in 'ABC' for n in '123']
    ['A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3']

    同:

    L = []for m in 'ABC':
        for n in '123':
            L.append(m + n)


    查看全部
    0 采集 收起 来源:多层表达式

    2018-10-27

  •  return sum([i * i for i in L])   #L=[1,2,3]

    相当于return sum([1,4,9])~~return sum(L)


    查看全部
  •  for 循环后面还可以加上 if 判断。

    >>> [x * x for x in range(1, 11) if x % 2 == 0]
    [4, 16, 36, 64, 100]


    查看全部
    0 采集 收起 来源:条件过滤

    2018-10-27

  • 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 '\n'.join(tds)
    print '</table>'


    查看全部
    0 采集 收起 来源:复杂表达式

    2018-10-27

  • >>> range(1, 11)
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> [x * x for x in range(1, 11)]
    [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


    查看全部
    0 采集 收起 来源:生成列表

    2018-10-27

  • dict 对象的 items() 方法返回的值:

    >>> d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    >>> print d.items()
    [('Lisa', 85), ('Adam', 95), ('Bart', 59)]
    >>> for key, value in d.items():
    ...     print key, ':', value
    ... 
    Lisa : 85
    Adam : 95
    Bart : 59


    查看全部
  • 做判断的时候最好直接在要判断的数的地方进行
    ,中间不要加代码块
    查看全部
  • d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    print d.values()
    # [85, 95, 59]for v in d.values():    print v# 85# 95# 59
    d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 }
    print d.itervalues()
    # <dictionary-valueiterator object at 0x106adbb50>for v in d.itervalues():
        print v
    # 85
    # 95
    # 59


    查看全部
    0 采集 收起 来源:迭代dict的value

    2018-10-27

  • enumerate() 函数:

    >>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
    >>> for index, name in enumerate(L):...     print index, '-', name
    ... 
    0 - Adam
    1 - Lisa
    2 - Bart
    3 - Paul


    查看全部
    0 采集 收起 来源:索引迭代

    2018-10-27

  • sum = 0
    x = 1
    n = 1
    y = 0
    while True:
        sum = ( x + n)**y
        y += 1
        if y > 20:
            break
    print sum - 1

    查看全部

举报

0/150
提交
取消
课程须知
如果您了解程序设计的基本概念,会简单使用命令行,了解中学数学函数的概念,那么对课程学习会有很大的帮助,让您学起来得心应手,快速进入Python世界。
老师告诉你能学到什么?
通过本课程的学习,您将学会搭建基本的Python开发环境,以函数为基础编写完整的Python代码,熟练掌握Python的基本数据类型以及list和dict的操作。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!