Python学习笔记

 # -*- coding: GBK -*-
i = 5                       #定义一个整数
s1 = 'hello word'           #定义一个字符串
#inp = raw_input('Enter something:')             #输入字符串
#print inp
print i
print s1
s1 = 'This is a string. /
This continues the string.' #明确的行连接
s2 = '''i want to return
this is the second line'''  #多行字符串

print s1
print s2
print 's1的长度为 len(s1) = ',len(s1),';第一个字符为“'/
      ,s1[0],'”;子字符串5~6为“',s1[5:7],'”;子字符串2到最后为“'/
      ,s1[2:],'”;子字符串开始会到倒数第3位“' /
      ,s1[:-2] ,'”'    #字符串最常用的的操作

if s1.startswith('This'):
    print 'Yes, s1 starts with "This"'
if 'This' in s1:
    print 'Yes, it contains the string "This"'
print s1==s2


i = i + 1 ; print i         #两行同时存在
print '除法: 9/5 = ',9/5                          #除法
print '除法: 9.0/5 = ',9.0/5                      #除法
print '整除: 9.0//5 = ', 9.0//5                   #整除
print '左移: 2 << 2 = ',2 << 2                    #左移
print '右移: 11 >> 1 = ',11 >> 1                  #左移
print '按位与: 1 & 3 = ', 1 & 3                   #按位与
print '按位或: 1 | 2 = ',1 | 2                    #按位或
print '按位异或: 3^6 = ',3^6                      #按位异或
print '按位翻转: ~5 = ',~5                        #按位翻转,x的按位翻转是-(x+1)
print '比较: 5>3,5>3,5==3,5!=3 : ',5>3," ",5>3," ",5==3," ",5!=3   #小于
print '布尔“非”: not (5>3) = ',(not 5>3)                          #布尔“非”
print '布尔“与”: (5>3) and (5<3) = ',(5>3) and (5<3)               #布尔“与”
print '布尔“或”: (5>3) or (5<3) = ',(5>3) or (5<3)                          #布尔“或”
print '''if 1 > 2:
    print '1 > 2'
elif 1 < 2:
    print '1 < 2'
else:
    print '这是不可能的'
=>'''

if 1 > 2:
    print '1 > 2'
elif 1 < 2:
    print '1 < 2'
else:
    print '这是不可能的'

print '''
age = 15
money = 47
while age <18:
    print "小子你没满18岁"
    age = age + 1
else:
    if money >= 50:
        print '你是大爷'
    else:
        print '50块都没有,滚开滚开'
   
for money in range(47,51):
    print "努力赚钱,有",money,'块了'
    if money > 51:
        break
    if money == 48:
        continue
else:
    if money >= 50:
        print '你是大爷'
    else:
        print '50块都没有,滚开滚开'
=>'''
age = 15
money = 47
while age <18:
    print "小子你没满18岁"
    age = age + 1
else:
    if money >= 50:
        print '你是大爷'
    else:
        print '50块都没有,滚开滚开'
   
for money in range(47,51):
    print "努力赚钱,有",money,'块了'
    if money > 51:
        break
    if money == 48:
        continue
else:
    if money >= 50:
        print '你是大爷'
    else:
        print '50块都没有,滚开滚开'

def  defFunction():                                 #函数
    print 'er...,貌似函数就是这么简单'
    return '你想试一下返回?'
def defParamFun(str='(貌似你什么都没有传哇^_^)',str2='(er,貌似没附加什么东东)'):                               #传参
    print "你给我的是这个吧 :",str," 也顺带了",str2,"^_^"
print 'defFunction()'
returnParam = defFunction()
print returnParam
print 'defParamFun()'
defParamFun()
print 'defParamFun(/'试试看/')'
defParamFun('试试看')
print 'fParamFun(str2 = /'再试/')'
defParamFun(str2 = '再试')

globalParam = 100
param2 = 100
def globalParamAndLocalParam():
    global globalParam
    globalParam = 110
    param2 = 110
    print "globalParam = ",globalParam ,";  param2 = ",param2
globalParamAndLocalParam()
print "globalParam = ",globalParam ,";  param2 = ",param2

def defDocStringTestFun(x):
    '''
    x必须是整数
    如果不是整数的话
    哈哈,哈哈,哈哈哈
    '''
    x = int(x)
    print x>0
print defDocStringTestFun.__doc__
defDocStringTestFun(11)
defDocStringTestFun('11')

#*************列表*************
ogres =['鬼','ogre','monster','Right Reverend','warrior']        #这个东东就是数组
ogres.append('贼')                                               #增
print "在禽兽世界里,我们创建了:"
for ogre in ogres:
    print ogre
print '总共',len(ogres),'种畜生.'
print '因为某种原因,种族灭绝',ogres[0],'挂了',ogres[1],'变种了'   
del ogres[0]                                                    #删
ogres[1] = '食人族'                                              #该
print '现在剩下',len(ogres),'种种族'
ogres.sort()                                                    #排
for ogre in ogres:                                              #查
    print ogre
#*************元组*************
woman = ('傻女','美女','丑女')                                    #元组和字符串一样是 不可变的 即你不能修改元组                                               #。
myName = 'whisper'                                             
mySex = '男'
myAge = 26
myGirlFrame = 'NULL'
myMsgYuanZu = (myName,mySex,myAge,myGirlFrame)
#下面一句取消注释会出错D
#myMsgYuanZu[0] = 'whisper lin'
print '%s  %s %d 女朋友: %s '%myMsgYuanZu


#字典  map
nameMap = {'yello':'刘建国',
    'sky':'胡增彬',
    'chcm':'陈超明'
    }
nameMap['kyo']='草薙京'
print 'yello是%s'%nameMap['yello']
print 'kyo是%s'%nameMap['kyo']
nameMap['kyo'] = '真 草薙京'
print 'kyo是%s'%nameMap['kyo']
del nameMap['kyo']


#序列
shoplist = ['apple', 'mango', 'carrot', 'banana']
# 序列索引查找
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]

# 获取子序列集合
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]

#删除子序列
del shoplist[0]
print 'shoplist is', shoplist

# 字符串示例
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:]

if name.startswith('Swa'):
    print 'Yes, the string starts with "Swa"'
if 'a' in name:
    print 'Yes, it contains the string "a"'
if name.find('war') != -1:
    print 'Yes, it contains the string "war"'

 

 

 class People:                           #定义一个类
    def __init__(self,name):            #这个相当于构造函数
        self.name = name
    def __del__(self):
        print '我要挂了...,我叫%s,记住我!!!'%self.name
    def sayHello(self):                 #方法与函数的区别只是一个额外的self变量
        print 'Hello,我是',self.name
class SuperPeople(People):
    def __init__(self,name = '无名',speed = 100):
        People.__init__(self,name)
        self.speed = speed
    def sayHello(self):
        print '(%dm/s闪速飙出)Hello,我是%s'%(self.speed,self.name)
p = People('项羽')
p.sayHello()
p2 = SuperPeople()
p2.sayHello()

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值