• python练习


     
    使用while语句:
    number=23 running=True while running: guess=int(raw_input('Enter an integer:')) if guess==number: print 'Congratulations,you guessed it.' running=False# this causes the while loop to stop elif guess<number: print 'No,it is a little higner than that' else: print 'No,it is a little lower than that' else: print 'The while loop is over.' print 'Done'
    #使用for语句
    for i in range(1,5):
        print i
    else:
        print'The for loop is over.'
    
    #使用break语句
    while True:
        s=raw_input('Enter something:')
        if s=='quit':
            break
        print 'Length of the string is',len(s)
    print 'Done'
    
    #使用continue语句
    while True:
        s=raw_input('Enter something:')
        if s=='quit':
            break
        if len(s)<3:
            continue
        print 'Input is of sufficient length
    #定义函数
    def sayHello():
        print 'Hello World!'# block belonging to the function
    sayHello() #call the function
    
    #使用函数形参
    def  printMax(a,b):
        if a>b:
            print a, 'is maximum'
        else:
            print b,'is maximum'
    printMax(3,4)#directly give literal values
    x=5
    y=7
    printMax(x,y)#give variables as arguments
    
    #使用局部变量
    def func(x):
        print 'x is',x
        x=2
        print 'Changed local x to',x
    x=50
    func(x)
    print'x is still',x
    
    #使用global语句
    def func():
        global x
        print 'x is',x
        x=2
        print 'Changed local x to',x
    x=50
    func()
    print 'Valued of x is',x
    
    #使用默认参数值
    def say(message,times=1):
        print message*times
    say('Hello')
    say('World',5)
    
    #使用关键参数
    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)
    
    #使用字面意义的语句
    def maximum(x,y):
        if x>y:
            return x
        else:
            return y
    print maximum(2,3)
  • 相关阅读:
    一个统计代码行数的简单方法
    关于string的对象引用
    mysql连接的一些问题。
    linux环境初始化 用户问题
    php null o false ''
    php支付宝在线支付接口开发教程【转】
    模拟支付宝服务窗环境
    ctags
    校验全球电话号码 正确性 库 正则表达式
    php短路与 短路或
  • 原文地址:https://www.cnblogs.com/ilxx1988/p/python.html
Copyright © 2020-2023  润新知