美文网首页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的优雅

    Elegence @ddm coding camp字数300阅读7 Bool运算符 %d zipmin max I...

  • Python使用format与f-string数字格式化

    Python使用format与f-string数字格式化 输出:

  • PYTHON应用5大领域,哪个领域薪酬最高?

    Python的定位是“优雅”、“明确”、“简单”,所以Python程序看上去总是简单易懂,初学者学Python,不...

  • Python格式化字符串f-string

    f-string,亦称为格式化字符串常量(formatted string literals),是Python3....

  • Pip你需要城墙般的锅盖

    作为一个单纯无害的python初学者,我一直沉浸于python宣称的简介,好用,优雅迷人的slogan中,直到有一...

  • 优雅从容

    我不知道什么是优雅从容 尤其是来自贫瘠农村的孩子 也就更不知道什么是优雅从容 可我很喜欢老妈对我的教导 尽管在现代...

  • 优雅从容

    在任何时候都保持优雅从容始终境界,需要不断的修炼。

  • 优雅从容

    那年,那月,那时光,那风景,都已经悄然流逝。那情,那一纸相思,已辗转千山万水,风干在遗失的完美里,清浅流年里,我...

  • 优雅从容

    如果有人,能从高处俯瞰我们,他会看到,这个世界上到处都是行色匆匆的、汗流浃背、疲惫不堪的人流,以及他们姗姗来迟、不...

  • 优雅从容

    沉淀后 再出发 虽过程漫长 却十分必要 后来明白 不是每次伤害 都要还之以痛 不是所有脆弱 都该惭愧自责 世事沧桑...

网友评论

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

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