00:00:00 - Introduction
00:01:01 - Python
print("hello world")
与c的显著差异
1.不必显式包含标准库
2.不再需要定义main函数
00:07:24 - Speller
00:13:41 - Filter
from PIL import Image, ImageFilter
before = Image.open("bridge.jpg")
after = before.filter(ImageFilter.BoxBlur(10))
after.save("out.bmp")
00:17:31 - Face Recognition
00:20:53 - Functions
在c中安全地获取字符串非常烦人,c中使用cs50的get_string很有用,但是python中只是使用input()函数
# from cs50 import get_string
# answer = get_string("what is your name?:")
# print("hello," + answer)
# f格式字符串
answer = input("what is your name:")
print(f"hello,{answer}")
from cs50 import get_int
x = get_int("x:")
y = get_int("y:")
print(x + y)
00:33:24 - Types
00:34:47 - Calculator
x = int(input("x:"))
y = int(input("y:"))
print(x + y)
00:44:45 - Conditionals
在python中缩进很重要
注意是elif
00:48:05 - Compare
s = input("do you agree?")
if s in ["y", "Y", "yes"]:
print("agreed")
elif s in ["n", "N"]:
print("not agreed")
00:57:18 - Object-Oriented Programming 面向对象编程
c语言的tolower或toupeer只能传入一个字符
在python中str也是对象,对象不仅可以包含值或属性,还可以内置功能,字符串内部内置函数
s = input("do you agree?").lower()
if s in ["y","yes"]:
print("agreed")
elif s in ["n", "no"]:
print("not agreed")
01:02:54 - Loops
range() 是一种pythonic的方式,不一定是做某事的唯一方法,但是是python社区的共识
下划线_表明它是一个变量,并且需要它来实现for循环
True在python中首字母大写
python可以循环任何可以迭代的内容。比如字符串是可以迭代的
for _ in range(3)
print("hello world")
named parameters ,print的结束参数默认总是\n,如果不想换行,可以
print("hello world", end = "")
而print()可以自动添加一个换行
s = input('before: ')
print("after: ", end = "")
for c in s:
print(c.upper(), end = "")
print()
upper()也是每个字符串附带的方法,不需要单独每个字符调用它
s = input('before: ')
print("after: ", end = "")
print(s.upper())
01:13:51 - Meow
尽管在python中不需要有一个main(),但在传统中有一个main()函数,必须自己调用该函数,不会像c中被神奇地调用,python要做的最后一件事是调用main()
def main():
meow(3)
def meow(n):
for i in range(n):
print("meow")
main()
01:21:20 - Truncation
python不会截断,如1/3可以得到python中的浮点数,浮点会存在不精确的问题,但是整数溢出不会出现在python中
01:25:54 - Exceptions
python支持异常,异常称为错误值,ValueError
def get_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
# print("not an integer")
pass
def main():
x = get_int("x: ")
y = get_int("y: ")
print(x + y)
main()
01:33:22 - Mario
from cs50 import get_int
height = get_int("height:")
for i in range(height):
for j in range(height):
print("#",end = "")
print()
01:39:44 - Lists
列表的真正好处是内存会自动处理,python中列表更像是链表。list也是对象,有自己的属性和函数
from cs50 import get_int
scores = []
for i in range(3):
score = get_int(f"score{i}: ")
scores.append(score)
average = sum(scores) / len(scores)
print(f"Average: {average:.4f}")
phonebook.py
names = ["cater","dc", "daichagn"]
name = input("name: ")
if name in names:
print("found")
else:
print("not found")
01:49:18 - Dictionaries
python中的字典本质上是一个哈希表,即键值对的集合
字典在编程中称为数据结构的瑞士军刀 ,可以将某物与某物联系起来
for-else
结构在一些场景下特别有用,例如在查找列表、元组或其他可迭代对象中是否存在特定元素时,若未找到则执行特定操作。
people = [
{'name':'cater', 'number':'23452'},
{'name':'dc', 'number':'564645'},
{'name':'dfgd', 'number':'456456'}
]
name = input('name:')
for person in people:
if person['name'] == name:
print(f"found number: {person['number']}")
break
else:
print('not found')
创建字典列表不断迭代
方式二:想列表一样使用字典,python会在键中查找该名称
people = {
'cater':'53456456',
'dc':'63456456',
'aoxue':'367457'
}
name = input('name:')
if name in people:
print(f'found number {people[name]}')
else:
print('not found')
01:59:58 - sys
与系统相关的库
要想在python中执行与命令行参数相关
from sys import argv
if len(argv) == 2:
print(f"hello, {argv[1]}")
else:
print("hello world")
02:04:04 - pip
pip install cowsay
import cowsay
cowsay.cow("this is cs50")
| this is cs50 |
============
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
src/python/ $
import qrcode
img = qrcode.make("https://blog.csdn.net/qq_36372352?spm=1000.2115.3001.5343")
img.save('qr.png')