1        条件判断

[root@daidai python]# cat age.py

#!/usr/bin/python

# -*- coding: utf-8 -*-

 

age=20

if age >=18:

    print('你的年龄 :',age)    --缩进的两行被认为是一个语句块

    print('Adult')

else:

    print('你的年龄 :',age)

print('Teenager')

if的语法结构

if <条件判断1>:

    <执行1>

elif <条件判断2>:

    <执行2>

elif <条件判断3>:

    <执行3>

else:

<执行4>

这里没有像shell中有个fi结尾,表示if判断结束。

if语句执行有个特点,它是从上往下判断,如果在某个判断上是True,把该判断对应的语句执行后,就忽略掉剩下的elifelse

if x:

print('True')

只要x是非零数值、非空字符串、非空list等,就判断为True,否则为False

 

if判断中交互式传入参数

[root@daidai python]# cat birth.py

#!/usr/bin/python

# -*- coding:utf-8 -*-

 

birth=input('Please input your birthdayyear: ')

birth=int(birth)

if birth > 2000:

   print('你是00')

else:

   print('你是00')

[root@daidai python]# python birth.py

Please input your birthday year: 2015

你是00