学习「“笨方法”学Python 3」第三篇

习题21 函数可以返回某些东西

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def add(a, b):
print(f"ADDING {a} + {b}")
return a + b

def substract(a, b):
print(f"SUBSTRACTING {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 = substract(78, 4)
weight = multiply(90 , 2)
iq = divide(100, 2)

print(f"Age:{age}, Height:{height}, Weight:{weight}, Iq:{iq}")

print("Here is a puzzle.")

what = add(age, substract(height, multiply(weight,divide(iq, 2)))) #从里到外

print("That's becomes: ", what , "Can u do it by hand? ")

习题22 到现在为止你都学到了什么

1
2
3
#回顾之前遇到的每一个词和每一个符号 
#说明他们的作用
#做一份列表

习题23 字符串、字节串和字符编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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, error)

def print_line(line, encoding, errors):
next_lang = line.strip()
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)

习题24 更多的练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
print("Let's practice everything. ")
print('You\'d need to know \'bout escapes with \\ that do:') #\后分别显示‘ ’ \
print('\n newlines and \t tabs. ') #换行和水平制表,注意\n后面有个空格

poem = """ #'''和下一行不在同一行,因此这一行有个空格
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehand passion from intuition
and requires an expanation
\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)

print("With a staring point of: {}".format(start_point)) #调用方法,方法里有参数

print(f"We'd have {beans}, {jars} jars, and {crates} crates. ")

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point)

print("We'd have {} beans , {}jars, and {} crates.".format(*formula)) #因为函数有返回值

习题25 更多更多的练习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
print("Let's practice everything. ")
print('You\'d need to know \'bout escapes with \\ that do:') #\后分别显示‘ ’ \
print('\n newlines and \t tabs. ') #换行和水平制表,注意\n后面有个空格

poem = """ #'''和下一行不在同一行,因此这一行有个空格
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehand passion from intuition
and requires an expanation
\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)

print("With a staring point of: {}".format(start_point)) #调用方法,方法里有参数

print(f"We'd have {beans}, {jars} jars, and {crates} crates. ")

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point)

print("We'd have {} beans , {}jars, and {} crates.".format(*formula)) #因为函数有返回值

习题26 恭喜你,现在j可以考试了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weigh?", end=' ')
weight = input()

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

script, filename = argv

txt = open(filenme)

print("Here's your file {filename}:")
print(tx.read())

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again_read())


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 explanation
\n\t\twhere there is none.
"""

print("--------------")
print(poem)
print("--------------")

习题27 记住逻辑关系

1
2
#and:与(既...又 全true才true
#or:或(有true即true

习题28 布尔表达式练习

1
print(3 != 4 and not ("testing" != "test" or "Python" == "Python"))

习题29 if语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#if下一行4个空格缩进
#4个空格常被作为缩进排版的一个单位。缩进的确切解释并未详细指定(使用空格还是tab).Tab一定要设置为8个空格 (而非4个)。
people = 20
cats = 30
dogs = 15

if people < cats: #20<30? 1 打印
print("Too many cats!The world is doomed! ")

if people > cats: #20>30? 0
print("Not many cats!The world is saved! ")

if people < dogs: #20<15? 0
print("The world is drooled on! ")

if people > dogs: #20>15 1 打印
print("The world is dry! ")

dogs += 5 #20

if people >= dogs: #20>=20 1 打印
print("People are greater than or equal to dogs. ")

if people <= dogs: #20<=20 1 打印
print("People are less than or equal to dogs. ")
if people == dogs: #20=20 1 打印
print("People are dogs. ")

习题30 else和if

#if elif else后面都有:
people = 30
cars = 40
trucks = 15

if cars > people:
    print("We should take the cars. ")
elif cars < people:
    print("We should not take the cars. ")
else:
    print("We can't decide. ")

if trucks > cars:
    print(" That's too many trucks. ")
elif trucks < cars: #if不满足看elif满不满足,而不是else(先elif再else 并列就按先后顺序来
    print("Maybe we coule take the trucks. ")
else:
    print("We still can't decide. ")

if people > trucks:
    print("Alright , let's just take the trucks. ")
else:
    print("Fine, let's stay home then. ")