我在学python

来源:互联网 发布:淘宝送的优酷会员在哪 编辑:程序博客网 时间:2024/05/21 08:58

1.raw_input

guess = int(raw_input('Enter an integer : '))

2.while  loop

while running:        guess = int(raw_input('Enter an integer : '))        if guess == number:                running=False                print 'Congratulations, you guessed it.' # New block starts here                print "(but you do not win any prizes!)" # New block ends here        elif guess < number:                print 'No, it is a little higher than that' # Another block                # You can do whatever you want in a block ...        else:                print 'No, it is a little lower than that'                # you must have guess > number to reach hereelse:        print 'Done'

3.for loop

for i in range(1,5,2):        print ielse:        print "the loop is over"
the default step for range is "1"

4.break the loop

for i in range(1,5,2):        print i        if i==1:                breakelse:        print "the loop is over"

5.len

len(s) print the lenth of the string s

6.python function

def sayhello():
        print "hello python"
sayhello()

7.global var

def printMax(a,b):        global x        x=100        if a>b:                print a,"is maximum"        else:                print b,"is maximum"x=5y=6printMax(x,y)print x

8.default arg

def printMax(a,b=1010):        global x        x=100        if a>b:                print a,"is maximum"        else:                print b,"is maximum"x=5printMax(10)print x
we do not need care about the sequence

9.

#!/bin/pythonif __name__ == '__main__':        print 'this is being run by himself'else:        print 'I am being run by others'

$ python using_name.py
This program is being run by itself
$ python
>>> import using_name
I am being imported from another module
>>>

10.import

#!/bin/pythondef sayhi():        print 'hi,this is my module'version = '0.1'

#!/bin/pythonimport mymodulemymodule.sayhi()print 'version',mymodule.version

11.from......import





原创粉丝点击