python 基础 2.2 if流程控制(二)

一. if  else
 
1.逻辑值(bool)包含了两个值:
----True:表示非空的值,比如:string ,tuple,list,set,dictonary,所有非空的序列。
-----False:表示0,None,空的量等(空的字符串,空的元组,空的列表等等)。
 
 
2. a.lower   //字符串中把大小字符串变成小写
In [1]: a = 'BC'
In [3]: a.lower()
Out[3]: 'bc'
 
示例:
[root@localhost python-scripts]# cat 14.py
#!/usr/bin/python
#coding=utf-8
yn = raw_input("please input [Yes/No]:" )
yn = yn.lower()
if yn == 'yes' or yn == 'y':
    print "程序正在运行"
elif yn == 'no' or yn == 'n':
    print "程序已经退出"
else:
    print "请输入[Yes/No]"
 
 
运行:
[root@localhost python-scripts]# python 14.py
please input [Yes/No]:yes
程序正在运行
[root@localhost python-scripts]# python 14.py
please input [Yes/No]:no
程序已经退出
[root@localhost python-scripts]# python 14.py
please input [Yes/No]:uu
请输入[Yes/No]
 
 
3. a.upper()    //把小写字符串变成大写字符串,如下:
 
In [16]: a = 'abc'
 
In [17]: a.upper()
Out[17]: 'ABC'
 
 
[root@localhost pythonscripts]# vim 4.py
 
#!/usr/bin/python
#coding=utf-8
yn = raw_input ("please input [Yes/No]:")
yn = yn.upper()
if yn == "YES" or yn == "Y":
    print "程序正在运行"
elif yn == "NO" or yn == "N":
    print "程序已经退出"
else:
    print "请输入[Yes/No]"
 
 
运行如下:
[root@localhost pythonscripts]# python 4.py
please input [Yes/No]:yes
程序正在运行
[root@localhost pythonscripts]# python 4.py
please input [Yes/No]:no
程序已经退出
[root@localhost pythonscripts]# python 4.py
please input [Yes/No]:oo
请输入[Yes/No]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/lzcys8868/p/7730293.html