forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.if_else.py
More file actions
42 lines (38 loc) · 910 Bytes
/
07.if_else.py
File metadata and controls
42 lines (38 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
"""
@description: 判断语句
@author:XuMing
"""
from __future__ import print_function
from __future__ import unicode_literals
# 基本用法
a = 62
print("exam score check:")
if a >= 60:
print("student pass")
elif a == 0:
print("student 0: not pass")
else:
print("student not pass")
# 可以使用 and , or , not 等关键词结合多个判断条件:
a = 10
b = -5
print(a > 0 and b < 0) # True
print(not a > 0) # False
print(a < 0 or b < 0) # True
# 一个例子
year = 1900
if year % 400 == 0:
print("This is a leap year!")
# 两个条件都满足才执行
elif year % 4 == 0 and year % 100 != 0:
print("This is a leap year!")
else:
print("This is not a leap year.")
# This is not a leap year.
my_list = [1, 2]
# 判断一个列表是否为空。
if len(my_list) > 0:
print("the first element is: ", my_list[0])
else:
print("no element.")