zoukankan      html  css  js  c++  java
  • Python基础--1(语法基础)

    1)数据交换:

    C语言中:temp=x;

                 x=y;

         y=temp

    Python中:x,y=y,x

    2) 典型的Python文件结构

     1 #/usr/bin/env python             #(1)起始行
     2 
     3 “this is a test module”          #(2)模块文档
     4 
     5 import sys                       #(3)模块导入
     6 import os
     7 
     8 debug=True                       #(4)(全局)变量定义
     9 
    10 class FooClass(object):          #(5)类定义(若有)
    11     "Foo class"
    12     pass
    13 
    14 def test():                      #(6)函数定义(若有)
    15     "test function"
    16     foo=FooClass()
    17     if debug:
    18         print('ran test()')
    19 
    20 if(__name__=='__main__'):      #(7)主程序
    21     test()

     3) 内存管理:

    a.变量无需事先声明;变量在第一次被赋值时自动声明

    b.变量无需指定类型

    c.我们不用关心内存管理

    d.变量名会被’回收‘的

    e.del语句能直接释放资源

    4)增加引用计数有:

    a.对象被创建:x=3.14

    b.另外的别名被创建:y=x

    c.作为参数传递给函数(新的本地引用):foobar(x)

    d.成为容器对象的一个元素:mylist=[123,x,'xyz']

    5)减少引用计数:

    a.一个本地引用离开了其作用范围;比如foobar(x)函数结束时:

    b.del y  

    c.对象的一个别名被赋值给其它对象:

    1 x=3.14
    2 y=x
    3 x=123

    d.对象被从一个窗口对象中移除:mylist.remove(x)

    e.窗口对象本身被销毁:del mylist

  • 相关阅读:
    bzoj1415 NOI2005聪聪和可可
    Tyvj1952 Easy
    poj2096 Collecting Bugs
    COGS 1489玩纸牌
    COGS1487 麻球繁衍
    cf 261B.Maxim and Restaurant
    cf 223B.Two Strings
    cf 609E.Minimum spanning tree for each edge
    cf 187B.AlgoRace
    cf 760B.Frodo and pillows
  • 原文地址:https://www.cnblogs.com/yuzaihuan/p/6391917.html
Copyright © 2011-2022 走看看