练习24--更多练习

一 复习内容

1 转义字符 :\t  \n  \'  \\ 

2 format格式化函数 ——三种不同的打印方法

  • "{}".format(变量名)
  • f"{变量名}"
  • "{}{}{}".format(*元组)

3 三引号的使用:程序头部或者尾部用来做注释(文档字符串),代码中间使用并将其赋给一个变量

4 算术运算符  + - * /

5 定义函数并设置返回值  def

6 函数调用  函数名(参数)

二 代码及运算结果

1 ex24.py文件

print("Let's practice everthing.")                                    # 打印一些提示信息
print('You\'d need to know \'bout escapes with \\ that do:')
print('\n newlines and \t tabs.')

# 用三引号定义一首诗
poem = """
    \tThe lovely world
   with logic so firmly planted
         cannot discern \n       the needs of love
 nor comprehend passion from intuition
    and requires an explanation
 \n\twhere there is none
"""
# 将诗句的内容打印出来
print("----------------------------------")
print(poem)
print("----------------------------------")

# 完成数学运算
five = 10 - 2 + 3 - 6
print(f"This should be five:{five}")

# 定义一个secret_formula函数,并返回三个变量的值
def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans,jars,crates

# 调用secret_formula函数,并将其函数返回值分别赋给beans,jars,crates
start_point = 10000
beans,jars,crates = secret_formula(start_point)

# 使用三种不同的format格式化函数输出方法
# remember that this is another way to format a string
print("With a starting point of: {}".format(start_point))                          # "{}".format(变量名)
# is's just like with an f"" string
print(f"We have {beans} beans,{jars} jars, and {crates}crates.")                   # f"{变量名}"

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point)
print(type(formula))
# this is an easy way to apply a list to a format string
print("W'd have {}beans,{}jars,and {} crates.".format(*formula))                   #"{}{}{}".format(*元组)

2 运行结果

PS E:\3_work\4_python\2_code_python\02_LearnPythonTheHardWay> python ex24.py
Let's practice everthing.
You'd need to know 'bout escapes with \ that do:

 newlines and    tabs.
----------------------------------

        The lovely world
   with logic so firmly planted
         cannot discern
       the needs of love
 nor comprehend passion from intuition
    and requires an explanation

        where there is none

----------------------------------
This should be five:5
With a starting point of: 10000
We have 5000000 beans,5000.0 jars, and 50.0crates.
We can also do that this way:
<class 'tuple'>
W'd have 500000.0beans,500.0jars,and 5.0 crates.
posted @ 2020-07-08 12:39  洛兰123  阅读(187)  评论(0编辑  收藏  举报