id() -- 可以查看变量指向内存的地址
//进入python界面 [root@localhost test1]# ipython Python 2.6.6 (r266:84292, Nov 22 2013, 12:11:10) Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: a = 123 In [2]: id(a) Out[2]: 135958416 In [3]: a =456 In [4]: id(a) Out[4]: 139897836
赋值为字符串,需要用 单引号 ''
type() --可以查看此变量是什么类型的
In [5]: 4/3 Out[5]: 1 In [6]: 4.0/3 Out[6]: 1.3333333333333333 --得到小数 In [7]: 4.0//3 Out[7]: 1.0 --整除 (只得整数) In [8]: 19%2 Out[8]: 1 --取余 In [9]: 2**3 Out[9]: 8 --乘方 表示 2^3
In [10]: 1 >2 Out[10]: False --不成立 则返回false In [11]: 1< 2 Out[11]: True
In [13]: 1==2 and 2<1 Out[13]: False In [14]: 1==2 or 2>1 Out[14]: True In [15]: not 1==1 Out[15]: False
====================================
Tranning:
写一个四则运算器
1.要求从键盘读取数字
2. 分清楚input() and raw_input()的区别
/* 1. input() */ In [16]: input("please input: ") please input: 11 Out[16]: 11 In [17]: input("please input: ") please input: abc --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-17-dd86387934e0> in <module>() ----> 1 input("please input: ") <string> in <module>() NameError: name 'abc' is not defined /* 出现错误~~ 因为不是输入数字,输入字符串要加入单引号或双引号。 */ In [18]: input("please input: ") please input: 'abc' Out[18]: 'abc' =========================================== /* 2. raw_input() */ In [19]: raw_input("please input: ") please input: sdjhsjh Out[19]: 'sdjhsjh' In [20]: raw_input("please input: ") please input: 234 Out[20]: '234'
[root@localhost test1]# vi 3.py //ADD #!/usr/bin/python num1 = input("Please input a number: ") num2 = input("Please input a number: ") print "%s + %s = %s" % (num1, num2, num1+num2) print "%s - %s = %s" % (num1, num2, num1-num2) print "%s * %s = %s" % (num1, num2, num1*num2) print "%s / %s = %s" % (num1, num2, num1/num2) [root@localhost test1]# python 3.py Please input a number: 2 Please input a number: 3 2 + 3 = 5 2 - 3 = -1 2 * 3 = 6 2 / 3 = 0