• 笨办法学Python——学习笔记3


        第27-39章流程控制语句和列表,if...else...,while,for
    基本上和c语言类似,不过要注意嵌套的if...else...的关键字是elif
    people = 30
    cars = 40
    buses = 15
    if cars > people:
        print "We should take the cars."
    elif cars < people:#注意此处为elif,不是else if
        print "We should not take the cars."
    else:
        print "We can't decide."

    在如果你的某一行是以 :(冒号,colon)结尾,那就意味着接下来的内容是一个新的代码片段,新的代码片段是需要被缩进的。

    列表(list)就是一个按顺序存放东西的容器,使用 [   ] 括起,如下:

    the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] 
    for number in the_count:#遍历列表 print "This is count %d" % number
    python的列表要比c语言的数组高级,遍历也很方便for后面随便写个变量,这个变量会用来保存列表里的内容,而且python会自动
    遍历列表,把列表里的内容依次放入这个变量里。
    列表的插入使用append(),如the_count.append(6)
    控制范围和步长使用range([start]stop[step]),其中不指定start和step,则分别默认为0、1。
    for i in range(0, 6): print "Adding %d to the list." % i the_count.append(i)
    while循环则和c的类似如下
    i = 0 numbers = [] while i < 6: print "At the top i is %d" % i numbers.append(i) i = i + 1
    第40-44章字典
    python中的字典dict和列表比较像,但本质不同,列表如同c++的vector,字典如同hashmap,使用{ }括起。
    stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2} 冒号前是索引,冒号后是值,不同的索引、值对
    用逗号分割,对于列表和字典的区别可以看看下面个链接。
    http://zhidao.baidu.com/question/304172638.html


    作者:半山
    出处:http://www.cnblogs.com/xdao/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
    使用智能移动设备访问Ossim制
    POJ 3207 Ikki&#39;s Story IV
    AndroidMainifest标签说明2——&lt;activity&gt;
    POJ1149 PIGS 【最大流量】
    POJ3617 Best Cow Line 馋
    颜色(color)转换为三刺激值(r/g/b)(干股)
    关于SQL中的Update语句
    Java Script 正则表达式的使用示例
    Javascript 知识点简介
  • 原文地址:https://www.cnblogs.com/xdao/p/2740196.html
Copyright © 2020-2023  润新知