为了账号安全,请及时绑定邮箱和手机立即绑定
课程 \ Python3 进阶教程(新版)

Python3 进阶教程(新版)

6-7 Python正确关闭文件
# Enter a code
with open('test.txt', 'a+') as f:
f.seek(0)
content = f.readlines()
f.seek(2)
for line in content:
f.write(line)
2022-02-20 查看完整代码
6-6 Python往文件追加内容
# Enter a code
f = open('test.txt', 'a+')
f.seek(0)
content = f.readlines()
f.seek(2)
f.writelines(content)
f.seek(0)
con= f.readlines()
print(con)
f.close
2022-02-20 查看完整代码
6-5 Python把字符串写入文件
# Enter a code
f = open('test.txt', 'w')
f.readlines()
g=reverce(f)
h = open('test1.txt', 'w')
h.writelines(g)
f.close()
2022-02-19 查看完整代码
6-4 Python读取文件内容
# Enter a code
f = open('read.py')
content = f.readlines()
print(content)
2022-02-19 查看完整代码
6-3 Python打开二进制文件
# Enter a code
f = open('C:\Users\ZL\PycharmProjects\1107\慕课网\test.jpg', 'rb')
f.close()
2022-02-19 查看完整代码
6-2 Python打开文本文件
# Enter a code
f = open('C:\Users\ZL\PycharmProjects\慕课网\test.txt', 'r')
type(f)
f.close()
2022-02-19 查看完整代码
6-1 向Python程序输入内容
# Enter a code
num = input('please input number: ')

num = int(num)
result = 1
for i in range(1, num):
result = result * i

print(result)
2022-02-19 查看完整代码
5-5 Python安装第三方模块的方法
# Enter a code
pip install requests
2022-02-19 查看完整代码
5-4 Python模块导入的路径
# Enter a code
import sys
L=sys.path
L.append('../')
print(L)
2022-02-17 查看完整代码
5-3 Python导入模块
# Enter a code
import math
math.sin(0)
math.cos(0)

from math import sin,cos
sin(0)
cos(0)
2022-02-17 查看完整代码
5-2 Python定义模块
# Enter a code
# common.py

def say_hello(name):
print('Hello {}'.format(name))
2022-02-17 查看完整代码
4-6 Python类的__call__方法
# Enter a code
class Fib(object):
def __init__(self):
self.res = []

def __call__(self, num):
a = 0
b = 1
for x in range(num):
self.res.append(a)
a, b = b, a + b
return self.res

f = Fib()
print(f(10))
2022-02-17 查看完整代码
4-5 Python类的__slots__方法
# Enter a code
class Person(object):

__slots__ = ('name', 'gender')

def __init__(self, name, gender):
self.name = name
self.gender = gender


class Student(Person):

__slots__ = ('score',)

def __init__(self, name, gender, score):
self.name = name
self.gender = gender
self.score = score


s = Student('Bob', 'male', 59)
s.name = 'Tim'
s.score = 99
print(s.score)
2022-02-17 查看完整代码
4-4 Python类的数学运算
# Enter a code
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)

class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __add__(self, r):
return Rational(self.p * r.q + self.q * r.p, self.q * r.q)
def __sub__(self, r):
return Rational(self.p * r.q - self.q * r.p, self.q * r.q)
def __mul__(self, r):
return Rational(self.p * r.p, self.q * r.q)
def __truediv__(self, r):
return Rational(self.p * r.q, self.q * r.p)
def __str__(self):
g = gcd(self.p, self.q)
return '{}/{}'.format(int(self.p/g), int(self.q/g))

r1 = Rational(1, 2)
r2 = Rational(1, 5)
print(r1 + r2)
print(r1 - r2)
print(r1 * r2)
print(r1 / r2)
2022-02-17 查看完整代码
4-3 Python类的__len__方法
# Enter a code
class Fib(object):
def __init__(self, num):
self.res = []
self.num = num
a = 0
b = 1
for x in range(num):
self.res.append(a)
a, b = b, a + b

def __str__(self):
return str(self.res)

def __len__(self):
return self.num

f = Fib(10)
print(f)
print(len(f))
2022-02-16 查看完整代码
3-3 Python判断类型
# Enter a code
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender

class Student(Person):
def __init__(self, name, gender, score):
super(Student, self).__init__(name, gender)
self.score = score

class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher, self).__init__(name, gender)
self.course = course

p = Person('Tim', 'Male')
s = Student('Bob', 'Male', 88)
t = Teacher('Alice', 'Female', 'English')
print(isinstance(t,Person))
print(isinstance(t,Student))
print(isinstance(t,Teacher))
print(isinstance(t,object))
2022-02-15 查看完整代码
3-2 Python继承类
# Enter a code
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender

class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course

teacher = Teacher('Alice', 'Female', 'English')
print(teacher.name)
print(teacher.gender)
print(teacher.course)
2022-02-15 查看完整代码
2-5 Python类属性
# Enter a code
class Animal(object):
count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Animal.count += 1

dog = Animal('wangwang', 1)
print(Animal.count)
cat = Animal('mimi', 3)
print(Animal.count)
pig = Animal('panpan', 1)
print(Animal.count)
2022-02-15 查看完整代码
2-9 Python定义类方法
# Enter a code
class Animal(object):
__localtion = 'Asia'
__count = 0
def __init__(self, name, age):
self.name = name
self.age = age
Animal.__count += 1

@classmethod
def get_count(cls):
return cls.__count

dog = Animal('wangwang', 1)
cat = Animal('mimi', 3)
pig = Animal('panpan', 1)
count = Animal.get_count()
print(count)
2022-02-15 查看完整代码
2-4 Python实例属性的初始化
# Enter a code
class Animal(object):
def __init__(self, name,age):
self.name = name
self.age = age
dog=Animal('xiaobai',1)
cat=Animal('xiaohong',2)
print(dog.name,dog.age)
print(cat.name,cat.age)
2022-02-12 查看完整代码
首页上一页12下一页尾页
意见反馈 帮助中心 APP下载
官方微信