python 基础 2.1 if 流程控制(一)
2024-08-23 22:29:47
一.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'
最新文章
- java环境配置为1.7jdk为什么cmd java -version查看版本是1.8
- 未能解析目标框架“.NETFramework,Version=v4.0”的 mscorlib的解决方法
- 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题
- 更新日志 - fir.im Jenkins &; Gradle 插件上线
- python一个注意的地方
- HDU 1404 (博弈) Digital Deletions
- python 获取当前调用函数名等log信息
- cloneNode克隆节点在不同浏览器的差异
- QA问答系统,QA匹配论文学习笔记
- DirectX--给视频加马赛克、字符OSD
- mybatis的动态sql编写以及一对一关系查询和一对多的查询
- Linux内存管理--用户空间和内核空间【转】
- NiftyDialogEffects-多种弹出效果的对话框
- javascript对象属性的命名规则
- markdown语法示例
- spring配置事务 元素 ";tx:annotation-driven"; 的前缀 ";tx"; 未绑定
- Python 入门学习(壹)上机时间提醒
- Nutch 问题杂记
- Centos7与Centos6的区别
- Python全栈day9(Python基础)
热门文章
- atom 默认的换行符设置成LF
- ABP开发框架前后端开发系列---(1)框架的总体介绍
- SQL Server的WAITFOR DELAY注入
- noip2017集训测试赛(六)Problem A: 炮艇大赛之正式赛
- Delphi Integer 转成单字节
- Delphi 释放数组中的数据
- windows上,python安装非官方包,提示error: Unable to find vcvarsall.bat
- WinRAR4.20注册文件key文件注册码
- php数据库操作代码
- 2017.2.21 activiti实战--第七章--Activiti与spring集成(一)配置文件