一.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'
 
 
 
 
 
 
 

最新文章

  1. java环境配置为1.7jdk为什么cmd java -version查看版本是1.8
  2. 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib的解决方法
  3. 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题
  4. 更新日志 - fir.im Jenkins &amp; Gradle 插件上线
  5. python一个注意的地方
  6. HDU 1404 (博弈) Digital Deletions
  7. python 获取当前调用函数名等log信息
  8. cloneNode克隆节点在不同浏览器的差异
  9. QA问答系统,QA匹配论文学习笔记
  10. DirectX--给视频加马赛克、字符OSD
  11. mybatis的动态sql编写以及一对一关系查询和一对多的查询
  12. Linux内存管理--用户空间和内核空间【转】
  13. NiftyDialogEffects-多种弹出效果的对话框
  14. javascript对象属性的命名规则
  15. markdown语法示例
  16. spring配置事务 元素 &quot;tx:annotation-driven&quot; 的前缀 &quot;tx&quot; 未绑定
  17. Python 入门学习(壹)上机时间提醒
  18. Nutch 问题杂记
  19. Centos7与Centos6的区别
  20. Python全栈day9(Python基础)

热门文章

  1. atom 默认的换行符设置成LF
  2. ABP开发框架前后端开发系列---(1)框架的总体介绍
  3. SQL Server的WAITFOR DELAY注入
  4. noip2017集训测试赛(六)Problem A: 炮艇大赛之正式赛
  5. Delphi Integer 转成单字节
  6. Delphi 释放数组中的数据
  7. windows上,python安装非官方包,提示error: Unable to find vcvarsall.bat
  8. WinRAR4.20注册文件key文件注册码
  9. php数据库操作代码
  10. 2017.2.21 activiti实战--第七章--Activiti与spring集成(一)配置文件