1.语言:python
2.学习资源:【Python+爬虫】
3.爬虫日记:
(1)引号问题
print("python") 输出:python print('python') 输出:python print('python"学习"') 输出:python"学习" print("python\"学习\"") #转义 输出:python"学习" (2)换行问题
-----print函数自动换行----- ----- \n 增加换行 ----- 输出:python 学习 【错误的示例】 print("python 学习") 【正确】 print("""python 学习""") (3)字符串拼接
print("python"+"学习") 输出:python学习 (4)输出变量内容
hello="您好!" print(hello + "我是李明") 输出:您好!我是李明 在文件开头引用'库'
在调用时格式为:库名.库函数(参数)
import math math.sqrt(4) (1) #
# print(1) # print(2) # print(3) 注:多段代码被注释掉时,可以用快捷键[Ctrl+/]
(2)写入长注释时
''' 你好 你好 这是注释 ''' """ 这也是 注释 """ (1)len获取长度
a = len("\n") ---1 b = len("你好") ---2 c = len("hello") ---5 (2)字符串的某个字符
d = "abcdef" print(d[1]) ---b (1)type查看数据类型
print(type("abc")) --- print(type(True)) --- (2)转换
str(123) int("456") i = input("请输入") 注意:输入的为字符串格式
i = input("请输入") if int(i) == 1: print("接收到了1") elif 1 < int(i) <= 10: print("接收到1 [ 优先级:not > and > or ]
(1)or
if i == 1 or i == 2 or i == 3: print("接收到了{1,2,3}") (2)and
if i == 1 and j == 0: print("i=1 j=0") (3)not (只能接一个)
if not i == 1: print("没接收到1") ''' 在列表末尾添加新的对象 ''' list1.append(1) ''' 统计某个元素在列表中出现的次数 ''' list1.count(1) ''' 拼接 ''' list1 = list1 + list2 ''' 从列表中找出某个值第一个匹配项的索引位置 ''' list1.index(11) ''' 将100插入列表index=0 ''' list1.insert(0, 100) ''' 移除列表中100的第一个匹配项 ''' list1.remove(100) ''' 反向列表中元素 ''' list2.reverse() ''' 对原列表进行排序 ''' list1.sort() ''' 读取列表中第三个元素 ''' print(list1[2]) ''' 读取列表中倒数第二个元素 ''' print(list1[-2]) ''' 从第二个元素开始截取列表 ''' print(list1[1:]) dict1={"1":"yi"} dict1["2"]="er" [ 注意 ] a:b键值对中: a是键,b是值 。
键还能是元组:
dict1={"1":"yi"} dict1["2"]="er" tuple1=("张伟",18) tuple2=("张伟",19) dict1[tuple1]="137000000" dict1[tuple2]="180000000" 计算:1 + 2 + ... + 100 for i in range(1, 101): total = total + 1 等价于
for(int i=1;i<101;) total+=i; while i >= 100: print("i>=100") class Employee: def __init__(self, name, jd): self.name = name self.id = id def print_info(self): print(f"员工名字:{self.name},工号:{self.id}") class FullTimeEmployee(Employee): def __init__(self, name, id, monthly_salary): super().__init__(name, id) self.monthly_salary = monthly_salary def calculate_monthly_pay(self): return self.monthly_salary class PartTimeEmployee(Employee): def __init__(self, name, id, daily_salary, work_days): super().__init__(name, id) self.daily_salary = daily_salary self.work_days = work_days def calculate_monthly_pay(self): return self.daily_salary * self.work_days class name:
def __inti__(self,其他参数...):
构造函数操作...
class A(B): #A是继承了B的子类
def __init__(self, name, id, daily_salary, work_days):
super().__init__(name, id)其他操作...
绝对路径
/user/demo/data.txt
相对路径
../tmp
f = open("a.txt", "r", encoding="utf-8") print(f.read()) f.close() 注:当前这次读完后,系统会记录读到的位置,然后下次调用read函数将返回继续增加的内容。读完后一定要close!
或者(with as 自动关闭):
with open("a.txt") as f: print(f.read()) 读字节
print(f.read(10)) # 指定10字节 读一行 --可搭配while使用
print(f.readline()) 全部读
lines=f.readlines() for line in lines: print(line) 特点:
(1)区别于"r":只写模式下,若没找到指定文件,则创建文件。
(2)“w”模式下,write函数覆盖掉文件中所有内容!
(3)write函数不自动加换行,需手动\n
(4)不能读原本的内容(即read函数)
特点:
(1)区别于"w":附加模式下,write函数在文件内容的末尾写入
(2)write函数不自动加换行,需手动\n
(3)若没找到指定文件,则创建文件。
(4)不能读原本的内容(即read函数)
(1)可读可写
(2)write函数不自动加换行
(3)若没找到指定文件,会报错!
try: user_weight = float(input("请输入您的体重(单位:kg)")) user_height = float(input("请输入您的身高(单位: m):")) user_BMI =user_weight /user_height ** 2 except ValueError: print("输入不为合理数字,请重新运行程序,并输入正确的数字。") except ZeroDivisionError: print("身高不能为零,请重新运行程序,并输入正确的数字。") except: print("发生了未知错误,请重新运行程序。") else: print("您的BMI值为:"+str(user_BMI)) finally: print("程序结束") Python之unittest框架的介绍及使用_python unittest abc-CSDN博客