python_2(编译,变量、引号、变量类型、算数表达式)

一、用于编译pc文件为pyc文件
import py_compile
py_compile.compile(‘hello.py’)

#import py_compile    
#py_compile.compile('hello.py') 是用来编译py为pyc文件
python下编译py成pyc和pyo

其实很简单,
用
python -m py_compile file.py

python -m py_compile /root/src/{file1,file2}.py
编译成pyc文件。

也可以写份脚本来做这事:
Code:

import py_compile
py_compile.compile('path') //path是包括.py文件名的路径

二、python变量

----------------------变量命名---------------------------
# 下面的两个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
----------------------变量赋值---------------------------
# 一次新的赋值操作,将创建一个新的变量
x = 1
print id(x)
x = 2
print id(x)

#print 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()
----------------------全局变量---------------------------
#gl.py
# 全局变量
global _a
_a = 1
_b = 2

#global_variable.py
# 在文件的开头定义全局变量
_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 = 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()

#use_global.py
#调用全局变量
import gl
def fun():
    print gl._a
    print gl._b
fun()
----------------------变量命名---------------------------
#const.py
class _const:
    class ConstError(TypeError): pass
    def __setattr__(self,name,value):
        if self.__dict__.has_key(name):
            raise self.ConstError, "Can't rebind const(%s)"%name
        self.__dict__[name]=value
import sys
sys.modules[__name__]=_const()

#use_const.py
import const
const.magic = 23  
const.magic = 33
----------------------变量类型---------------------------
# 下面的两个i并不是同一个对象
i = 1
print id(i)
i = 2
print id(i)

# 整型
i = 1
print type(i)
# 长整型
l = 9999999990
print type(l)
# 浮点型
f = 1.2
print type(f)
# 布尔型
b = True
print type(b)
# 复数类型
c = 7 + 8j
print type(c)
-------------------单引号和双引号的使用--------------------
# 单引号和双引号的使用是等价
str = "hello world!"
print str
str = 'hello world!'
print str

# 三引号的用法
str = '''he say "hello world!"'''
print str
# 三引号制作doc文档
class Hello:
    '''hello class'''
    def printHello():
        '''print hello world'''
        print "hello world!"
print Hello.__doc__
print Hello.printHello.__doc__

# 转义字符
str = 'he say:\'hello world!\''
print str
# 直接输出特殊字符
str = "he say:'hello world!'"
print str
str = '''he say:'hello world!' '''
print str
----------------------算数表达式---------------------------
# 算术表达式
from __future__ import division

print "1 + 1 =", 1 + 1
print "2 - 1 =", 2 - 1
print "2 * 3 =", 2 * 3
print "4 / 2 =", 4 / 2
print "1 / 2 =", 1 / 2
print "1 / 2 =", 1.0 / 2.0
print "3 % 2 =", 3 % 2
print "2 ** 3 =", 2 ** 3

# 算术运算的优先级
a = 1
b = 2
c = 3
d = 4
print "a + b * c % d =", a + b * c % d 
print "(a + b) * (c % d) =", (a + b) * (c % d) 

# 关系表达式
print 2 > 1
print 1 <= 2
print 1 == 2
print 1 != 2
print 1 <> 2
# 关系表达式的优先级别
print "1 + 2 < 3 - 1 =>", 1 + 2, "<", 3 - 1, "=>", 1 + 2 < 3 - 1
print "1 + 2 <= 3 > 5 % 2 =>", 1 + 2, "<=", 3, ">", 5 % 2, "=>", 1 + 2 <= 3 > 5 % 2

# 逻辑运算符
print not True
print False and True 
print True and False 
print True or False

# 逻辑表达式的优先级别
print "not 1 and 0 =>", not 1 and 0
print "not (1 and 0) =>", not (1 and 0)
print "(1 <= 2) and False or True =>", (1 <= 2) and False or True
print "(1 <= 2) or 1 > 1 + 2 =>", 1 <= 2, "or", 1 > 2, "=>", (1 <= 2) or (1 < 2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值