python 基础笔记

来源:互联网 发布:淘宝助理快递单模板 编辑:程序博客网 时间:2024/05/22 05:55
源代码->字节码->机器语言    解释器4种类型的数--整数,长整数,浮点数,复数#常见的类 :str#ifif guess == number:print 'do sth', #在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符elif guess < number:print 'do sth'elif True:print 'do sth'else:print 'do sth'没有switch 有时候使用数据结构 字典 会更快捷#while ...elsewhile running:...else:print '可选的else从句'#for ...infor i in range(1,5): ==> in [1,2, 3, 4] #任何种类的由任何对象组成的序列,range 还可以设置步长print i#用来替代for(int i = 0; i<5 ; i++)continuebreakelse:print '可选的else'布尔类型并不是真实的01#函数定义def funname(apple, guess = 1 ,times = 2):#列表末尾才能默认参数赋值print aplle * times #会打印出几次apple的字符串global x #表明这里使用的x是全局的,这只是个声明作用....函数体return y'可选,没有的 默认等价于 'return None (None 特殊类型) 可以 print funname()打印出值调用funname(times=3, apple = 2, 8)#关键参数 这里的8到底赋值给了谁?'''关键字参数与字典。如果换一个角度看待你在函数中使用的关键字参数的话,你已经使用了字典了!只需想一下——你在函数定义的参数列表中使用的键/值对。当你在函数中使用变量的时候,它只不过是使用一个字典的键(这在编译器设计的术语中被称作 符号表 )。'''pass #空的语句块DocStringsdef  fun()若干代码....'''Instruction Of The Code.......'''print fun.__doc__ #打印函数中的 '''之间的部分 (fun函数的 文档字符串 属性)模块import sysprint sys.path#它在sys.path变量中所列目录中寻找sys.py模块。如果找到了这个文件,这个模块的主块中的语句将被运行,然后这个模块将能够被你 使用 。注意,初始化过程仅在我们 第一次 输入模块的时候进行????.pyc 文件(字节编译文件)from sys import argv #部分插入from sys import *  #一般应该避免这种写法 以免名称的冲突模块的__name__if __name__ == '__main__':''' 是这个则被用户单独运行'''    print 'This program is being run by itself'else:    print 'I am being imported from another module' '''如import 文件名(没有py)这般调用 '''dir(sys) 列出模块定义的标识符 和 dir(); del用于删除变量/名称 可以是数据结构 del shoplist[0]数据结构三种内建的数据结构——列表、元组和字典#列表listshoplist =  ['apple', 'pear', 'banana']help(list)得到列表对象的所有方法#元组tuple(不可变) 用于给人提供安全的值 类似于constzoo = ('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_zooprint 'Animals brought from old zoo are', new_zoo[2]print 'Last animal brought from old zoo is', new_zoo[2][2] #new_zoo[2]是zoo 然后再zoo[2]singleton = (2 ,)#单个元素的元组,加逗号用于区分 和表达式 (2)的区别singleton =  ()#空元组age = 22name = 'Swaroop'print '%s is %d years old' % (name, age)# 最后的%是定制格式print 'Why is %s playing with that python?' % name #使用了一个定制,后面跟着%符号后的单个项目——没有圆括号。这只在字符串中只有一个定制的时候有效。#字典dictab = {       'Swaroop'   : 'swaroopch@byteofpython.info',             'Larry'     : 'larry@wall.org',             'Matsumoto' : 'matz@ruby-lang.org',             'Spammer'   : 'spammer@hotmail.com'     }for name, address in ab.items():    print 'Contact %s at %s' % (name, address)if 'Guido' in ab: # OR ab.has_key('Guido')    print "\nGuido's address is %s" % ab['Guido']键/值 是没有顺序的,若需要,用前记得排序列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符[]和切片[:]操作符shoplist = ['apple', 'mango', 'carrot', 'banana']# Indexing or 'Subscription' operationprint 'Item 0 is', shoplist[-1]#banana# Slicing on a stringname = 'swaroop'print 'characters 1 to 3 is', name[1:3]#waprint 'characters 2 to end is', name[2:]#aroopprint 'characters 1 to -1 is', name[1:-1]#warooprint 'characters start to end is', name[:]#swaroop序列的神奇之处在于你可以用相同的方法访问元组、列表和字符串mylist = shoplist # mylist is just another name pointing to the same object!#若要copy则用切片操作常见的类和方法delimiter = '_*_'mylist = ['Brazil', 'Russia', 'India', 'China']print delimiter.join(mylist) #Brazil_*_Russia_*_India_*_China