源码就是py文件,可以直接执行和访问:
cat 1.py
print ("hello word!")
python 1.py
hello word!
编译成pyc文件
.pyc 文件(字节代码)
vim 11.py
#!/usr/bin/python
import py_compile
py_compile.compile('11.py')
print ("hello word!")
此时会生成一个11.pyc文件,并且是个二进制文件,不能直接访问,但是能运行,
python 1.pyc
hello word!
其中11.py代表当前目录下的11.py文件,将11.py改成二进制文件并且后缀为11.pyc
.pyo文件(优化代码)
[root@geenk01 py]# python -O -m py_compile 11.py
[root@geenk01 py]# ls
11.py 11.pyc 11.pyo 22.py
查看变量文件类型,用type,id代表内存地址。
>>> a=456
>>> id(a)
30152544
>>> type(a)
<type 'int'>
算术运算符
+ - * / // % **
其中的// 表示整除,只取整数。
% 表示只取余数
** 表示乘方,2**3 表示二的三次方
关系运算符
> < >= <= == !=
a=a+2 等于a+=2
逻辑运算符
and 逻辑与: True and False
or 逻辑或:False or True
not 逻辑非:not True
第一个交互运算脚本:
vim a.py
#!/usr/bin/python
a=input("please input num: ")
b=input("please input num2: ")
print ("%s + %s = %s" %(a,b,a+b))
[root@jie py]# python 1.py
please input num: 1
please input num2: 2
1 + 2 = 3