《简明 Python 教程》笔记-----基础知识

初学Python,零零散散的大概花了三天时间看了《简明Python教程》,整理笔记的过程还是很有意义的。
 
 

《简明 Python 教程》笔记-----面向对象及系统相关 

 
 1、缩进:  就是指块,相当于C里面的{ },对齐就是{ 跟 }匹配

number = 1
while number < 5:
    print number
number += 1
print 'Done'

上面的代码会一直输出1,因为下一句number+=1不在while的范围内

2、运算符与它们的用法
 
3、if else语句,elif
if guess == number:
    print 'a.'
elif guess < number:

    print 'b'
else:

    print 'c' 
 
4、while语句
while number < 5:
    print number
    number += 1
print 'Done'
 
5、for语句,左闭右开
① 、for i in range(0,5)
    这是指i从0到4,
②、for i in range(0,5,2)
   2代表每次加的次数
   指0,2,4
 
6、注意  :一些关键的命令后面要加‘:’,例如:if ,elif,else ,while, for
7、while ,for 后面else可有可无
8、break,continue 跟C一样
9、当想使用外部变量时,用global语句来定义
def func():
    global x
    print 'x is', x
    x = 2
    print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x    
 
10、可设置默认参数值
代码:
def say(message, times = 1):
    print message * times
say('Hello')
say('World', 5)
输出:
Hello
WorldWorldWorldWorldWorld
只有在形参表末尾的那些参数可以有默认参数值,即:def func(a, b=5)有效,def func(a=5, b)无效
 
11、关键参数,就是可以形参默认值,想怎么改就怎么改

输入:

def func(a, b=5, c=10):
print 'a is', a, 'and b is', b, 'and c is', c
func(3, 7)
func(25, c=24)
func(c=50, a=100)

输出:

a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

12、DocStrings 文档字符串

def printMax(x):
    '''Prints the maximum of two numbers.

    The two values must be integers.'''
    x = int(x) 
    print y
printMax(3)
print printMax.__doc__ #调用函数文档

“文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。 强烈建议 你在你的函数中使用文档字符串时遵循这个惯例。”

13、模块

每个.py文件都是一个模块

模块间可互相调用 

 

# Filename: mymodule1.py
def sayhi():
    print 'Hi, this is mymodule speaking.'
version = '0.1'

 

# Filename: mymodule_demo.py
import mymodule1
mymodule1.sayhi()
print 'Version', mymodule1.version

 

# Filename: mymodule_demo2.py
from mymodule import sayhi, version
sayhi()
print 'Version', version

 

demo和demo2都会输出:
Hi, this is mymodule speaking.
Version 0.1

14、可以用dir()函数来列出模块定义的标识符。标识符有函数、类和变量。

15、我们在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符

16、数据结构:

   列表:元素是可变的

       shoplist = ['apple''mango''carrot''banana']

       print  len(shoplist),'

       shoplist.append('rice')

       shoplist.sort()

       for item in shoplist:
       print item,

   元组:元素是不可变的

       zoo = ('wolf''elephant''penguin')
       print 'Number of animals in the zoo is'len(zoo)

       new_zoo = ('monkey''dolphin', zoo)
       print 'Number of animals in the new zoo is'len(new_zoo)
       print 'All animals in new zoo are', new_zoo
       print 'Animals brought from old zoo are', new_zoo[2]
       print 'Last animal brought from old zoo is', new_zoo[2][2]

   字典:删除,插入,查找,遍历

       ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
                'Larry' : 'larry@wall.org',
            'Matsumoto' : 'matz@ruby-lang.org',
              'Spammer' : 'spammer@hotmail.com'
            }
       print "Swaroop's address is %s" % ab['Swaroop']

       ab['Guido'] = 'guido@python.org'

       del ab['Spammer']

       print '%d' % len(ab)
       for name, address in ab.items():
           print 'Contact %s at %s' % (name, address)

       if 'Guido' in ab: 
           print "\nGuido's address is %s" % ab['Guido']

 

   序列:切片操作符,左闭右开

       shoplist = ['apple''mango''carrot''banana']

       print 'Item 1 to 3 is', shoplist[1:3]
       print 'Item 2 to end is', shoplist[2:]
       print 'Item start to end is', shoplist[:]

       name = 'swaroop'
       print 'characters 1 to 3 is', name[1:3]
       print 'characters 2 to end is', name[2:]
       print 'characters start to end is', name[:]

   参考:感觉就是指针

       如果直接复制‘=’一个列表,那是原参数本身,同一块内存

       如果是想重新申请一块相同内存的新的参数,那就得用切片操作符来拷贝  

       shoplist = ['apple', 'mango', 'carrot', 'banana']
       mylist = shoplist
       del shoplist[0]

 

       输出是:

       shoplist is ['mango', 'carrot', 'banana']
       mylist is ['mango', 'carrot', 'banana']

       shoplist = ['apple', 'mango', 'carrot', 'banana']
       mylist = shoplist[:] 
       del shoplist[0]

       输出是:
       shoplist is ['mango', 'carrot', 'banana']
       mylist is ['apple', 'mango', 'carrot', 'banana']

 

   字符串的方法:一些字符串的处理方法,给的不全,有判断开头,查找某一部分,将列表转化成字符串(间隔差值)

 

       name = 'Swaroop' 
       if name.startswith('Swa'):
       print 1
       if 'a' in name:
       print 1
       if name.find('war') != -1:
       print 1

 

       delimiter = '_*_'
       mylist = ['Brazil', 'Russia', 'India', 'China']
       print delimiter.join(mylist)

 

 
posted @ 2012-04-22 17:56  kfinder  阅读(787)  评论(0编辑  收藏  举报