Python_4_Codecademy_4_Conditiona

2016-04-17  本文已影响16人  张一闻

<a href="http://www.jianshu.com/p/54870e9541fc">总目录</a>


课程页面:https://www.codecademy.com/
内容包含课程笔记和自己的扩展折腾

Comparators

Boolean operators

Conditional Statement Syntax (if)

if 6 + 1 != 10:
    print "abc"
def addition():
    if 1 + 1 == 2:
        return True
#
def print_result():
    if addition():
        return "2"
#
print "1+1=" + print_result()

if ... else ...

def addition(a, b):
    if a >= b:
        print "the 1st number %s is NOT less than the 2nd number %s" % (a, b)
    else: 
        print "the 1st number %s is less than the 2nd number %s" % (a, b)
#
addition(3, 3)
addition(1, 6)
addition(6, 1)
"""
Output:
the 1st number 3 is NOT less than the 2nd number 3
the 1st number 1 is less than the 2nd number 6
the 1st number 6 is NOT less than the 2nd number 1
"""

if... elif... elif... else...

def addition(a, b):
    if a > b:
        print "the 1st number %s is greater than the 2nd number %s" % (a, b)
    elif a == b:
        print "the 1st number %s is equal to the 2nd number %s" % (a, b)
    else: 
        print "the 1st number %s is less than the 2nd number %s" % (a, b)
#
addition(3, 3)
addition(1, 6)
addition(6, 1)
"""
Output:
the 1st number 3 is equal to the 2nd number 3
the 1st number 1 is less than the 2nd number 6
the 1st number 6 is greater than the 2nd number 1
"""
上一篇下一篇

猜你喜欢

热点阅读