python学习笔记 - 第一阶段总结(ex22-26)

小节 ex22-26:回顾总结,读代码做练习

ex22:到现在你学到了哪些东西?
请回顾之前内容,总结出一个记忆列表

ex23:读代码
花一周的时间,在网上阅读代码

提供几个有用的站点 描述
bitbucket.org
github.com
launchpad.net
koders.com

ex24: 更多练习
琐碎知识点的练习
目前 Python 基础知识足够了,接下来就是学编程原理和做练习

# 知识点:一次性给多个变量赋值
def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars,crates
start_point = 10000
beans,jars,crates = secret_formula(start_point)

ex25:更多更多的练习
函数和变量的练习
学会这个:在python 编译器里,用交互的方式和.py 作交流

案列:
# --coding:utf-8--
# 分词:用空格隔开字符串(返回单词数组)
def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

# 单词排序:sorted,输入单词数组,返回排序后的单词数组
def sort_words(words):
    """Sorts the words."""
    return sorted(words)

# 打印第一个单词pop
def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print(word)

# 打印最后一个单词 Pop
def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print(word)

# 句子排序:输入句子,先分词再排序,输出单词数组
def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

# 打印句子的第一个和最后一个单词:【句子分词】,再打印
def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

# 打印句子第一个和最后一个排序的单词:【句子分词排序】,再打印
def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)
    

终端运行(编译器):
在这里插入图片描述
分析:
在第 5 行你将自己的 ex25.py 执行了 import,和你做过的其它 import 一样。在 import 的时候你不需要加 .py 后缀。这个过程里,你把 ex25.py 当做了一个“模组(module)”来使用,你在这个模组里定义的函数也可以直接调用出来。
第 6 行你创建了一个用来处理的“句子(sentence)”。
第 7 行你使用 ex25 调用你的第一个函数 ex25.break_words。其中的 . (dot, period)符号可以告诉 Python:“嗨,我要运行 ex25 里的哪个个叫 break_words 的函数!”

ex26:恭喜你,现在可以考试了!
综合上述例子,排错练习

自己写的:
# --coding:utf-8--
# part1:函数打印
# 分词:以空格分词,返回元组list
def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

# 排序:元组list进行排序
def sort_words(words):
    """Sorts the words."""
    return sorted(words)

# 错误纠正:1.函数后面的冒号不能丢,2.print打印在python3中使用方法是函数print() 3.排序函数是pop不是poop
def print_first_word(words):
    """Prints the first word after popping it off."""
    # word = words.poop(0)
    word = words.pop(0)
    print(word)

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

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)


# part2: 打印字符串,转义字符
print("Let's practice everything.")
print('You\'d need to know \'bout escapes with \\ that do \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 explantion
\n\t\twhere there is none.
"""
print("--------------")
print(poem)
print("--------------")


# part3:算术运算和函数
five = 10 - 2 + 3 - 5
print("This should be five: %s" % five)

# 算术除法是撇,赋值是单等号
def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

start_point = 10000
beans,jars,crates = secret_formula(start_point)
print("With a starting point of: %d" % start_point)
print("We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates))

start_point = start_point / 10
print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point))

# part4:编译器用法,调用函数
import ex25
sentence = "All god\tthings come to those who weight."

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

ex25.print_first_word(words)
ex25.print_last_word(words)
ex25.print_first_word(sorted_words)
ex25.print_last_word(sorted_words)

sorted_words = ex25.sort_sentence(sentence)
print(sorted_words)

ex25.print_first_and_last(sentence)
ex25.print_first_and_last_sorted(sentence)

猜你喜欢

转载自blog.csdn.net/xingxing_sun/article/details/87919437