栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python入门教程(非常详细)(python自学笔记)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python入门教程(非常详细)(python自学笔记)

Giraffe Academy

python超简易入门笔记版(其三)

写在前面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 HTML
employee_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()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772781.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号