字节代码
[root@ftp day01]# vi 2.py
1 #!/usr/bin/python
2 import py_compile
3 py_compile.compile('./1.py')
root@ftp day01]# python 2.py
[root@ftp day01]# ll
total 12
-rwxr-xr-x 1 root root 38 Nov 28 11:35 1.py
-rw-r--r-- 1 root root 114 Nov 28 11:38 1.pyc
-rw-r--r-- 1 root root 65 Nov 28 11:38 2.py
[root@ftp day01]# python 1.pyc
hello world
优化了的字节代码文件 1.pyo
[root@ftp day01]# python -O -m py_compile 1.py
[root@ftp day01]# ll
total 16
-rwxr-xr-x 1 root root 38 Nov 28 11:35 1.py
-rw-r--r-- 1 root root 114 Nov 28 11:38 1.pyc
-rw-r--r-- 1 root root 112 Nov 28 11:48 1.pyo
-rw-r--r-- 1 root root 65 Nov 28 11:38 2.py
[root@ftp day01]# python 1.pyo
hello world
以上都是把源码编译为2进制了
给变量重新赋值,指向内存另外区域
In [1]: a =123
In [2]: id(a)
Out[2]: 12930848
In [3]: a = 456
In [4]: id(a)
Out[4]: 18816536
变量根据后面的值,自动确定类型
In [5]: x = 2
In [6]: type(x)
Out[6]: int
In [7]: x = '2'
In [8]: type(x)
Out[8]: str
计数器
[root@ftp day01]# vi 3.py
1 #!/usr/bin/python
2 num1 = input("Please a number: ")
3 num2 = input("Please a number: ")
4
5 print "%s + %s = %s" % (num1,num2,num1+num2)
6 print "%s - %s = %s" % (num1,num2,num1-num2)
7 print "%s * %s = %s" % (num1,num2,num1*num2)
8 print "%s / %s = %s" % (num1,num2,num1/num2)
[root@ftp day01]# python 3.py
Please a number: 8
Please a number: 4
8 + 4 = 12
8 - 4 = 4
8 * 4 = 32
8 / 4 = 2
#字典
[root@ftp day01]# vim 4.py
1 #!/usr/bin/python
2 info = {}
3 name = raw_input("please input name: ")
4 age = raw_input ("please input age: ")
5 gender = raw_input("please input (M/F): ")
6 info['name'] = name
7 info['age'] = age
8 info['gender'] = gender
9 for i in info.items():
10 print i
11
12 print '-'*20
13 for k,v in info.items():
14 print "%s: %s" %(k,v)
15 print "Main end"
~
~
[root@ftp day01]# python 4.py
please input name: Make
please input age: 28
please input (M/F): M
('gender', 'M')
('age', '28')
('name', 'Make')
--------------------
gender: M
age: 28
name: Make
Main end
阅读(1447) | 评论(0) | 转发(0) |