Python Tut_from freeCodeCamp.org (4)

Contents

24 Reading Files

25 Writing to Files

26 Modules and Pip

27 Classes and Objects

28 Building a Multiple Choice Quiz

29 Object Functions

30 Inheritance继承

31 Python Interpreter


24 Reading Files

# this is a txt.file
Jim - Salesman
Dwight - Salesman
Pam - Receptionist
Michael - Manager
Oscar - Accountant


'''
open("employees.txt", "r")
only read, and "a" means you can append information onto the end
of the file
'''

employee_file = open("employees.txt", "r")

print(employee_file.readable())

employee_file.close()

True

employee_file = open("employees.txt", "w")

print(employee_file.readable())

employee_file.close()

False

employee_file = open("employees.txt", "r")

print(employee_file.read())

employee_file.close()

Jim - Salesman

Dwight - Salesman

Pam - Receptionist

Michael - Manager

Oscar – Accountant

employee_file = open("employees.txt", "r")

print(employee_file.readline())

employee_file.close()

Jim – Salesman

employee_file = open("employees.txt", "r")

print(employee_file.readline())
print(employee_file.readline())

employee_file.close()

Jim - Salesman

Dwight – Salesman

print(employee_file.readlines())

['Jim - Salesman\n', 'Dwight - Salesman\n', 'Pam - Receptionist\n', 'Michael - Manager\n', 'Oscar - Accountant']

print(employee_file.readlines()[1])

Dwight – Salesman

for employee in employee_file.readlines():
    print(employee)

Jim - Salesman

Dwight - Salesman

Pam - Receptionist

Michael - Manager

Oscar – Accountant

25 Writing to Files

employee_file = open("employees.txt", "a")

employee_file.write("\nToby - Human Resources")

employee_file.close()

Jim - Salesman

Dwight - Salesman

Pam - Receptionist

Michael - Manager

Oscar - Accountant

Toby - Human Resources

employee_file = open("employees.txt", "w")

employee_file.write("\nKelly - Human Resources")

employee_file.close()

Kelly - Human Resources

employee_file = open("employees_1.txt", "w")

employee_file.write("\nKelly - Human Resources")

employee_file.close()

# creat a new file and add to list

employee_file = open("index.html", "w")

employee_file.write("<p>This is a HTML</p>")

employee_file.close()

creat a file <p>This is a HTML</p>

26 Modules and Pip

Creat a new useful_tools .py file

import random

feet_in_mile = 5280
meters_in_kilometer = 1000
beatles = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Star"]

def get_file_ext(filename):
    return filename[filename.index(".") + 1:]

def roll_dice(num):
    return random.randint(1, num)

open the main.py file

import  useful_tools

print(useful_tools.roll_dice(10))

2

Python_docx

Python 3.9.9 documentation

27 Classes and Objects

Creat a new Student.py file

class Student:
    def __init__(self, name, major, gpa, is_on_probation):
        self.name = name
        self.major = major
        self.gpa = gpa
        self.is_on_probation = is_on_probation

back to main.py

from Student import Student

Student1 = Student("JIm", "Business", 3.1, False)

print(Student1.name)

Jim

print(Student1.gpa)

3.1

from Student import Student

Student1 = Student("JIm", "Business", 3.1, False)
Student2 = Student("Pam", "Art", 2.5, True)

print(Student2.major)

Art

28 Building a Multiple Choice Quiz

Creat a new Question.py

class Question:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

back to main.py

from Question import Question

question_prompte = [
    "What color are apples?\n(a) Red/green\n(b) Purple\n(c) Orange\n\n",
    "What color are bananas?\n(a) Teal\n(b) Magenta\n(c) Yellow\n\n",
    "What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n",
]

questions = [
    Question(question_prompte[0], "a"),
    Question(question_prompte[1], "c"),
    Question(question_prompte[2], "b"),
]

def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
    print("You got " + str(score) + "/" + str(len(questions)) + " correct")

run_test(questions)

What color are apples?

(a) Red/green

(b) Purple

(c) Orange

a

What color are bananas?

(a) Teal

(b) Magenta

(c) Yellow

a

What color are strawberries?

(a) Yellow

(b) Red

(c) Blue

a

You got 1/3 correct

29 Object Functions

Creat a new Student.py

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

back to main.py

from Student import Student

Student1 = Student("JIm", "Business", 3.1)
Student2 = Student("Pam", "Art", 3.8)

print(Student2.on_honor_roll())

True

30 Inheritance继承

Creat a new 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 bbq ribs")

Creat a new ChineseChef

class ChineseChef:

    def make_chicken(self):
            print("The Cchef makes a chicken")

    def make_salad(self):
            print("The Cchef makes a salad")

    def make_special_dish(self):
            print("The Cchef makes Orange chicken")

    def make_fried_rice(self):
            print("The Cchef makes fried rice")

back to main.py

from Chef import Chef
from ChineseChef import ChineseChef

myChef = Chef()
myChef.make_special_dish()

myChineseChef = ChineseChef()
myChineseChef.make_special_dish()

The chef makes bbq ribs

The Cchef makes Orange chicken

When we try to use inheritance Fuction

We can change ChineseChef.py

from Chef import Chef

class ChineseChef(Chef):

    def make_fried_rice(self):
            print("The Cchef makes fried rice")

back to main.py again

from Chef import Chef
from ChineseChef import ChineseChef

myChef = Chef()
myChef.make_special_dish()

myChineseChef = ChineseChef()
myChineseChef.make_chicken()

the result is

The chef makes bbq ribs

The chef makes a chicken

Then how to rebuilt a new item?

Back to ChineseChef.py

Creat a new make_special_dish item

from Chef import Chef

class ChineseChef(Chef):

    def make_special_dish(self):
        print("The chef makes orange chicken")

    def make_fried_rice(self):
            print("The Cchef makes fried rice")

return main.py

from Chef import Chef
from ChineseChef import ChineseChef

myChef = Chef()
myChef.make_special_dish()

myChineseChef = ChineseChef()
myChineseChef.make_special_dish()

The chef makes bbq ribs

The chef makes orange chicken

31 Python Interpreter

You can use CMD as a Python interpreter directly

But don’t recommend

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值