# Doctest from doctest
import testmod
defMul(x, y)->int:"""This function returns the mul of x and y argumets incoking the function followed by expected output:
>>> Mul(4, 5)20>>> Mul(19, 20)39"""return x * y
testmod(name='Mul')# 输出如下:"""**********************************************************************
File "main.py", line 10,in Mul.Mul Failed example:Mul(19,20) Expected:39 Got:380**********************************************************************1 items had failures:1 of
2in Mul.Mul
***Test Failed***1 failures."""
# for-else for x inrange(5):print(x)else:print("Loop Completed")# executed # while-else
i =0while i <5:breakelse:print("Loop Completed")# Not executed
8.f-string的强大
a ="Python"
b ="Job"# Way 1
string ="I looking for a {} Programming {}".format(a, b)print(string)# I looking for a Python Programming Job #Way 2
string =f"I looking for a {a} Programming {b}"print(string)# I looking for a Python Programming Job
x =5if2>4else2print(x)# 2
y =10if32>41else24print(y)# 24
11.参数解包
您可以解压缩函数中的任何可迭代数据参数。看看下面的代码示例:
deffunc(a, b, c):print(a, b, c)
x =[1,2,3]
y ={'a':1,'b':2,'c':3}
func(*x)# 1 2 3
func(**y)# 1 2 3
12.呼唤世界(没啥用)
import __hello__
# 你猜输出啥? # other code import os
print(os)# <module 'os' from '/usr/lib/python3.6/os.py'>
13.多行字符串
此功能将向您展示如何编写没有三重引号的多行字符串。看看下面的代码示例:
# 多行字符串
str1="Are you looking for free Coding " \ "Learning material then " \ "welcome to py2fun.com"print(str1)# Are you looking for free Coding Learning material then welcome to Medium.com # 三重引号字符串
str2 ="""Are you looking for free Coding Learning material then welcome to py2fun.com """print(str2)#和上面的是不同的,换行也会被输出。