Process-oriented programming and a variety of expressions

Process-oriented programming:

The core is the 'process' word, refers to the process is a problem-solving steps, that is, first dared to do what is
written in the programming ideas based program, designed like a factory assembly line, a mechanical way of thinking in

- Advantages : The process of complex issues, and further simplification
- drawbacks : the selection of a portion of the current programming, while the other parts will lead to the need to modify
the retractor situation as a whole, poor scalability

A triplet of expressions: if ... else ... can be compiled into a branch line.

语法:
    条件成立,返回左边的值 if 判断条件 else 条件不成立返回右边的值


#通过if...else语法
def max2(num1,num2):
    if num1>num2:
        return num1
    else:
        return num2
res = max2(10,20)
print(res)

#三元表达式
num1 = 10
num2 = 20
def max2(num1,num2):
    res = num1 if num1 > num2 else num2
    return res
res = max2(num1,num2)
print(res)

# 需求: 让用户输入用户名,输入的用户如果不是tank,为其后缀添加_DSB
username = input('请输入用户名:').strip()
new_username = username if username == 'tank' else username + '_DSB'
print(new_username)

List of formula: can generate a list of his party achieved

语法:
    list = [取出的每一个值添加到当前列表中、任意值 for 可迭代对象中取出的每一个值 in 可迭代对象
    # for的右边是循环次数,并且可以取出可迭代对象中每一个值
    # for的左边可以为当前列表添加值
    list = [值 for 可迭代对象中取出的每一个值 in 可迭代对象]
    list = [值 for 可迭代对象中取出的每一个值 in 可迭代对象 if 判断]
    
# 需求:将list中的值,依次取出,添加到l1中
list = [1,2,3,4]
l1 = []
for i in list:
    l1.append(i)
print(l1)

# 普通方式
new_list = []
for i in range (1,101):
    new_list.append(i)
print(new_list)

#列表生成器
list = [ i for i in range(1,5)]
print(list)
>>>[1,2,3,4]

list = [ 1 for i in range(1,5)]
print(list)
>>>[1,1,1,1]

list = [ 'baohan' for i in range(1,5)]
print(list)
>>>['baohan','baohan','baohan','baohan']

#需求:将name_list列表中的每一个人后缀都添加_dsb
name_list = ['jason', '饼哥', 'sean', 'egon']
new_name_list = [name + '_dsb' for name in name_list]
print(new_name_list)

Generator expressions (Builder formula):

- 列表生成式:  若数据量小时采用
    [line for line in range(1, 6)] ---> [1, 2, 3, 4, 5]
    优点:可以依赖于索引取值,取值方便
    缺点:浪费资源
    
- 生成器生成式:  若数据量过大时采用
    () ---> 返回生成器
    (line for line in range(1, 6)) ---> g生成器(1, 2, 3, 4, 5)
    优点:节省资源
    缺点:取值不方便

# 生成一个有1000个值的生成器
g = (line for line in range(1, 1000001))
print(g)           # <generator object <genexpr> at 0x00000203262318E0>

Guess you like

Origin www.cnblogs.com/baohanblog/p/12143042.html