Python学习之条件语句

# coding=utf-8


# 使用逗号隔开抖个输出语句
print 'Age:', 42


# 输出自动转换后的字符串
name = 'Gumby'
salutation = 'Mr.'
greeting = 'Hello.'
print greeting + ',', salutation, name


import math as foobar


print foobar.sqrt(4)


print '赋值魔法'
# 个数要对应上,否则会有异常
x, y, z = 1, 2, 3
print x, y, z


x, y = y, x
print x, y, z


values = 1, 2, 3
x, y, z = values
print x


scoundrel = {'name': 'Robin', 'girlfriend': 'Marion'}
key, value = scoundrel.popitem()
print key
print value


print '链式赋值'
x = y = 'OK'
print x, y


print "增量赋值"
x = 2
x += 1
print x
x *= 2
print x


fnord = 'foo'
fnord += 'bar'
fnord *= 2
print fnord


print '条件语句'
# 这些值都被看作为假:false, None, 0, "", (), [], {}
print bool('I am test')


# 注意有个分号
name = 'llllllllllllll4445'
if name.endswith('2345'):
    print 'True'
elif name.endswith('345'):
    print 'True2'
else:
    if name.endswith('45'):
        print "True3"
    else:
        print 'False'


if 'a' in 'main':
    print 'True'


if 'alpha' < 'beta':
    print "True"


if [1, 2] < [2, 1]:
    print 'True'


if [2, [1, 4]] < [2, [1, 5]]:
    print 'True'


number = 2
if number > 1 and number < 10:
    print 'Right'


number = -1
if number > 1 or number == -1:
    print 'Right2'


print '断言'
assert number < 1


print '循环'


x = 1
while x < 5:
    print x
    x += 1


words = ['this', 'is', 'an']
for word in words:
    print word


# 迭代
print range(0, 10)
print range(0, 10, 2)


for number in range(0, 5):
    print number


d = {'x': 1, 'y': 2, 'z': 3}
for key in d:
    print key, 'corresponds to ', d[key]


names = ['anne', 'bett', 'ccc', 'dadd']
ages = [12, 34, 56, 78]
for i in range(len(names)):
    print names[i], 'is', ages[i], 'years old'




# zip函数可以把连个序列压缩在一起,返回一个元组的列表
print zip(names, ages)
# 最短的用完为止
print zip(range(5), xrange(10000))


strings = ['aa', 'bb', 'cc', 'aa', 'dd aa ee']
for index, string in enumerate(strings):
    if 'aa' in string:
        strings[index] = 'jj'
print strings


print sorted([3, 2, 5, 1, 3])
print list(reversed('Hello,world!'))


print ''.join(reversed('Hello, world!'))


print '跳出循环'
from math import sqrt


for n in range(99, 0, -1):
    root = sqrt(n)
    if root == int(root):
        print n
        break
else:
    print "Didn't find it"


print '列表推导式'


print [x * x for x in range(5)]
print [x * x for x in range(5) if x % 3 == 0]
print [(x, y) for x in range(3) for y in range(3)]




# pass 什么也不做
if name == 'A':
    print 'A'
elif name == 'B':
    pass
else:
    print 'Error'


x = 1
del x




def square2(x):
    return x * x




a = [1, 2, 3, 4, 5]
b = [6, 7]
# 把a中的每个值作为函数square2的参数,返回一个列表
print map(square2, a)

print map(None, a, b)


输出:


"D:\Program Files\Python\python.exe" G:/HelloPython/list/syntax.py
Age: 42
Hello., Mr. Gumby
2.0
赋值魔法
1 2 3
2 1 3
1
girlfriend
Marion
链式赋值
OK OK
增量赋值
3
6
foobarfoobar
条件语句
True
True3
True
True
True
True
Right
Right2
断言
循环
1
2
3
4
this
is
an
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]
0
1
2
3
4
y corresponds to  2
x corresponds to  1
z corresponds to  3
anne is 12 years old
bett is 34 years old
ccc is 56 years old
dadd is 78 years old
[('anne', 12), ('bett', 34), ('ccc', 56), ('dadd', 78)]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
['jj', 'bb', 'cc', 'jj', 'jj']
[1, 2, 3, 3, 5]
['!', 'd', 'l', 'r', 'o', 'w', ',', 'o', 'l', 'l', 'e', 'H']
!dlrow ,olleH
跳出循环
81
列表推导式
[0, 1, 4, 9, 16]
[0, 9]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
Error
[1, 4, 9, 16, 25]
[(1, 6), (2, 7), (3, None), (4, None), (5, None)]


Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值