#1.1 安装Python #1.1.1 Windows #1.1.2 Linux和UNIX #1.1.3 Macintosh #1.1.4 其它公布版 #1.1.5 时常关注。保持更新 #1.2 交互式解释器 D:>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print "Hello, World!" Hello, World! >>> The Spanish Inquisition File "<stdin>", line 1 The Spanish Inquisition ^ SyntaxError: invalid syntax #1.3 算法是什么 #1.4 数字和表达式 >>> 2 + 2 4 >>> 53673 + 235253 288926 >>> 1/2 0 >>> 1.0/2.0 0.5 >>> from __future__ import division >>> 1 / 2 0.5 >>> 1 // 2 0 >>> 1.0 // 2.0 0.0 >>> 1 % 2 1 >>> 10 / 3 3.3333333333333335 >>> 10 % 3 1 >>> 9 / 3 3.0 >>> 9 % 3 0 >>> 2.75 % 0.5 0.25 >>> 2 ** 3 8 >>> 3 ** 2 9 >>> (-3) ** 2 9 #1.4.1 长整数 >>> 1000000000000000000 1000000000000000000L >>> 1000000000000000000L 1000000000000000000L #1.4.2 十六进制和八进制 >>> 0xAF 175 >>> 010 8 #1.5 变量 >>> x = 3 >>> x * 2 6 #1.6 语句 >>> 2 * 2 4 >>> print 2*2 4 >>> x = 3 #1.7 获取用户输入 >>> input("The meaning of life: ") The meaning of life: 42 42 >>> x = input("x: ") x: 34 >>> y = input("y: ") y: 42 >>> print x*y 1428 >>> if 1 == 2: print 'One equals two' ... >>> if 1 == 1: print 'One equals one' ... One equals one >>> time = 120 >>> if time % 60 == 0: print 'On the hour!' ... On the hour! #1.8 函数 >>> 2**3 8 >>> pow(2,3) 8 >>> 10 + pow(2, 3.5)/3.0 13.771236166328254 >>> 10 + pow(2, 3*5)/3.0 10932.666666666666 >>> abs(-10) 10 >>> 1/2 0 >>> round(1.0/2.0) 1.0 #1.9 模块 >>> import math >>> math.floor(32.9) 32.0 >>> int(math.floor(32.9)) 32 >>> from math import sqrt >>> sqrt(9) 3.0 >>> foo=math.sqrt >>> foo(4) 2.0 #1.9.1 cmath和复数 >>> sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error >>> import cmath >>> cmath.sqrt(-1) 1j >>> (1+3j) * (9+4j) (-3+31j) >>> print "Hello, World!" Hello, World! >>> "Hello, world!" 'Hello, world!' >>> "Let's go!" "Let's go!" >>> '"Hello, world!" she said' '"Hello, world!" she said' >>> 'Let's go!本章的新函数' File "<stdin>", line 1 'Let's go。' ^ SyntaxError: invalid syntax >>> 'Let's go!' #1.9.2 回到__future__ #1.10 保存并执行程序 name=raw_input("What is your name?
") print "Hello, " + name + "!" raw_input("Press <enter>") #1.10.1 通过命令提示符执行Python脚本 #1.10.2 让脚本像普通程序一样执行 D:>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. #1.10.3 凝视 >>> import math >>> pi = 3.14 >>> radius = 1.5 >>> print 2 * pi * radius 9.42 >>> # print the girth/perimeter of the circle ... >>> user_name = raw_input("What is your name?
") What is your name?Jonathan >>> print user_name Jonathan #1.11 字符串 >>> print "Hello, world!" Hello, world! #1.11.1 单引號字符串和使引號转义 >>> "Hello, world!" 'Hello, world!' >>> "Let's go!" "Let's go!" >>> '"Hello, world!" she said' '"Hello, world!" she said' >>> 'Let's go!' File "<stdin>", line 1 'Let's go!' ^ SyntaxError: invalid syntax >>> 'Let's go!' "Let's go!" >>> ""Hello, world!" she said" '"Hello, world!" she said' #1.11.2 拼接字符串 >>> "Let's say " '"Hello, world!"' 'Let's say "Hello, world!"' >>> x="Hello," >>> y="wrold!" >>> x+y 'Hello,wrold!' >>> xy Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'xy' is not defined >>> "Hello, " + "world!" 'Hello, world!' #1.11.3 字符串表示。str和repr和反引號 #I guess repr means to reverse an expression >>> "Hello, world!" 'Hello, world!' >>> 10000L 10000L >>> print "Hello, world!" Hello, world! >>> print 10000L 10000 >>> print repr("Hello, world!") 'Hello, world!' >>> print repr(10000L) 10000L >>> print str("Hello, world!") Hello, world! >>> print str(10000L) 10000 >>> print `"Hello, world!"` 'Hello, world!' >>> print `10000L` 10000L >>> temp = 42 >>> print "The temperature is " + temp Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects >>> print "The temperature is " + `temp` The temperature is 42 #1.11.4 input和raw_input的比較 >>> name = input("What is your name?") What is your name?
Jon Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'Jon' is not defined >>> name = input("What is your name?") What is your name?"Jon" >>> print "Hello, " + name + "!" Hello, Jon! >>> input("Enter a number" ") File "<stdin>", line 1 input("Enter a number" ") ^ SyntaxError: EOL while scanning string literal >>> input("Enter a number ") Enter a number 3 3 >>> raw_input("Enter a number ") Enter a number 3 '3' #1.11.5 长字符串、原始字符串和Unicode >>> print '''This is a very long string ... It continues here. ... And it's not over yet. ... "Hello, world!" ... Still here.''' This is a very long string It continues here. And it's not over yet. "Hello, world!" Still here. >>> """You can also use double quotes like this.""" 'You can also use double quotes like this.' >>> print "Hello, ... world!" Hello, world! >>> 1+2+ ... 4+5 12 print 'Hello, world!' Hello, world! >>> print 'Hello, world!' Hello, world! >>> path = 'C: owhere' >>> path 'C: owhere' >>> print path C: owhere >>> print 'C:\nowhere' C: owhere >>> print path 'C:\Program Files\fnord\foo\bar\frozz\bozz' File "<stdin>", line 1 print path 'C:\Program Files\fnord\foo\bar\frozz\bozz' ^ SyntaxError: invalid syntax >>> print 'C:\Program Files\fnord\foo\bar\frozz\bozz' C:Program Filesfnordfooarfrozzozz >>> print r'C: owhere' C: owhere >>> print r'C:Program Filesfnordfooarfrozzozz' C:Program Filesfnordfooarfrozzozz >>> print r'Let's go!' Let's go! >>> print r'This is illegal' File "<stdin>", line 1 print r'This is illegal' ^ SyntaxError: EOL while scanning string literal >>> print r"This is illegal" File "<stdin>", line 1 print r"This is illegal" ^ SyntaxError: EOL while scanning string literal >>> print r'C:Program Filesfooar' '\' C:Program Filesfooar >>> u'Hello, world!' u'Hello, world!' >>> u'你好,世界。' u'u4f60u597duff0cu4e16u754cuff01' >>>
abs(number) 返回数字的绝对值
cmath.sqrt(number) 返回平方根,也适用于负数
float(object) 将字符串和数字转换为浮点数
help() 提供交互式帮助
input(prompt) 获取用户输入
int(object) 将字符串和数字转换为整数
long(object) 将字符串和数字转换为长整数
math.cell(number) 返回数的上入整数,返回值的类型为浮点数
math.floor(number) 返回数的下舍整数,返回值的类型为浮点数
math.sqrt(number) 返回平方根,不适用于负数
pow(x,y[,z]) 返回x的y次幂(所得结果对z取模)
raw_input(prompt) 获取用户输入,返回的类型为字符串
repr(object) 返回该值的字符串表示形式
round(number[,ndidits) 依据给定的精度对数字进行四舍五入
str(object) 将值转换为字符串