Giraffe Academy
写在前面try/except阅读文件写入文件Modules&Pip课程和对象class and object构建多项选择测验对象函数object function继承
写在前面之前只系统性学过C,之后做一些小项目需要用到python也没有学,直接上手就做了,感觉也比较简单。后面发专利,小论文,慢慢的还是想乘着有时间尝试去做一下自己想做的东西,因此重新回来系统性的学习一下python。这篇博客里没有太过于详细和基础的内容,我也比较热衷于网课,这里推荐Giraffe Academy的网课,他的风格我很喜欢,本系列博客也是根据他网课内容自己手动总结的,写成博客是希望以后如果自己如果遗忘了一些,可以通过博客迅速回忆起来,同时也是分享一下自己的学习经历,希望能够帮到一些正在学习的朋友,博客如有错误希望大家可以指正出来,谢谢!
try/except在python中捕捉错误
try:
number = int(input(“Enter a number:”))
print(number)
except:
print(“Invalid Input”)
try:
value = 10/0
number = int(input(“Enter a number:”))
print(number)
except ZeroDivisionError:
print(“Divided by zero”)
except ValueError:
print(“Invalid Input”)
从python之外的文件中读取信息
employees.txt
open(文件的路径)
在同一文件夹下可以直接输入文件的名字,例如
empolyee_file = open(“employees.txt”,“r”)
r 只读
w 只写
r+ 可读可写
a 添加
print(employee_file.readable())
print(employee_file.read())全部读取
print(employee_file.readline())只读一行(可反复执行,从前往后打印所有行)
print(employee_file.readlines())读取所有行 并存入一个数组中
print(employee_file.readlines()[1])读取第一行
for employee in employee_file.readlines():
print(employee) 读取所有行
employee_file.close() 关闭文件
employee_file = open(“employees.txt”,“a”)
employee_file.write(“Toby - Human Resourses”)这样会连接在文件的后面
employee_file_close()
employee_file = open(“employees.txt”,“a”)
employee_file.write("nToby - Human Resourses")写在文件的下一行
employee_file_close()
employee_file = open(“employees.txt”,“w”)
employee_file.write("nToby - Human Resourses")此时会用这一行字符串去覆盖所有的文件
employee_file_close()
employee_file = open(“index.html”,“w”)
employee_file.write(“
This is HTML
”)此时会创建一个新的文件夹index.html并在其中写入This is HTMLemployee_file_close() Modules&Pip
import useful_tools
print(useful_tools.roll_dice(10))
这里可以通过搜索python modules +你想要搜索的算法来找到相对应的库函数
pip本质上是个程序,是个包管理器,可以帮助管理安装或者删除Modules,例如
cmd
pip --version
pip install python-docx
import docx
pip uninstall python-docx
课程和对象class and object创建自己想要的数据类型class
class student:
def __init__(self,name,major,gpa,is_on_probation): __init__是初始化函数
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation
form student(学生文件) import student(学术班级)
student1 = student(“Jim”,“Business”,3.1,False)
student1 = student(“Pam”,“Art”,2.5,True)
print(student1.name)
class if loop
from Question import Question
question_prompts = [
“What color are apples?n(a) Red/Greenn(b) Purplen© Orangenn”,
“What color are Bananas?n(a) Tealn(b) Magentan© Yellownn”",
]
questions = [
Question(question_prompts[0],“a”)
Question(question_prompts[1],“c”)
]
class Question:
def init(self,prompt,answer)
self.prompt = prompt
self.answer = answer
def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
is answer == question.answer:
score += 1
print("You got " + str(score) + “/” + str(len(questions)) + “correct”)
run_test(questions)
对象函数object function可以用在class中的函数
class Student:
def init(self,name,major,gpa)
self.name = name
self.major = major
self.gpa = gpa
def on_honor_roll(self):
if self.gpa >= 3.5:
return True
else:
return False
form student import student
student1 = student(“Jim”,“Business”,3.1)
student1 = student(“Pam”,“Art”,2.5)
print(student2.on_honor_roll())
继承文件1 chef.py
class chef:
def make_chicken(self):
print("The chef makes a chicken")
def make_salad(self):
print("The chef makes a salad")
def make_special_dish(self):
print("The chef makes a bbq ribs")
文件2 chinesechef.py
from chef import chef
class chinesechef(chef):
def make_special_dish(self)
print("the chef make orange chicken")
def make_fried_rice(self)
print("the chef make fried rice")
主程序
from chef import chef
from chinesechef import chinesechef
mychef = chef()
mychef.make_special_dish()
mychinesechef = chinesechef()
mychinesechef.make_chicken()



