python 条件语句

1. 布尔值

# if语句中视为假
if not '': print('假')
if not False: print('假')
if not None: print('假')
if not 0: print('假')
if not (): print('假')
if not {}: print('假')
if not []: print('假')

# True 和 False 是 1 和 0的别名
print(True == 1)
print(False == 0)

# 转换布尔值
bool1 = bool('hello world')
print(bool1)
bool1 = bool('')
print(bool1)
bool1 = bool(0)
print(bool1)

========================1=========================
假
假
假
假
假
假
假
True
True
True
False
False

2. if条件语句

name = 'Herry Gumby'
if name.endswith('Gumby'):
    print('Hello Mr.Gumby')

========================2=========================
Hello Mr.Gumby

3. else 语句

name = 'Herry Petter'
if name.endswith('Gumby'):
    print('Hello Mr.Gumby')
else:
    print('Hello stranger')
========================3=========================
Hello stranger
stranger

4. elif 子句

num = -1
if num > 0:
    print('The number is positive')
elif num < 0:
    print('The number is negative')
else:
    print('The number is zero')
========================4=========================
The number is negative

5. 代码块嵌套

name = 'Herry Gumby'
if name.endswith('Gumby'):
    if name.startswith('Herry'):
        print('Hello Herry Gumby')
    else:
        print('Hello Mr.Gumby')
else:
    print('Hello stranger')
========================5=========================
Hello Herry Gumby

6. 比较运算符

# 6.1 == 相等运算符 值是否相等
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y)

print('6.2'.center(50, '='))
# 6.2 is 相同运算符 对象是否相同
x = [1, 2, 3]
y = [1, 2, 3]
print(x is y)

# is 不用于不可变对象, 结果不可预测??
x = y = [1, 4, 5]
z = [1, 4, 5]
print(x is y)
print(x is z)
print( x == y == z)

x = y = 'hello'
z = 'hello'
print(x is y)
print(x is z)
print( x == y == z)


print('6.3'.center(50, '='))
# 6.3 in 成员资格检查
name = 'silly'
if 's' in name:
    print('your name contains the letter "s"')


print('6.4'.center(50, '='))
# 6.4 字符串和序列的比较
# 按照字母的排列顺序进行比较
print('alpha' < 'beta')
# 列表比较
print([1, 2] > [2, 1])

========================6=========================
=======================6.1========================
True
=======================6.2========================
False
True
False
True
True
True
True
=======================6.3========================
your name contains the letter "s"
=======================6.4========================
True
False

7. 布尔运算符

number = 9
print(number <= 10 and number >= 1)
========================7=========================
True

8. 断言

age = 10
assert 0 < age < 100
age = -1
try:
    assert 0 < age < 100
except AssertionError as e:
    print('[AssertionError]', e)
# 添加自己的说明
try:
    assert 0 < age < 100, 'The age must be realistic'
except AssertionError as e:
    print('[AssertionError]', e)
========================8=========================
[AssertionError]
[AssertionError] The age must be realistic

9. while循环

x = 1
while x <= 10:
    print(x)
    x += 1

========================9=========================
1
2
3
4
5
6
7
8
9
10

10. for循环

words = ['this', 'is', 'an', 'ex', 'parrot']
for word in words:
    print(word)
========================10========================
this
is
an
ex
parrot

11. range方法, 返回可迭代对象

print(list(range(0, 10)))
========================11========================
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

12. 迭代字典

d = {'x': 1, 'y': 2, 'z': 3}
for k, v in d.items():
    print(k, v)
========================12========================
x 1
y 2
z 3

13. 迭代工具

# 13.1 并行迭代
names = ['anne', 'beth', 'george', 'damon']
ages = [12, 45, 32, 102]
for i in range(len(names)):
    print(names[i], 'is', ages[i], 'years old')
# zip方法返回一个可迭代的对象
print(list(zip(names, ages)))


print('13.2'.center(50, '='))
# 13.2 迭代时返回索引加值
for index, string in enumerate(names):
    print(index, string)


print('13.3'.center(50, '='))
# 13.3 反向和排序
lst = [4, 3, 6, 8]
print(sorted(lst))
print(list(reversed(lst)))
========================13========================
=======================13.1=======================
anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old
[('anne', 12), ('beth', 45), ('george', 32), ('damon', 102)]
=======================13.2=======================
0 anne
1 beth
2 george
3 damon
=======================13.3=======================
[3, 4, 6, 8]
[8, 6, 3, 4]

14. 跳出循环

# 14.1 break 结束循环
for i in range(1, 100):
    print(i)
    if i == 4:
        break


print('14.2'.center(50, '='))
# 14.2 continue 跳出本次循环, 继续下一个循环
for i in range(1, 6):
    if i == 4:
       continue
    print(i)
========================14========================
=======================14.1=======================
1
2
3
4
=======================14.2=======================
1
2
3
5

15. 循环加else

from math import sqrt
for n in range(99, 81, -1):
    root = sqrt(n)
    if root == int(root):
        print(n)
        break
else:
    print('didn\'t find it')
========================15========================
didn't find it

你可能感兴趣的:(python 条件语句)