小阳说:本文从Helloworld开始,列举了python的入门基础知识,以最简洁的方式对python进行深入剖析。
参考资料:
【1】简明 Python 教程 http://sebug.net/paper/python/index.html
1、HelloWorld
- 使用vi新建helloworld.py
samuel@linuxdaily:~/python_test> vi helloworld.py
- 输入以下内容
#!/usr/bin/python # Filename : helloworld.py print 'Hello World'
- 运行与查看helloworld.py
samuel@linuxdaily:~/python_test> ll total 4 -rw-r--r-- 1 samuel users 65 2012-11-05 14:42 helloworld.py samuel@linuxdaily:~/python_test> chmod a+x helloworld.py samuel@linuxdaily:~/python_test> ll total 4 -rwxr-xr-x 1 samuel users 65 2012-11-05 14:42 helloworld.py samuel@linuxdaily:~/python_test> ./helloworld.py Hello World
- 可执行的Python文件
使用chmod为helloworld.py添加可执行权限
samuel@linuxdaily:~/python_test> ll total 4 -rw-r--r-- 1 samuel users 65 2012-11-05 14:42 helloworld.py samuel@linuxdaily:~/python_test> chmod a+x helloworld.py samuel@linuxdaily:~/python_test> ll total 4 -rwxr-xr-x 1 samuel users 65 2012-11-05 14:42 helloworld.py samuel@linuxdaily:~/python_test> ./helloworld.py Hello World
2、控制语句
#!/usr/bin/python # Filename: if.py number = 23 guess = int(raw_input('Enter an integer : ')) if guess == number: 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 print 'Done' # This last statement is always executed, after the if statement is executed
#!/usr/bin/python # Filename: while.py 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 higher than that' else: print 'No, it is a little lower than that' else: print 'The while loop is over.' # Do anything else you want to do here print 'Done'
while
语句有一个可选的else
从句(个人感觉用途不是很明显呀)
#!/usr/bin/python # Filename: for.py for i in range(1, 5): print i else: print 'The for loop is over'
输出结果
samuel@linuxdaily:~/python_test> python for.py 1 2 3 4 The for loop is over
for i in range(1,5)
等价于for i in [1, 2, 3, 4]
#!/usr/bin/python # Filename: break.py while True: s = raw_input('Enter something : ') if s == 'quit': break print 'Length of the string is', len(s) else: print 'end loop' print 'Done'
输出结果:
samuel@linuxdaily:~/python_test> python break.py Enter something : 123 Length of the string is 3 Enter something : 345 Length of the string is 3 Enter something : 567 Length of the string is 3 Enter something : quit Done
如果你从for
或while
循环中 终止 ,任何对应的循环else
块将不执行。
#!/usr/bin/python # Filename: continue.py while True: s = raw_input('Enter something : ') if s == 'quit': break if len(s) < 3: continue print 'Input is of sufficient length' # Do other kinds of processing here...
输出结果
samuel@linuxdaily:~/python_test> python continue.py Enter something : 1 Enter something : 12 Enter something : 123 Input is of sufficient length Enter something : 1234 Input is of sufficient length Enter something : quit
3、函数
#!/usr/bin/python # Filename: function1.py def sayHello(): print 'Hello World!' sayHello() # call the function
#!/usr/bin/python # Filename: func_param.py 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
#!/usr/bin/python # Filename: func_local.py def func(x): print 'x is', x x = 2 print 'Changed local x to', x x = 50 func(x) print 'x is still', x
当你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是 局部 的。这称为变量的 作用域 。所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。
输出结果
samuel@linuxdaily:~/python_test> python func_local.py x is 50 Changed local x to 2 x is still 50
#!/usr/bin/python # Filename: func_global.py def func(): global x print 'x is', x x = 2 print 'Changed local x to', x x = 50 func() print 'Value of x is', x
如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。我们使用global
语句完成这一功能。没有global
语句,是不可能为定义在函数外的变量赋值的。
你可以使用定义在函数外的变量的值(假设在函数内没有同名的变量)。然而,我并不鼓励你这样做,并且你应该尽量避免这样做,因为这使得程序的读者会不清楚这个变量是在哪里定义的。使用global
语句可以清楚地表明变量是在外面的块定义的。
输出结果
samuel@linuxdaily:~/python_test> python func_global.py x is 50 Changed local x to 2 Value of x is 2
#!/usr/bin/python # Filename: func_default.py def say(message, times = 1): print message * times say('Hello') say('World', 5)
#!/usr/bin/python # Filename: func_key.py 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)