python 学习笔记(自家用)

本文主要介绍Python基础语法,包含变量定义、if判断、while和for循环、函数定义与使用,还涉及模块导入(如sys)、从屏幕接收数据、类的声明与继承、文件操作及序列化,同时提及异常机制,强调sys和os包功能强大。

number = 23;#定义变量
guess = 22;
if number == guess :#if判断,注意:不能少
    print 'yse';    #注意缩进在python中缩进只有在包含进去的时候才可以使用
elif number > guess: #注意不是else if 而是elif
    print 'big';
else :
    print 'no';
run=True;
i=0;
while i>3:  #while循环同样注意:
    print "loop";
    print i;
    i=i+1;
    if i==5:
        run=False;
else:   #如果条件不满足的使用运行的语句
    print "not while !";
for i in range(1,5): #for循环  range表示从1开始到5结束
    print i;
    #break;  #break结束
    continue;  #跳过此次循环
else:   #结束循环时候执行的语句
    print "end";
def name():  #函数定义
    print "hello!";
    #function end #不用结束
name();   #函数使用
x=50;

#def echo(x):
#    print x;
#    x=2;  #局部变量函数内有效
#   return x;
#echo(x);

def echo():
    global x  #使用全局变量
    print x;
    x=2;
    return x;
echo();
print echo.__doc__,x; #多个变量输出使用,分隔
def Null() :
    '''asdfasdfsdfasdf #函数注释,我是这样理解的
    adfadfasdfasdfasdfa'''#注释结束,‘’‘中间的内容可以分成多行’‘’
    pass;# 代表函数没有语句
print Null();  #如果函数为空的话输出 none
#print none.__doc__; #输出函数的注释(我理解的那段)

##################################################
import sys; #使用系统的sys模块
 print 'The command line arguments are:'
 for i in sys.argv:  #输出系统的参数
  print i

print '/n/nThe PYTHONPATH is', sys.path, '/n'#输出系统中的环境变量
#注意模块的字符编码最好采用pyc的文件名,加快加载时候的速度,先进行一部分编译
print __name__;#模块名称,一般在判断是不是主模块的时候使用,主模块的名称是__main__
##################################################

raw_input('input:');#从屏幕接受数据,并且有输出显示input,如果直接接受的话raw_input就可以

##############################
#类的使用
class Person:                          #声明类的名称
 pop="123";         #定义类中的变量 应该是为public的
 name='';
 #pass # An empty block
 def __init__(self,name):       #构造函数,方法名固定。self为规定传入,必须,后面是参数
  self.name = name;
 def sayHi(self):  #定义方法
  print "woea:",self.name;
 def echoPop(self):
  print self.pop;
  self.pop='come on baby';
  print self.pop;
 def __del__(self):  #析构函数,方法名固定
  pass;

class PChild(Person):   #继承person类
 def __init__(self):
  Person.__init__(self,"sanshi"); #使用父类的方法
 def __del__(self):
  pass
 def sayChild(self):
  print "child:",self.name;
##############################

import cPickle as p    #引入包,是c语言写的,速度快
#import pickle as p

shoplistfile = 'shoplist.data' #文件名
# the name of the file where we will store the object

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

# Write to the file
f = file(shoplistfile, 'w')     #打开文件
p.dump(shoplist, f)   #把文件序列化,写入介质,我们是写入文件
f.close()

del shoplist    #删除列表

f = file(shoplistfile)
storedlist = p.load(f)  #从文件中读出序列化的数据
print storedlist
##################################################
异常机制

 try:
    测试代码
    raise:
   抛出异常
except :
    捕捉异常处理
else:
    无异常执行
-------------------------------------------------------------
 try:
    测试代码
finally:
    有无异常都要执行

####################################################
还有很多其他的东西,但是那些我就没有整理笔记了
其中注意2个包,一个sys,一个os,这2个包完成了很多的东西

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值