美文网首页python
f-string: 初学者从容感受python的优雅

f-string: 初学者从容感受python的优雅

作者: Python_Camp | 来源:发表于2020-03-13 15:55 被阅读0次

    Elegence

    @ddm coding camp 字数 300 阅读 7

    Bool运算符  %d  zipmin max

    I always thought that my old friend John was rather richer than he looked, but I never knew exactly how much money he actually had. One day (as I was plying him with questions) he said:

    "Imagine I have between m and n Zloty..." (or did he say Quetzal? I can't remember!)

    "If I were to buy 9 cars costing c each, I'd only have 1 Zloty (or was it Meticals?) left."

    "And if I were to buy 7 boats at b each, I'd only have 2 Ringglets (or was it Zloty?) left."

    Could you tell me in each possible case:

    how much money f he could possibly have ?

    the cost c of a car?

    the cost b of a boat?

    So, I will have a better idea about his fortune. Note that if m-n is big enough, you might have a lot of possible answers.

    Each answer should be given as ["M: f", "B: b", "C: c"] and all the answers as [ ["M: f", "B: b", "C: c"], ... ]. "M" stands for money, "B" for boats, "C" for cars.

    Note: m, n, f, b, c are positive integers, where 0 <= m <= n or m >= n >= 0. m and n are inclusive.

    班主任拨出一笔钱购买文具盒,任务交给我后,我先算了一笔帐,如果买7元一个的剩2元,买9元一个的剩1元。

    老师取出一张纸币给我。请问老师给了我多少钱,如果分别买7元和9元的文具盒各能买到几个?

    输入参数:自由定义

    输出格式:例如['M:37', 'F:5', 'N:4']

    “三个都是正整数:M是老师给我的钱,F是5元一个文具盒的数量,N是9元一个文具盒的数量。注意输出示例不能完全满足题意!”

    # 假设预算数值的合理范围10,20,50,100

    # 人民币发行只有这几种面值超过价格

    def howmuch(cash):    

    for i in cash:

            if (i-1) % 9 == 0 and (i-2) % 7 == 0:

    f,n = (i-2)//7,(i-1)//9           

        return [f'{x+str(y)}'for x, y in zip(['M:','F:','S:'],[i,f,n])]

    #[f'{x + str(y)}' for x, y in zip(['M:', 'F:', 'S:'], [f, f,n])

    cash = [10,20,50,100]

    输出结果 ['M:100', 'F:14', 'S:11']

    下面的写法列表推导式更简洁!

    def howmuch(cash):

        return [['M:%d'%i,'B:%d'%(i/7),'C:%d'%(i/9)] for i in cash if i%7 == 2 and i%9 == 1]

    请参考下面链接更多f-string格式输出的用法!

    name="Eric"

    "Hello, %s."%name

    'Hello, Eric.'

    F"Hello, {name}. You are {age}."

    'Hello, Eric. You are 74.'

    name = "Eric Idle"

    f"{to_lowercase(name)} is funny."

    'eric idle is funny.'

    name = "Eric"

    age = 74

    "Hello, %s. You are %s." % (name, age)

    class类:f“class”输出

    class Comedian:

        def __init__(self, first_name, last_name, age):

            self.first_name = first_name

            self.last_name = last_name

            self.age = age

        def __str__(self):

            return f"{self.first_name} {self.last_name} is {self.age}."

        def __repr__(self):

            return f"{self.first_name} {self.last_name} is {self.age}. Surprise!"

    new_comedian = Comedian("Eric", "Idle", "74")

    f"{new_comedian}"

    'Eric Idle is 74.'

    多行合并输出

    name = "Eric"

    profession = "comedian"

    affiliation = "Monty Python"

    message = (

        f"Hi {name}. "

        f"You are a {profession}. "

        f"You were in {affiliation}."

        ... )

    message

    'Hi Eric. You are a comedian. You were in Monty Python.'

    比较f-string和formate执行速度

    >>>import timeit

    >>>timeit.timeit("""name = "Eric"

    ... age = 74

    ... '%s is %s.' % (name, age)""", number = 10000)

    0.003324444866599663

    >>> timeit.timeit("""name = "Eric"

    ... age = 74

    ... '{} is {}.'.format(name, age)""", number = 10000)

    0.004242089427570761

    >>> timeit.timeit("""name = "Eric"

    ... age = 74

    ... f'{name} is {age}.'""", number = 10000)

    0.0024820892040722242

    透视字符串拼接f-strings的效率

    ▽ 大咖说 ▽

    "It’s not at all important to get it right the first time. It’s vitally important to get it right the last time.” - The Pragmatic Programmer

    "第一次就能搞定代码并不重要。更重要的问题,最后一次是正确的。”- 务实的程序员

    "Every great developer you know got there by solving problems they were unqualified to solve until they actually did it." - Patrick McKenzie

    “每一个伟大的开发者都是解决他们没有能力解决的问题开始的,直到他们真正做到了。

    -Patrick McKenzie

    相关文章

      网友评论

        本文标题:f-string: 初学者从容感受python的优雅

        本文链接:https://www.haomeiwen.com/subject/hgvlshtx.html