Pyhton学习之条件语句

19 篇文章 0 订阅
17 篇文章 1 订阅

条件 语句

条件语句是编程语言中重要的一部分,程序在处理的时候先检测条件,如果符合则执行语句块儿,如果不符合则不执行语句块。

python中的条件结构

if condition :
statement

condition 代表条件 如果条件成立,则执行statement语句块儿,如果条件不成立,则不执行语句块儿。
规则
在Python中,规定
·条件的结束以一个冒号“:”结束
·语句块儿必须以4个空格来进行缩进(一般是4个空格),python这样做就是为了每个编程人员养成良好的编程习惯。
·对于条件语句和后面将要学习的循环语句,最好使用文本编辑器,因为一旦在命令行直接输入,一旦错误,修改起来将会很麻烦

单条件结构也叫单分支语句

if condition**:**
statement

需求,假设a = 10,只有当a > 3 的时候,那么print a,

>>> a = 10
>>> if a > 3:
...     print a
... 
10
>>> 

需求,假设string = name,只有当string == “name的时候输出string,否则输出“string is not name”

>>> string = "name"
>>> if string == "name":
...     print string
... else:
...     print "string is not equal name"
... 
name
>>> if string == "age":
...     print string
... else:
...     print "string is not equal name"
... 
string is not equal name
>>> 

我们这样做有点太古板了,就是每次我们都是先定义好的变量,然后在让其去执行,那么现在试试人机交互模式
先来看看帮助

help(raw_input)
Help on built-in function raw_input in module builtin:

raw_input(…)
raw_input([prompt]) -> string

Read a string from standard input.  The trailing newline is stripped.
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
On Unix, GNU readline is used if enabled.  The prompt string, if given,
is printed without a trailing newline before reading.

(END)
解释:从标准输入设备读取一个字符串。并且换行,如果用户触发EOF(unix ctl+D, windows:ctl-Z+Return)将会引发EOFError。
其余的就不解释了,反正就是从标准输入设备读取一个字符串

>>> string = raw_input()
name
>>> string
'name'
>>> string = raw_input()
xiaohuafen
>>> string
'xiaohuafen'
>>> 

那么在条件中我们应该怎么写。这里我使用文本编辑器vi,因为我的操作系统是Linux,所以使用vi编辑器,当然也可以在windows下使用sublime,editplus,notpad等等,但是不要使用txt文本编辑,因为可能会引发字符编码的错误。

#!/usr/bin/env python
#coding:utf-8
#anthor:xiaohuafen
#description:this is test if condition statement
string = raw_input()
if string == "name":
    print "your input\'s string is %s" % string
else:
    print "your input\'s string is %s" % string
    print "please input again"
~                                

执行结果:

[root@python ~]# python iftest.py 
name
your input's string is name
[root@python ~]# python iftest.py 
python
your input's string is python
please input again
[root@python ~]# 

多条件结构语句,也叫多分支语句

语法格式:

if condition:
statement
elif condition1:
statement1
elif condition2:
statemnet2
else
statement4

需求:假设从键盘接收一个数字number,如果number > 3 那么输出 number more than 3,并且输出Number,如果number == 3,那么输出number is equal 3,并输出Number,如果number<3那么输出number less than 3,并且输出Number,否则输出,you are computer

#!/usr/bin/env python
#coding:utf-8

number = raw_input()
if number > 3:
    print "your input's is %d" % number
    print "number more than 3"
elif number == 3:
    print "your input's is %d" % nubmer
    print "number is qual 3"
elif number < 3:
    print "your input's is %d" % number
    print "number less than 3"
else:
    print "you are computer,please try again"
~                        

执行结果:

[root@python ~]# python eliftest.py 
3
Traceback (most recent call last):
  File "eliftest.py", line 6, in <module>
    print "your input's is %d" % number
TypeError: %d format: a number is required, not str

我们发现报错了,提示说格式类型错误,请求的是一个数字,而不是字符串
我们之前说到了raw_input( )函数,它只接受字符串
修改方法,进行类型转换或者修改输出属性
方法一,修改输出属性

#!/usr/bin/env python
#coding:utf-8
print "Please input a number: "
number = raw_input()
if number > 3:
    print "your input's is %s" % number
    print "number more than 3"
elif number == 3:
    print "your input's is %s" % number
    print "number is qual 3"
elif number < 3:
    print "your input's is %s" % number
    print "number less than 3"
else:
    print "you are computer,please try again"
~        

%d修改为%s
执行输出结果:

[root@python ~]# python eliftest.py 
Please input a number: 
4
your input's is 4
number more than 3
[root@python ~]# python eliftest.py 
Please input a number: 
3
your input's is 3
number more than 3
[root@python ~]# python eliftest.py 
Please input a number: 
2
your input's is 2
number more than 3
[root@python ~]# 

发现都只执行第一个条件,我们的程序执行有问题
方法二:修改条件,将条件修改为字符串

#!/usr/bin/env python
#coding:utf-8
print "Please input a number: "
number = raw_input()
if number > "3":
    print "your input's is %s" % number
    print "number more than 3"
elif number == "3":
    print "your input's is %s" % nubmer
    print "number is qual 3"
elif number < "3":
    print "your input's is %s" % number
    print "number less than 3"
else:
    print "you are computer,please try again"
~                                            

执行结果:

[root@python ~]# python eliftest.py 
Please input a number: 
4
your input's is 4
number more than 3
[root@python ~]# python eliftest.py 
Please input a number: 
3
your input's is 3
number is qual 3
[root@python ~]# python eliftest.py 
Please input a number: 
2
your input's is 2
number less than 3
[root@python ~]# 

发现可以执行,但是number被当做字符串来执行
方法三,进行类型转换,将raw_input( )转换为整型

#!/usr/bin/env python
#coding:utf-8
print "Please input a number: "
number = int(raw_input())
if number > 3:
    print "your input's is %s" % number
    print "number more than 3"
elif number == 3:
    print "your input's is %s" % number
    print "number is qual 3"
elif number < 3:
    print "your input's is %s" % number
    print "number less than 3"
else:
    print "you are computer,please try again"

执行输出结果:

[root@python ~]# python eliftest.py 
Please input a number: 
4
your input's is 4
number more than 3
[root@python ~]# python eliftest.py 
Please input a number: 
3
your input's is 3
number is qual 3
[root@python ~]# python eliftest.py 
Please input a number: 
2
your input's is 2
number less than 3

三元操作符

三元操作符在其它编程语言中也叫做三目运算符如java中,它们表示的都是一样的,是一种简单的条件结构
语句结构
statement if condition else statement

>>> a = 10
>>> b = 3
>>> string = "python" if a > b else "Java"
>>> string
'python'
>>> string = "python" if a < b else "Java"
>>> string
'Java'
>>> 

解释,如果condition为true,则执行string = “python”,如果condition 为假,则执行 string = “Java”
关于条件分支结构,就学习到这里,接下来要学习的是多种循环结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值