#y = |x|
if x < 0:
    y = -x
else:
    y = x
##python不支持switch语句,故:
if condition_1:
    statement_1
elif condition_2:
    statement_2
...
elif condition_i:
    statement_i
else:
    statement_n

关于判断条件的常用总结:

if i != 0:
....
l = [2,3,4,5]
for item in l:
    print(item)
2
3
4
5

for i in <iterable>:
    ...

#字典本身只有键可迭代,遍历值时,得通过values()/items()实现,values()返回字典值的集合,items()返回键值对的集合

d = {'name':'jason','do':'2000-01-01','gender':'male'}
for k in d:#遍历字典的键
    print(k)

for v in d.values():#字典的值
    print(v)
jason
20001-01-01
male

for k,v in d.items():
    print('key: {},value: {}'.format(k,v))
key:name,value:jason
....


l = [2,3,4,5,6,7]
for index in range(0,len(1)):
    if index < 5:
        print(l[index])
2
3
4
5
6
#通过python的内置函数
l = [2,3,4,5,6,7]
for index ,item in enumberate(1):
    if index < 5:
        print(item)
2
3
4
5
7
#name_price:产品到价格的映射字典
#name_color:产品到颜色的字典
#需求找出价格低于1000,同时颜色不是红色的颜色和名称的组合
#1
for name,price in name_price.items():
    if price < 1000:
        if name in name_color:
            for color in name_color[name]:
                if color !='red'
                    print('name: {},color:{}'.format(name,color))
        else:
            print(name:{},color:{}'.format(name,color))
#2
for name,price in name_price.items():
    if price >= 1000:
        continue
    if name not in name_color:
        print('name:{},color:{}'.format(name,'None'))
    for color in name_color[name]:
        if color == 'red':
            continue
        print('name: {},color: {}'.format(name,color))

#while
while condition

l = [1,2,3,4]
index = 0
while index < len(1):
    print(l[index])
    index +=1

#while场景交互式问答系统,根据用户输入的文字,实现
while True:
    try:
        text = input('Please enter your questions,enter "q" to exit')
        if text =='q'
            print('EXit system')
            break
        ...
        print(respones)
    except as err:
        print('Encounted error:{}'.format(err))
        break

#
i = 0
while i < 100000
    i +=1
for i in range(0,100000)
    pass
#哪个效率高 

条件与循环的复用

expression if condition else expression2 for item in iterable

#分解如下
for item in iterable:
    if condition:
        expression
    if condition:
        expression2

#expression if condition else expression2 for item in iterable
#no else 
expression for item in iterable if condition

#y = 2 * |x| + 5,给定集合x的数据点
y = [value * 2 + 5 if value > 0 else - value * 2 + 5 for value in x]

#完整语句通过逗号分隔,去掉首字符,并过滤掉长度小于4的单词,最后返回单词组的列表
text = ' Today, is,Sunday'
text_list = [s.strip() for s in text.split(',') if len(s.strip()) > 4]
print(text_list)
['Today','Sunday']
#给定x,y返回x,y中所有组成的元组
[(xx,yy) for xx in x for yy in y if xx !=yy]

l = []
for xx in x:
    for yy in y:
        if xx !=yy:
            l.append((xx,yy))


###给定两个列表,针对values中的每一组子列表value,输出其和attributes中的键对应后的字典,最后返回字典组成的列表

attributes = ['name', 'dob', 'gender']
values = [['jason', '2000-01-01', 'male'], 
['mike', '1999-01-01', 'male'],
['nancy', '2001-02-01', 'female']
]

# expected output:
[{'name': 'jason', 'dob': '2000-01-01', 'gender': 'male'}, 
{'name': 'mike', 'dob': '1999-01-01', 'gender': 'male'}, 
{'name': 'nancy', 'dob': '2001-02-01', 'gender': 'female'}]


#
list = []
for value in values:
    dic = {}
    for index,key in enumerate(attributes):
        dic[key] = value[index]
    list.append(dic)
    print(list)
#
[{key:value[index] for index,key in enumerate(attributes)} for value in values ]