现在的位置: 首页 > 综合 > 正文

我在学python

2013年01月15日 ⁄ 综合 ⁄ 共 1328字 ⁄ 字号 评论关闭

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 here
else:
        print 'Done'

3.for loop

for i in range(1,5,2):
        print i
else:
        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:
                break
else:
        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=5
y=6
printMax(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=5
printMax(10)
print x
we do not need care about the sequence

9.

#!/bin/python
if __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/python
def sayhi():
        print 'hi,this is my module'
version = '0.1'

#!/bin/python
import mymodule
mymodule.sayhi()
print 'version',mymodule.version

11.from......import



抱歉!评论已关闭.