1.1 模块的导入

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#规范导入方式
import sys

print sys.path           #利于path的理解,是来自sys
print sys.argv

#不规范导入方式
from sys import path
from sys import argv

print path
print argv

1.2 代码块的分割

#!/usr/bin/python
# -*- coding: UTF-8 -*-

class A:
    def funX(self):
        print "funY()"
#换行
    def funY(self):
        print "funY()"
#换行
if __name__ == "__main__":
    a = A()
    a.funX()
    a.funY()

1.3 注释的使用

# Copyright (C) 2008 bigmarten    *****版权
#
# This program is free software. you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by      *******说明
# the Free Software Foundation
#
########################################################################
#
# Version is 1.0                                                                                                      *******版本
#
# Its contents are calculate payment.
#
########################################################################

# 规范的变量命名
sumPay = 0                                  # 年薪
bonusOfYear = 2000                          # 年终奖金
monthPay = 1200                             # 月薪
sumPay = bonusOfYear + 12 * monthPay        # 年薪 = 年终奖金 + 12 * 月薪


                                                                                      # 注释在调试程序中的作用----通过注释某些行或函数来确定问题
def compareNum(num1, num2):
    if(num1 > num2):
        return str(num1)+" > "+str(num2)
    elif(num1 < num2):
        return str(num1)+" < "+str(num2)
    elif(num1 == num2):
        return str(num1)+" = "+str(num2)
    else:
        return ""
num1 = 2
num2 = 1
print compareNum(num1, num2)
num1 = 2
num2 = 2
print compareNum(num1, num2)
num1 = 1
num2 = 2
print compareNum(num1, num2)

1.4 语句的分隔

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 下面两条语句是等价的
print "hello world!"
print "hello world!";                #也可以加分号 
                                                 # 使用分号分隔语句
x = 1; y = 1; z = 1
                                                 # 一条语句写在多行
print \
"hello world!"
                                                    # 字符串的换行
# 写法一
sql = "select id,name \           #一串换行
from dept \
where name = 'A'"
print sql
# 写法二
sql = "select id,name " \          #分成多串
      "from dept " \
      "where name = 'A' "
print sql

#写法三
sql = '''select id,name \
from dept \
where name = 'A' '''                    #采用 ‘’‘ ’‘’‘
print sql

输出:

hello world!
hello world!
hello world!
select id,name from dept where name = 'A'
select id,name from dept where name = 'A'


1.5 变量和常量

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 下面的两个i并不是同一个对象
i = 1
print id(i)
i = 2
print id(i)
# 正确的变量命名
var_1 = 1
print var_1
_var1 = 2
print _var1
# 错误的变量命名               #需要以字母或下划线开头
1_var = 3
print 1_var
$var = 4
print $var

1.6 变量的赋值

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 一次新的赋值操作,将创建一个新的变量
x = 1
print id(x)
x = 2
print id(x)

#print y                    # y无定义,错误

# 使用元组给多个变量赋值
a = (1, 2, 3)
(x, y, z) = a
print "x =", x
print "y =", y
print "z =", z

# 局部变量
def fun():
    local = 1
    print local
fun()

#调用全局变量

文件一

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 全局变量
global _a                     # 用global 声明
_a = 1
_b = 2

文件二

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#调用全局变量
import gl                     #导入 gl 模块
def fun():
    print gl._a
    print gl._b
fun()

文件三

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 在文件的开头定义全局变量
_a = 1
_b = 2
def add():
    global _a                      #引用全局变量
    _a = 3
    return "_a + _b =", _a + _b
def sub():
    global _b                            #引用全局变量
    _b = 4
    return "_a - _b =", _a - _b
print add()
print sub()                                   # 输出:('_a + _b =', 5)           3+ 2 = 5
                                                                      ('_a - _b =', -1)       3-4 = -1
# 错误的使用全局变量
_a = 1
_b = 2
def add():
    _a = 3
    return "_a + _b =", _a + _b
def sub():
    _b = 4
    return "_a - _b =", _a - _b
print add()
print sub()                                     # 输出:('_a + _b =', 5)               3 +2 =5

                                                                       ('_a - _b =', -3)            1-4 = -3

常量

文件 1

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import const
const.magic = 23  
const.magic = 33   #常量被第二次赋值,会报错

文件 2

class _const:                                                          #定义类
    class ConstError(TypeError): pass                #重复赋值时抛出错误类型
    def __setattr__(self,name,value):                 
        if self.__dict__.has_key(name):                   #检查name是否是常量
            raise self.ConstError, "Can't rebind const(%s)"%name
        self.__dict__[name]=value
import sys
sys.modules[__name__]=_const()


posted on 2022-07-05 18:14  我在全球村  阅读(61)  评论(0编辑  收藏  举报