【python】学习入门 - part1

小阳说:本文从Helloworld开始,列举了python的入门基础知识,以最简洁的方式对python进行深入剖析。


参考资料:

【1】简明 Python 教程 http://sebug.net/paper/python/index.html

Part1目录:

 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、控制语句

  • if
#!/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

返回目录 

  • while
#!/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从句(个人感觉用途不是很明显呀)

返回目录

  • for
#!/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]

返回目录

  • break
#!/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

  如果你从forwhile循环中 终止 ,任何对应的循环else块将执行。

返回目录

  •  continue
#!/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

返回目录

  • global
#!/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) 

返回目录

转载于:https://www.cnblogs.com/onliny/archive/2012/11/05/2755110.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值