文章目录
Exercise 21. Functions Can Return Something
这一节也很简单, 只讲了函数的返回值,示例:
def add(a, b):
print(f"ADDING {a} + {b}")
return a + b
def subtract(a, b):
print(f"SUBTRACTING {a} - {b}")
return a - b
def multiply(a, b):
print(f"MULTIPLYING {a} * {b}")
return a * b
def divide(a, b):
print(f"DIVIDING {a} / {b}")
return a / b
print("Let's do some math with just functions!")
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print(f"Age: {age}, Height: {height}, Weight: {weight}, IQ: {iq}")
# A puzzle for the extra credit, type it in anyway.
print("Here is a puzzle.")
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print("That becomes: ", what, "Can you do it by hand?")
Exercise 22. What Do You Know So Far?
这一节是让大家复习一下学过的各种符号,这里不再赘述了。
Exercise 23. Strings, Bytes, and Character Encodings
这一节比较复杂,讲了编码和解码,一定要记住string是要encode,而bytes是要decode,但是power shell有乱码不知道怎么处理,示例:
import sys
script, encoding, error = sys.argv
def main(language_file, encoding, errors):
line = language_file.readline()
if line:
print_line(line, encoding, errors)
return main(language_file, encoding, errors)
def print_line(line, encoding, errors):
next_lang = line.strip()# 掐头去尾,将空格和tab等头尾字符去掉,包括\r,\t,\n等
raw_bytes = next_lang.encode(encoding, errors = errors)
cooked_string = raw_bytes.decode(encoding, errors = errors)
print(raw_bytes, "<===>", cooked_string)
languages = open("languages.txt", encoding = "utf-8")
main(languages, encoding, error)
Exercise 24. More Practice
这一节作者带我们回顾了以前学过的python基础知识,不再赘述
print("Let's practice everything.")
print('You\'d need to know \'bout escape 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\t\twhere there is none.
"""
print("--------------")
print(poem)
print("--------------")
five = 10 - 2 + 3 - 6
print(f"This should be five : {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)
# remeber that this is another way to format a string
print("With a starting point of: {}".format(start_point))
# it's just like with an f"" cooked_string
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
print("We can also do that this way:")
formula = secret_formula(start_point)
# this is an easy way to apply a list to a format string
print("We'd have {} beans, {} jars, and {} crates.".format(*formula))
Exercise 25. Even More Practice
这一节主要讲怎么自己写包,其实就是写一个python文件,里面只有函数,示例:
def break_words(stuff):
"""This function will break up words for us.分词函数"""
words = stuff.split(' ')
return words
def sort_words(words):
"""Sorts the words.排序函数"""
return sorted(words)
def print_first_word(words):
"""Prints the first word after poping it off.去掉首项,并打印首项"""
word = words.pop(0)
print(word)
def print_last_word(words):
"""Prints the last word after poping 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)
然后打开交互式的python环境,使用import 文件名
即可导入该文件。
但是每次运行其中的函数时都需要使用文件名.函数名
的格式,比较繁琐;有一种快捷的方法就是,直接from ex25 import *
导入所有的函数即可,这样就可以直接使用函数名来调用了。
注意:help(文件名)
可以得到其帮助文档,也就是“”“
之间的注释
基础部分完成啦!!!