zoukankan      html  css  js  c++  java
  • 笨办法学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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    去除 SQL Server 查询结果中的两边空格
    Ubuntu 中安装 Oracle 10g
    不同格式的下拉列表框
    闲来无趣,写了个简单的JavaScript验证码
    Ubuntu 任务前后台调度管理
    C#数据类型转换,Convert
    OleDbType,C#,access 对应数据类型,互相对应
    SQL 将查询出的表当做 value 插入到表中
    asp.net mvc && asp.net 页面跳转
    asp.net mvc 与 asp.net结合(asp.net mvc 技巧)
  • 原文地址:https://www.cnblogs.com/xdao/p/2740196.html
Copyright © 2011-2022 走看看