标签:input space 执行 stat div 相对 size python 基础 使用
一.if else
1.if 语句
if expression: //注意if后有冒号,必须有
statement(s) //相对于if缩进4个空格
注:python使用缩进作为其语句分组的方法,建议使用4个空格
2.示例:
1》[root@localhost python-scripts]# cat 11.py
#!/usr/bin/python
#coding=utf-8
if 1: //在python中1为真(true),0为假(fluse)
print "hello python" //当为真的时候输出“hello python”
else:
print "hello linux" //否则就输出“hell linux”
运行如下:
[root@localhost python-scripts]# python 11.py
hello python
2》[root@localhost python-scripts]# cat 11.py
#!/usr/bin/python
#coding=utf-8
if 0: //只有当条件成立的时候才会执行print "hello python"的语句,否则就执行print "hell linux"
print "hello python"
else:
print "hello linux"
运行如下:
[root@localhost python-scripts]# python 11.py
hello linux
3》[root@localhost python-scripts]# cat 11.py
#!/usr/bin/python
#coding=utf-8
if 1 < 2: //1小于2成立,就会执行下面的语句
print "hello python"
else:
print "hello linux"
运行结果如下:
[root@localhost python-scripts]# python 11.py
hello python
4》[root@localhost python-scripts]# cat 11.py
#!/usr/bin/python
#coding=utf-8
a = (‘b‘,1,2)
if ‘b‘ in a:
print "hello python"
else:
print "hello linux"
运行如下:
[root@localhost python-scripts]# python 11.py
hello python
5》[root@localhost python-scripts]# cat 11.py
#!/usr/bin/python
#coding=utf-8
if not 1 > 2: //取反
print "hello python"
else:
print "hello linux"
运行如下:
[root@localhost python-scripts]# python 11.py
hello python
6》[root@localhost python-scripts]# cat 11.py
#!/usr/bin/python
#coding=utf-8
if not 1 > 2 and 1==1:
print "hello python"
else:
print "hello linux"
[root@localhost python-scripts]# python 11.py
hello python
3.if语句练习:此处用的是input
[root@localhost python-scripts]# cat 12.py
#!/usr/bin/python
#coding=utf-8
sorce = input("please input a num: ")
if sorce >= 90:
print "A"
print "very good"
elif sorce >=80:
print ‘B‘
print ‘good‘
elif sorce >=70:
print ‘pass‘
else:
print ‘not pass‘
4.int 用法
In [1]: int(‘30‘) //30加引号是字符串,通过int又变成数字了
Out[1]: 30
In [2]: type(int(‘50‘))
Out[2]: int //类型为整形
5.if练习,此处用raw_input
[root@localhost python-scripts]# cat 12.py
#!/usr/bin/python
#coding=utf-8
sorce = int(raw_input("please input a num: ")) //此处用的是raw_input,把加引号的数字变成整形
if sorce >= 90:
print "A"
print "very good"
elif sorce >=80:
print ‘B‘
print ‘good‘
elif sorce >=70:
print ‘pass‘
else:
print ‘not pass‘
python 基础 2.1 if 流程控制(一)
标签:input space 执行 stat div 相对 size python 基础 使用
原文地址:http://www.cnblogs.com/lzcys8868/p/7730278.html