第一章 基础知识 1.1 简单使用和认识 python >>> 2**3 >>> pow(2, 3) >>> 2+8 >>> x = input("x: ") >>> print 'hello world' >>> print "hello world" >>> print 3 >>> print '3' >>> 0xAF 175 >>> 010 8 >>> abs(-10) >>> round(1.0/2.0) // 把浮点数四舍五入为最简介的整数值 1.0 * 模块 >>> import math >>> math.floor(32.9) 32.0 >>> math.sqrt(9) 3.0 >>>hello.py $ python2 hello.py
name = raw_input("What is your name?") print "Hello, " + name + "!"hello2.py $ chmod u+x hello2.py
#!/usr/bin/python2.7 # you should zhushi name = raw_input("What your name ? \n") print "Hello " + name + " !\n"单引号 和 双引号
>>> "Let's go" "Let's go" >>> '"hello world" she said' '"hello world" she said'字符串 (3种方法可以转化之) str, repr, 反引号。 print 与 不用 print
>>> '"hello world" she said' '"hello world" she said' >>> "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 >>>input 与 raw_input 的区别
>>> raw_input("shuru : ") shuru : 6 '6' >>> input("shuru : ") #默认为合法的python表达式 shuru : 5 5 >>>
长字符串, 原始字符串 和 Unicode
>>> print '''asddddddjkhasdjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkaweuwqbxcs''' asddddddjkhasdjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkaweuwqbxcs >>> print """asddddddjkhasdjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkaweuwqbxcs""" asddddddjkhasdjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkaweuwqbxcs >>> print 'C:\nowhere' C: owhere >>> print r'C:\nowhere' C:\nowhere >>> print r'C:\nowhere''\\' # 解决最后一个字符是 '\' 的情况! 完美解决 C:\nowhere\ >>> print u'hello, world' # Unicode 字符串, 在python3.0中,所有的字符串都是 Unicode 字符串 hello, world >>>本章小结
*1 算法
*2 表达式
*3 变量
*4 语句
*5 函数
*6 模块 #例如 : math模块提供了很多数学函数
*7 程序
*8 字符串
本章涉及的新函数 : abs, cmath.sqrt, float(object) 将字符串和数字转换为浮点数, 同 int(Object), long(object), help(), math.ceil(number), pow(x, y[,z]), round, str(object)......