VB老鸟初学Python(Python for kids)

来源:互联网 发布:电脑一般装什么软件 编辑:程序博客网 时间:2024/06/03 21:46

1. 多行文本, 使用三个单引号括起

2. 语句,复合语句使用冒号,比如

 if a > b :

    print("big")

  else :

    print("small")

3. 语句块,缩进一致就是同一block,比如:

 if a > b :

    print("big")

    print("yes, goold")

  else :

    print("small")

    print("no good")

4. 列表: a=[1,3,5,7,9]

                b=['a','b','c']

    列表元素: a[1]

    列表片断: a[2:4]

    列表操作: a.append(11,13) ,  del a[3], a + b, 5*a (重复5次)

5. 元组: 数据不变的列表,用的是小括号。 a=(1,3,5,7,9) 

6. 字典: a={Key1:value1, Key2:value2, .....}

    字典元素: a[Key]

7. 画图:

   导入模块: import turtle

   创建画笔: t=turtle.Pen();

   移动: t.forward( 像素大小); t.backward(像素大小)

   改变方向: t.left(转动度数), right(转动度数)

   收起画笔: t.up(), 放下画笔(继续画画): t.down()

   清除画笔所画内容:t.clear()

   重置画布: t.reset()

   设置海龟面向指定的方向:t.setheading(角度)

   设置画笔的颜色:color

   设置填色: begin_fill(), end_fill()

   画圆形:circle(半径)

8. 循环: for x in range(0,5) : 语句

                 while 条件

    退出循环: break

9. print中字符串参数的替代: print("this is %s for %s", %(i,j))

10. 定义函数: def 函数名(参数1,参数2,...) :

                              语句体

                             [return 返回值]

      调用函数: 函数名(参数1,参数2,...)


11.导入模块: import 模块名

          读命令行的模块: sys

                                           命令: sys.stdin.readline()


12. 类的定义: class 类名 :
                              语句块
                              def  __init__(self, 参数) :
                                 self.属性 = 参数
                              def 函数名(self) :
                                 函数语句块
                              pass
           self主要用来,从类中的一个函数调用类中的另一个函数。
      继承父类: class 类名(父类名) :
                              语句块
                              pass

      子类可以调用父类的函数
      实例可以在后来再设置属性,即不需要在类定义中设置属性。
13. None指无值
14. 内建函数: abs, bool, dir, help, eval, exec, float, int, len, max, min, range, sum
15. 打开文件:open(文件全名,['w']) ,比如: fp=open('c:\\test.txt')
      读文件: read(), 比如: strData=fp.read()
      写文件: write(内容), 比如: fp.write('I am a good boy')
      关闭文件: close(),比如:fp.close()
16. Python模块:
       copy, 用于拷贝对象, copy, deepcopy
       keyword,记录了所有的关键字, iskeyword, kwlist
       random,获得随机数, randint(最少值,最大值), choice, shuffe, randrange
       sys,控制shell程序, exit, stdin, stdout, version
       time,用来得到时间, time(),返回1970.1.1以来的秒数, asctime([参数]),localtime,struct_time, sleep(延迟数)
       pickle,保存信息,dump(数据,文件名), load(文件名)

17. 注释用井号:#
     
0 0