Python学习笔记三

Python中完整的if语句是
if condition:
    XX
elif:
    YY
else:
    ZZ

Python中没有switch语句,不过可以使用dictionary,或者if-elif-else来实现

关于缩进:Python是以缩进来识别语句块的,例如:

x = 1
if x:
    y = 0
    if y:
        print ‘block2′
    print ‘block1′
print ‘block0′

上面这段程序的结果是:
block1
block0
但如果缩进改为这样:

x = 1
if x:
    y = 0
    if y:
        print ‘block2′
        print ‘block1′
print ‘block0′
结果就变为了:
block0
长字符串
print “a very loooooooooooooooooooooooong statement “\
“test”
两行都使用双引号,第一行末使用反斜杠。

关于while语句:
while : # Loop test
# Loop body
else: # Optional else
# Run if didn’t exit loop with break
else是可选的,而且只有在while中的循环没有进行时才执行。

This entry was posted in Python, 编程相关. Bookmark the permalink.

4 Responses to Python学习笔记三

  1. wind says:

    好玩,用缩进来标志程序块 这样程序员就得注意排版了,难怪python写出来的代码漂亮。

  2. cocobear says:

    好玩 不好玩
    看看你几年没写博客了

  3. luguo says:

    list/dictionory再加上lambda东东就无敌了啊~~

  4. crazyfranc says:

    没switch?有case没,感觉和shell有很多相通之处。缩进识别语句块很好吗?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>