bilibili速通

print( "a string" )

和c++不一样,不用加分号,python的缩进很重要,python读一行执行一行

python中print可以实现字符串拼接print("hello" + "jack" + "damn\n")

字符串用单引号还是双引号都可以,效果大部分情况下一样,如果字符串里面有引号,可以用\转义符

打印跨行内容可以用三引号

1
2
3
print("""hello,
how are you?
im good.""")

python变量的定义不需要说明类型,直接写名字并用等号进行赋值操作

python运算 + - / % , 为幂运算,例如2 * 3 = 8

math库,在文件开头导入import math,在使用时写上math.function name(...)

python中注释#,负责单行;””” “””三引号包裹,负责多行注释

字符串:可以用len函数获取字符串长度;[index]可以提取出该索引位置的字符

整数:int

浮点数:float

布尔类:True False

空值类: None

type函数可以获取数据类型

input("里面放字符串作为给用户的提示信息"),返回值为字符串

int("666")将字符串转化为整数

if语句

1
2
3
4
5
6
7
8
9
if [condition]:
[expression]
[expression]
elif [condition 2]:
[expression]
# elif equals else if
else:
[expression]
# 缩进决定了该语句的位置

逻辑判断 and or not ==not只能对一个操作对象进行运算==

列表 list = ["item 1", "item 2"]

list.append("item 3")往列表中间加元素

list.remove("item 3")删除原列表中的元素

python的列表可以存放不同类型的数据,列表也可以用len返回列表里面元素的数量,也可以用索引获取对应值,或进行修改

max min sorted

字典 contacts = {"key1" : "value 1", "key2" : "value 2" }

contacts["key1"]返回对应的value,注意key必须是不可变数据结构,因此不可以用list

但可以使用元组tuple tuple = ("name", 52)

contacts["new item"] = "new value",字典中会多出一个键值对

del contacts["key1"],删除键和对应的值

contacts.keys() 返回所有键

contacts.values() 返回所有值

contacts.items() 返回所有键值对

for循环

1
2
for 变量名 in 可迭代对象:
对每个变量进行操作

range(start value, end value, step) #不包括结束值,例如range(5, 10)只执行5 6 7 8 9,共5次, 步长表示每次跨几个数字,默认为1

while循环

1
2
while condition A:
expression B

格式化字符串

1
2
3
4
5
6
7
8
9
message_content = """
my name is {0},
my age is {1}.
my friend is {friend_name}.
""".format(name, age, friend_name)
message_content = f"""
my name is {name},
my age is {age}.
"""

函数

1
2
3
4
def calculate_sector(central_angle, radius):
sector_area = central_area / 360 * 3.14 * radius ** 2
print (f"the sector area is : {sector_area}")
return sector_area

1
2
3
4
5
6
7
8
9
10
class NameOfClass:
def _init_(self, name):
self.name = name
def getInfo(self):
print(f"my name is {self.name}")
# 子类的继承
class Son(Father):
def whatever(self):
Expression

文件操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 读文件
f = open("./data.txt", "r", encoding = "utf-8") # "r" 读取模式(只读),"w" 写入模式(只写),"a" 追加模式,"r+" 读写模式 且是append追加模式
print(f.read(10)) # 10表示读10字节
print(f.readline()) # 只读一行文件内容
line = f.readline()
while line != "":
print(line)
line = f.readline()
# f.readlines 返回全部文件内容组成的列表
lines = f.readlines()
for line in lines:
print(line)
f.close() # 关闭文件 释放资源
# 另一种打开方式
with open("./data.txt") as f:
OPERATION
# 缩进内为对该文件的操作,缩进外则自动关闭该文件

## 写文件
# 用w模式时如果原来的文件已经存在,会把原本的文件内容清空
with open("./data.txt", "w", encoding = "utf-8") as f:
f.write("Hello!\n")

异常处理

1
2
3
4
5
6
7
8
try:
user_input = int(input("please type in your password"))
except ValieError:
print("your not typing in numbers")
else:
print("well done")
finally:
print("this code will work in all ways")

高阶函数

传入函数参数时可以传入函数作为参数

匿名函数

calculate_and_print(7, lambda num : num * 5, print_with_vertical_bar)

(lambda num1, num2 : num1 + num2)(2, 3)

但注意匿名函数冒号后面只有一个语句,只能用于比较简单的情形


CS50P

Functions, Variables

Conditionals

Loops

Exceptions

Libraries

Unit Tests

File I/O

Regular Expressions

Classes