1.从用户那里得到输入最容易的方法就是使用raw_input函数,
[root@oracle11g ~]# python
Python 2.6.6 (r266:84292, May 1 2012, 13:52:17)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> user = raw_input('Enter login name:')
Enter login name:root
>>> print 'Your login is:',user
Your login is: root
2.int()将数值转化成整数类型
>>> num = raw_input('Now enter a number:')
Now enter a number:1024
>>> print 'Doubling your number:%d' % (int(num)*2)
Doubling your number:2048
3.help函数查看内建函数信息
>>> help(raw_input)
Help on built-in function raw_input in module __builtin__:
raw_input(...)
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled. The prompt string, if given,
is printed without a trailing newline before reading.
4.python运算符
+ - * / // % **
/是传统的除法
//是取模
**是乘法运算符
运算符优先级别:
+和-的优先级最低,其次*,/,//,%再次单目运算符+和-,最后**的运算符最高。
5.python比较运算符
< <= > >= == != <>
返回的是布尔值,例如:
[root@oracle11g ~]# python
Python 2.6.6 (r266:84292, May 1 2012, 13:52:17)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 > 3
False
>>> 2 < 3
True
6.python逻辑运算符
and or not
可以和任意表达式连接在一起,并得到一个布尔值,例如
>>> 2 > 3 and 3 < 4
False
>>> 2 < 3 and 3 < 4
True
为提高程序的可阅读性,Python建议采用()。
7.python变量
>>> counter = 0
>>> miles = 1000.0
>>> name = 'Bob'
>>> counter += 1
>>> kilometers = 1.69 * miles
>>> print '%f miles is the same as %f km' % (miles, kilometers)
1000.000000 miles is the same as 1690.000000 km
依次是整数、浮点、字符、整数加1、浮点乘法。
8.数字
int:有符号整数
long:长整数
bool:布尔值
float:浮点值
complex:复数
decimal:十进制浮点数
9.字符串
>>> pystr = 'Python'
>>> iscool = 'is cool!'
>>> pystr[0]
'P'
>>> pystr[2:5]
'tho'
>>> iscool[:2]
'is'
>>> iscool[3:]
'cool!'
>>> iscool[-1]
'!'
>>> pystr + iscool
'Pythonis cool!'
>>> pystr + '' +iscool
'Pythonis cool!'
>>> pystr + ' ' +iscool
'Python is cool!'
>>> pystr * 2
'PythonPython'
>>> pystr
'Python'
>>> _ * 5
'PythonPythonPythonPythonPython'
10.数组
>>> aList = [1, 2, 3, 4]
>>> aList
[1, 2, 3, 4]
>>> aList[0]
1
>>> aList[2:]
[3, 4]
>>> aList[3:]
[4]
>>> aList[:3]
[1, 2, 3]
>>> aList[1] = 5
>>> aList
[1, 5, 3, 4]
阅读(1145) | 评论(0) | 转发(0) |