python 学习源码练习(1)

#编译方式,python3 文件名

#!/usr/bin/python3
#print('hello world')

 

mystring = 'hello world'
print (mystring)

 

 

#测试输入

#!\usr\bin\python3

num = input('Please enter an num :')
print ("now Doubling your num %d" % (int(num)*2))

 

#字符串和切片

#!/usr/bin/python3

pystr = 'Python'
iscool = 'is cool'
print ('pystr[0] is %s.' % pystr[0])
print ('pystr[2:5] is %s'% pystr[2:5])

 

 

#!/usr/bin/python3

#if test
count = 1
if count > 1:
    print ("test 1")
elif count == 1 :
    print ("test 2")

 

#while loop test
while count < 3:
    print("now count = %d" % count)
    count += 1


#for loop test
print("I like to use internet for:")
for item in ['e-mail', 'net-surfing', 'homework']:
    print(item,)

    print()

#range test

for eachNum in [0,1,2]:
     print(eachNum)

print('*'*4)
for eachNum in range(3):
     print(eachNum)

print('test for string','*'*4)

foo = 'abs'
for s in foo:
    print(s)

print('test of range and len')

for i in range(len(foo)):
    print (foo[i],'%d'%i)


squared = [x**2 for x in range(4)]
for i in squared:
    print(i)

 

#function test
def addMe2Me(x):
    'apply + operation to argument'
    return (x+x)


print(addMe2Me([2,'4']))

 

#default function param test

#!/usr/bin/python3


def foo(debug=True):
    if debug:
        print('cur in debug mode')
    else:
        print('cur in realse mode')
    print('done')

 foo(False)

 

#class test

#!/usr/bin/python3


class Myfoo(object):
    version = 0.1

    def __init__(self,nm='John Doe'):
        self.name = nm
        print('created a class instance for',nm)
        print('this is a ','test')

    def showname(self):
        print('your name is ', self.name)

    def showverison(self):
        print( self.version)


foo = Myfoo()

 

转载于:https://www.cnblogs.com/honorplus/p/7899390.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值