本文主要是介绍嵌入式 pyhton中的判断语句,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Python中比较语句和其它的一样都是用if来做判断的,只是语法上后面会带上冒号,如ifa>b:,相当于if(a>b){}.
同样你可以判断函数的返回值为True或者False来做判断
[python] viewplain copy print ?
- __author__="Alex"
- __date__
="$2011-2-17 10:39:27$" - if
__name__ == "__main__": -
print "Hello"; - a
= 3 - b
= 2 - c
= 2 - #
True statements - if
c == b: print('c is equal as b') - if
c >= b: print('...') - if
a >= b: print('a is greater than b') - if
c <= a: print('c is less than a.') - if
c != a: print('c is not equal as a') - #
False statements - if
c == a: print('...')# will show nothing on console - if
c != b: print('...')# will show nothing on console - def
inside(): -
return True - if
inside(): -
print ("Inside") - else:
-
print("OutSide")
Console OutPut:
[c-sharp] viewplain copy print ?
- Hello
- c
is equal as b - ...
- a
is greater than b - c
is less than a. - c
is not equal as a - Inside
多重比较:
当然你可能会用到 且或这些操作,在Python中是使用and/or 来做到的,看代码吧,从代码中学习语言才是王道:
[python]viewplaincopyprint?
- __author__="Alex"
- __date__
="$2011-2-17 10:39:27$" - if
__name__ == "__main__": -
print "Hello"; - if
4 > 3 and 6 > 3: print('You are right , 4 is greater than 3 and 6 is greater than 3 too') # will print, True and True becomes True - if
4 > 3 and 2 > 3: print('...') # will not print, True and False becomes False - if
4 > 3 and not 2 > 3: print('You are right , 4 is greater than 3 and 2 is less than 3 ') # will print, True and not False becomes True - if
4 > 3 or 6 > 3: print('You are right...,When 4 is grater than 3 is ture, the condition is ture') # will print, True or True becomes True - if
(4 > 3 and 6 > 3) and not ( 3 > 5 or 3 > 7): print("(True and True) and not ( False or False ) becomes True and not False")#Complex Multiple Conditions
Console Output:
[c-sharp]viewplaincopyprint?
- Hello
- You
are right , 4 is greater than 3 and 6 is greater than 3 too - You
are right , 4 is greater than 3 and 2 is less than 3 - You
are right...,When 4 is grater than 3 is ture, the condition is ture - (True
and True) and not ( False or False ) becomes True and not False
这篇关于嵌入式 pyhton中的判断语句的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!