当看到上周的 PSET 被 David 用 Python 迅速写出时,我脑海中持之以恒地回荡着一句俗气话语「你以为的岁月静好是有人替你负重前行」,谢谢你 Python,谢谢你帮我解决了 malloc、pointer 等等一堆破事!
and 这堂课上 David 借举例之口 show off 了一下他的 Spanish,我怀疑他就是单纯地想 show off 一下。
圣诞周了,cs50 的主页换成了圣诞红,还放了 「congratulations」的 gif——but 我还没有完成 final work,well,就当是提前祝贺我吧……
Python Syntax
-
在 Python 中 print 一个变量:
print( f "hello, {answer}" ) -
在 Python 中,+1 的写法:
counter = counter + 1counter += 1 -
if 条件句的写法:
if x < y :
print ( "x is less than y" )
elif x > y :
print ( " x is greater than y" )
else
print ( " x is equal to y" )
- 创建 Foever loop:
while True:
print("Hello,world")
注意:True和False在 Python 中都要大写首字母。
- 创建循环:
for i in range(3)
print("hello, world")
Python中的数据类型
| Python中数据类型 | Definition | 是否在C中存在 |
|---|---|---|
| bool,True,False | / | Yes |
| float | real numbers | Yes |
| int | integers | Yes |
| str | strings | Yes |
| range | sequence of numbers | No |
| list | sequence of mutable 可变的 values | No |
| tuple | sequence of immutable 不可变的values | No |
| dict | dictionaries, collection of key/value pairs, 类似 hash table | No |
| set | collections of unique values, or values without duplicates | No |
Libraries
运行 python 文件:python hello.py
这里用 Python 把上周的 PSET5 Speller Check 重新写了一遍。
解法和注释已经更新到了 PSET 系列的笔记中。
Input, Conditions
在 Python 中,input 会被默认为一个 strings
x = input("x: ")
y = input("y: ")
print(x + y)
运行这段程序得到的结果:
$ python calculator.py
x: 1
y: 2
12
为了得到正确的答案,应该把 x、y 两个 string 是变为 integer。 修改:
x = int(input("x: "))
y = int(input("y: "))
print(x + y)
Lists, Strings
- 创建 list
scores = [72,73,33]
average = sum(scores) / len(scores)
print(f"Average: {average}")
两个可以注意的 function
sumlen
- 在 list 中增加 item
from cs50 import get_int
scores = [] ## 创建一个空的 list
for i in range(3):
score = get_int( "Score:" )
scores.append(score)
## 注:`append`功能,可以在一个 list 中增加 values
average = sum(scores) / len(scores)
print(f"Average:{average}")
- 合并两个 list
一个 list:scores
一个 list:score
可以使用
scores += [score]来把两个 list join 到一起。
Command-line Arguments, Exit Codes
from sys import argv
if len(argv) == 2:
print(f"hello, {argv[1]}")
else:
print("hello,world")
argv这个功能在 python 中,需要通过导入sys来实现;argv是一个 list,我们可以得到这个 list 中任意位数的 item;
for arg in argv[1:] :
print(arg)
- 一个比较有意思的功能:slice the list;
- 当我们输入
argv[1:]后,可以得到 1 位置以后的所有内容。
for arg in argv[:-1]:
print(arg)
- 通过
argv[:-1],可以获得 list 中除了最后一个元素以外的所有内容。
Algorithms 算法
- 在 python 中寻找某一个元素,既可以找一个 integer,也可以找一个 string;
import sys
numbers = [4,6,8,3,5]
if 4 in numbers:
print("Found")
sys.exit(0)
print("Not found")
sys.exit(1)
## 省略上面的
names = ["Bill","Charlie", "Fred", "George", "Ginny", "Percy", "Ron"]
if "Ron" in names:
print("Found")
sys.exit(0)
print("Not found")
sys.exit(1)
- 重点:创建一个 dictionary
- 回顾:dictionary,是 一堆 key 对应相应的 value;
from cs50 import get_string
## 创建了一个名为 people 的 dictionary,名字是 key,后面的电话号码是 value
people = {
"Carter": "+1-617-495-1000",
"David": "+1-949-468-2750"
}
name = get_string("Name: ")
if name in people:
number = people[name]
print(f"Number: {number}")
- 在 dictionary 中寻找特定的 item 时,我们通过 key 来匹配,然后输出时,可以输出对应的 value(即
people[value])
Files
一个例子:写入 csv 文件
import csv
from cs50 import get_string
file = open ("phonebook.csv", "a")
name = get_string("Name: ")
number = get_string("Number: ")
接下来有两种方式,将得到的 name 和 number 写入 csv 文件中。 第一种:
writer = csv.writer(file)
writer.writerow([name, number])
file.close()
第二种:
with open( "phonebook.csv", "a") as file:
writer = csv.writer(file)
writer.writerow([name, number])
More libraries
老师演示了一些其他的 python library。
包括face_recognition,speech recognition,qrcode等等,实现了包括人脸识别、语音识别、二维码生成以及动态追踪等功能。
啊!代码真神奇。
结尾的时候老师展示了如何用 deep fake 来用其他人的脸模拟某一输入源的动作,很喜欢很喜欢这节课最后的那段话。关于如何 ethically 使用技术。
很喜欢 art of programming 的说法。在 cs50 的课堂上我真真切切感觉到了 coding 超乎实用性以外的美感、哲学意味和艺术性。Thanks David though you might never see my thanks lol.