《笨方法学PYTHON》——twelfthlesson

习题24:更多练习

print("Let's practice everything.")
#列举了几个转义字符
print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')
#多行显示,可以使用6个单引号或者6个双引号,同时也有注释多行的意思,灵活使用
poem = """
\tThe lovely world
with logic so firmly planted
connot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""
print("------------------")
print(poem)
print("------------------")
five = 10 - 2 + 3 - 6
print("This should be five:%s" % five)

#这里注意一下,我之前说过,只要使用了除法,得到的结果必然是浮点型,但后面输出你会发现是整型,为什么呢?那是因为作者使用的%d,改为%f试试
def secretFormula(started):
    jellyBeans = started * 500
    jars = jellyBeans / 1000
    crates = jars / 100
    return jellyBeans, jars, crates


startPoint = 10000
#函数是返回的三个变量,所以使用三个变量进行接收
beans, jars, crates = secretFormula(startPoint)
print("With a starting point of:%d" % startPoint)
print("We'd have %d beans,%d jars,and %d crates." % (beans, jars, crates))
startPoint = startPoint / 10
print("We can also do that this way:")
print("We'd have %d beans,%d jars,and %d crates." % secretFormula(startPoint))
Let's practice everything.
You'd need to know 'bout escapes with \ that do 
 newlines and 	 tabs.
------------------

	The lovely world
with logic so firmly planted
connot 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'd have 5000000 beans,5000 jars,and 50 crates.
We can also do that this way:
We'd have 500000 beans,500 jars,and 5 crates.

1. 记得仔细检查结果,从后往前倒着检查,把代码朗读出来,在不清楚的位置加上注释。

2. 故意把代码改错,运行并检查会发生什么样的错误,并且确认你有能力改正这些错误。

如果到了这儿,你照着打还是不能一次性打对,那就有必要做一下这道题了。这个打错不是说你敲键盘是否熟练的问题,个人觉得,是你有没有对关键字熟悉的原因,当然这中间输出内容比较多,也涉及到了简单英文单词是否熟练,但不管如何,如果到现在还是有很多错误,肯定需要自己制造错误来熟悉熟悉了。

习题25:更多更多的练习

def breakWords(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words


def sortWords(words):
    """Sorts the words."""
    return sorted(words)


def printFirstWord(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print(word)


def printLastWord(words):
    '''Prints the last word after popping it off.'''
    word = words.pop(-1)
    print(word)


def sortSentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = breakWords(sentence)
    return sortWords(words)


def printFirstAndLast(sentence):
    """Prints the first and last words of the sentence."""
    words = breakWords(sentence)
    printFirstWord(words)
    printLastWord(words)


def printFirstAndLastSorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sortSentence(sentence)
    printFirstWord(words)
    printLastWord(words)

打完有没有发现,全是函数的定义,根本没有调用,所以啥也没有。。。

printFirstAndLast("test sort def,and I love you!")
print("\n")
printFirstAndLastSorted("test sort def,and I love you!")
  • split(string)——分隔,一串字符串,以string进行分隔,返回的是数组
  • sorted(array)——对数组按ACSII的从小到大排序,返回排序好的数组,没有ed的时候没有返回,对原数组进行排序
  • pop()——数组列表的指针位置,0起始位,-1末位

1. 研究答案中没有分析过的行,找出它们的来龙去脉。确认自己明白了自己使用的是模组 ex25 中定义的函数。

2. 试着执行 help(ex25) 和 help(ex25.break_words) 。这是你得到模组帮助文档的方 式。所谓帮助文档就是你定义函数时放在 """ 之间的东西,它们也被称作 documentation comments (文档注解),后面你还会看到更多类似的东西。

3. 重复键入 ex25. 是很烦的一件事情。有一个捷径就是用 from ex25 import * 的方式导 入模组。这相当于说:“我要把 ex25 中所有的东西 import 进来。”程序员喜欢说这样的倒装 句,开一个新的会话,看看你所有的函数是不是已经在那里了。

4. 把你脚本里的内容逐行通过 python 编译器执行,看看会是什么样子。你可以执行CTRL-D (Windows 下是 CTRL-Z)来关闭编译器。

print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)

猜你喜欢

转载自blog.csdn.net/qq_41470573/article/details/84724281