20210212【学习笔记】Python初步

概述

-1 Python计算基础及环境搭建
-2 编程基础
-3 函数基础
-4 面向对象
-5 文件操作及常用模块使用

特点

简洁性 实现同样的功能,python代码的行数往往是java的1/5。
易读性 代码像纯英语一样易于理解。
可扩展性 开源,任何人都可以做出自己的贡献。

学习方法

-1 python是一个工具,只有不断重复、练习,才能彻底掌握。课下多练习,多实践,研究开源
-2 不要复制粘贴原代码! 从对着讲义手打-理解后独立复现-学习如何找bug、改bug
-3 以理解为主,不要把时间花在优美的笔记上面。尤其是讲义上面已经有的东西。

运算符优先级和结合性一览表

在这里插入图片描述

格式

number = 0.5 #浮点型
age = 18 #整型
name = 'Molly' #字符串
beautiful = True #布尔型

list1 = [1,'2', True] #列表,各种类型数据可放一起,用[]定义
tuple1 = (1,'2', True) #类似于列表,但数据对象不可变,用()定义
dict1 = {'name':'Molly', 'age':18, 'gender':'female'} #字典,用{}定义,每一项用逗号隔开,每一项都是P:Value,P之间不可重复

set1 = {1,2,3,4,5,5,5} #集合,用{}定义,集合中相同项自动归类;相当于字典中的字不能重复

函数格式及描述

函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。
函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。
任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。
函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
函数内容以冒号起始,并且缩进。
return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。

定义函数及函数调用
def student_name(name):
    "打印学生的名字" #函数注释
    print('姓名:', name)
    return {'姓名':name} #返回值,是词典
rst = student_name('Alice') #函数调用
rst #rst接受函数的返回值

–>
姓名: Alice
{‘姓名’: ‘Alice’}

# 返回多个值
def student_name_and_age():
    "记录学生的名字和年龄"
    name = input('请输入姓名\n') #字符串
    age = int(input('请输入年龄\n')) #整型
    print(f'姓名:{name};年龄:{age}')
    return name,age
rst = student_name_and_age()

–>
请输入姓名
请输入年龄
姓名:小王;年龄:111

rst

–>
(‘小王’, 111)

type(rst)

tuple–元组

参数传递

位置参数
缺省参数
可变参数
关键字参数
命名关键字参数

循环

#控制例子,while循环

my_number = 3200  # 这是真实的价格
guess_number = input('这台冰箱多少钱?') 
guess_number = int(guess_number)
while True:
    if guess_number<my_number:   
        guess_number = input('猜低了!再猜')
        guess_number = int(guess_number)
    elif guess_number>my_number:
        guess_number = input('猜高了!再猜')
        guess_number = int(guess_number)
    else:
        break #跳出循环
    
print('\n恭喜您,猜对了!\n')
# continue : 跳过本轮 
# 打印1-10中的偶数 
for i in range(10):
    #range(10)为0-9的整型序列,本身是一个生成器
    #python本身较抽象
    num = i+1
    if num%2 != 0: #注意缩进
        # 跳过奇数
        continue #后面都不执行,如果if为真,跳出循环
    print(num)
  

字符串

name = 'molly'
a=name[1]  
b=name[-4]
c=name[1:4] #从字符串中字符1到字符3,左闭右开,不包括字符4
d=name[::-1]
print(a)
print(b)
print(c)
print(d)
e=name[1:7:2] #步长为2,1-6位
print(e)

–>
o
o
oll
yllom
ol

# 小练习
string = 'Hello world!'
string[2]
string[2:5]
string[3:]
string[8:2:-1]

–>
‘row ol’

# 如果字符串以'p'结尾,则打印
list_string = ['apple','banana_p','orange','cherry_p']
for fruit in list_string:
    if fruit[-1] == 'p':
        print(fruit)
# 如果字符串以'pr'结尾,则打印
list_string = ['apple','banana_pr','orange','cherry_pr']
for fruit in list_string:
    if fruit.endswith('pr'):
        print(fruit)
list1 = ['a','b','c','d','e','f']
list1[2]
list1[2:5]

–>
[‘c’, ‘d’, ‘e’]

添加新元素

list1 = ['a','b','c','d','e','f']
list1.append('g') # 在末尾添加元素
print(list1)
list1.insert(2, 'ooo')  # 在指定位置添加元素,如果指定的下标不存在,那么就是在末尾添加
print(list1)

–>
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’]
[‘a’, ‘b’, ‘ooo’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’]

list2 = ['z','y','x']
list1.extend(list2) #合并两个list   list2中仍有元素
print(list1)
print(list2)

–>
[‘a’, ‘b’, ‘ooo’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘z’, ‘y’, ‘x’]
[‘z’, ‘y’, ‘x’]

删除元素

list1 = ['a','b','a','d','a','f']
print(list1.pop(3)) 
print(list1)
list1.remove('a')
print(list1)

–>
d
[‘a’, ‘b’, ‘a’, ‘a’, ‘f’]
[‘b’, ‘a’, ‘a’, ‘f’]

生成器

通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。
1\列表生成式

# 第一种方法:类似列表生成式
L = [x * x for x in range(10)]
g = (x * x for x in range(10))
next(g)
g = (x * x for x in range(10))
for n in g:
    print(n)

–>
0
1
4
9
16
25
36
49
64
81

2、基于函数

# 第二种方法:基于函数

# 如何定义一个函数?
def reverse_print(string):
    print(string[::-1])
    return 'Successful!'
    ret = reverse_print('Molly')
    ret
# 在IDE里面看得更清晰
def factor(max_num):
    # 这是一个函数  用于输出所有小于max_num的质数
    factor_list = []
    n = 2
    while n<max_num:
        find = False
        for f in factor_list:
            # 先看看列表里面有没有能整除它的
            if n % f == 0:
                find = True
                break
        if not find:
            factor_list.append(n)
            yield n
            
        n+=1
        g = factor(10)
        next(g)
        g = factor(100)
for n in g:
    print(n)
练习
# 练习 斐波那契数列
def feb(max_num):
    n_1 = 1
    n_2 = 1
    n = 0
    while n<max_num:
        if n == 0 or n == 1:
            yield 1
            n += 1
        else:
            yield n_1 + n_2
            new_n_2 = n_1 
            n_1 = n_1 + n_2
            n_2 = new_n_2
            n += 1

g = feb(20)
for n in g:
    print(n)

在这里插入图片描述

闭包

python中的闭包从表现形式上定义(解释)为:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure).

如何定义类

class Athlete:
第一部分:class定义类的关键字,Athlete符合python标识符命名规则,:表示类内容的开始
def init(self,a_name,a_dob=None,a_times=[]):
第二部分:def定义函数的关键字,init 方法是一个特殊方法会在实例化对象时自动调用,我们会在这个方法中对数据进行赋值。self作为类中函数的第一个参数,方便该方法调用该类的其他属性和方法。
第三部分:自定义的属性和方法

def get_coach_data(filename):
    with open(filename) as f:
        line = f.readline()
    return line.strip().split(',')

# 从文件中读取数据
james_new = get_coach_data('mywork/james_new.txt')
james_name = james_new.pop(0)
james_dob = james_new.pop(0)
james_times = james_new

# 创建Athlete对象
james = Athlete(james_name,james_dob,james_times)

print('姓名:%s,生日:%s,最快的3次成绩:%s' %(james.name,james.dob,james.top3()))

–>
姓名:james,生日:2006-11-11,最快的3次成绩:[‘2.01’, ‘2.22’, ‘2.34’]

如何使用类

1.创建对象
对象名 = 类名(参数)
2.使用.调用类的方法和属性
对象.属性名
对象.方法名()

定义:

class 子类名(父类名):

情况1,如果子类有新增的属性,那么需要在子类__init方法中,调用父类的__init__

情况2,如果子类没有新增的属性,子类不需要写__init__方法

使用:
对象名 = 子类名(参数)

class Athlete:

    #运动员集训了,要买东西的同学要把地址改一下
    address = '中国足球协会训练基地xx街xx号'

    def __init__(self,a_name,a_dob=None,a_times=[]):
        self.name = a_name
        self.dob = a_dob
        self.times = a_times

    def top3(self):
        return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
        
    def sanitize(self,time_string):
        if '-' in time_string:
            splitter = '-'
        elif ':' in time_string:
            splitter = ':'
        else:
            return (time_string)
        (mins,secs) = time_string.split(splitter)
        return (mins+'.'+secs)
julie_new = get_coach_data('mywork/julie_new.txt')
julie_name = julie_new.pop(0)
julie_dob = julie_new.pop(0)
julie_times = julie_new

james_new = get_coach_data('mywork/james_new.txt')
james_name = james_new.pop(0)
james_dob = james_new.pop(0)
james_times = james_new

julie = Athlete(julie_name,julie_dob,julie_times)
james = Athlete(james_name,james_dob,james_times)

print(julie.address)
print(james.address)
print(Athlete.address)

–>
中国足球协会训练基地xx街xx号
中国足球协会训练基地xx街xx号
中国足球协会训练基地xx街xx号

class Athlete:
    def __init__(self,a_name,a_dob=None,a_times=[]):
        self.name = a_name
        self.dob = a_dob
        self.times = a_times

    def top3(self):
        return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
        
    def sanitize(self,time_string):
        if '-' in time_string:
            splitter = '-'
        elif ':' in time_string:
            splitter = ':'
        else:
            return (time_string)
        (mins,secs) = time_string.split(splitter)
        return (mins+'.'+secs)
james = Athlete(james_name,james_dob,james_times)
james.name = 'hehe'
# james.sanitize()
print('姓名:%s,生日:%s,最快的3次成绩:%s' %(james.name,james.dob,james.top3()))

–>
姓名:hehe,生日:2006-11-11,最快的3次成绩:[‘2.01’, ‘2.22’, ‘2.34’]

使用类的好处

降低复杂性-》更少的bug-》提高可维护行

类可以将数据与函数绑定在一起,使代码模块化

调用数据和函数,使用对象名.的方式,使代码更加优雅

# 从文件中读取数据
james_new = get_coach_data('mywork/james_new.txt')
james_name = james_new.pop(0)
james_dob = james_new.pop(0)
james_times = james_new

# 创建Athlete对象
james = Athlete(james_name,james_dob,james_times)
print('姓名:%s,生日:%s,最快的3次成绩:%s' %(james.name,james.dob,james.top3()))
继承的好处:代码重用,升级功能(重写),新增功能(新的方法)

class 子类名(父类名):

情况1,如果子类有新增的属性,那么需要在子类__init方法中,调用父类的__init__
情况2,如果子类没有新增的属性,子类不需要写__init__方法
使用:
对象名 = 子类名(参数)
继承的好处:代码重用,升级功能(重写),新增功能(新的方法)

文件处理模型

输入,处理,输出。

输入:读取4个队员的训练数据,读取4个文件

james.txt 2-34,3:21,2,34,2.45,3.01,2:01,2:01,3:10,2-22

sarah.txt 2:58,2.58,2:39,2-25,2:55,2:54,2.18,2:55,2:55

julie.txt 2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21

mikey.txt 2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38

处理:标准化数据,切分数据,top3(最快的3个时间)

输出:将每个人的信息打印在屏幕上显示

kelly教练,每次训练结束后,还要同步更新4个文件太麻烦了,把所有记录写在一个文件中吧,这个对于你来说应该不难吧?

f = open('work/train_data_cor.txt')
line = f.readline()
print(line)
line = f.readline()
print(line)
f.close()

–>
james,2004-5-21,2.34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22

julie,2006-5-9,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
open() 为bif(内置函数),参数有多个,必须的是文件路径。 返回的一个文件对象。

file.readline(),读取文件中的一行。

import sys
#读取整个文件内容
f = open('work/train_data_cor.txt')
line = f.readline()
while(line != ''):
    print(line)
    line = f.readline()
f.close()

–>
james,2004-5-21,2.34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22

julie,2006-5-9,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21

sarah,2004-3-8,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55

mikey,2003-9-10,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38

#更好的方式
f = open('work/train_data_cor.txt')
for line in f:
    print(line)
f.close()

–>
james,2004-5-21,2.34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22

julie,2006-5-9,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21

sarah,2004-3-8,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55

mikey,2003-9-10,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38

异常问题解决

1.使用异常跳过有问题的数据 2.增加代码判断

#使用异常
f = open('work/train_data_wrg.txt')
for line in f:
  
    data = line.strip().split(',')
    try:

        print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
    except:
        pass
f.close()

–>
姓名:james生日:2004-5-21时间:[‘2.34’, ‘3:21’, ‘2.34’, ‘2.45’, ‘3.01’, ‘2:01’, ‘2:01’, ‘3:10’, ‘2-22’]
姓名:julie生日:2006-5-9时间:[‘2.59’, ‘2.11’, ‘2:11’, ‘2:23’, ‘3-10’, ‘2-23’, ‘3:10’, ‘3.21’, ‘3-21’]
姓名:sarah生日:2004-3-8时间:[‘2:58’, ‘2.58’, ‘2:39’, ‘2-25’, ‘2-55’, ‘2:54’, ‘2.18’, ‘2:55’, ‘2:55’]
姓名:mikey生日:2003-9-10时间:[‘2:22’, ‘3.01’, ‘3:01’, ‘3.02’, ‘3:02’, ‘3.02’, ‘3:22’, ‘2.49’, ‘2:38’]

#代码判断
f = open('work/train_data_wrg.txt')#1
for line in f:#2
  
    data = line.strip().split(',')

    if len(data) != 1:
        print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))

f.close()#3

–>
姓名:james生日:2004-5-21时间:[‘2.34’, ‘3:21’, ‘2.34’, ‘2.45’, ‘3.01’, ‘2:01’, ‘2:01’, ‘3:10’, ‘2-22’]
姓名:julie生日:2006-5-9时间:[‘2.59’, ‘2.11’, ‘2:11’, ‘2:23’, ‘3-10’, ‘2-23’, ‘3:10’, ‘3.21’, ‘3-21’]
姓名:sarah生日:2004-3-8时间:[‘2:58’, ‘2.58’, ‘2:39’, ‘2-25’, ‘2-55’, ‘2:54’, ‘2.18’, ‘2:55’, ‘2:55’]
姓名:mikey生日:2003-9-10时间:[‘2:22’, ‘3.01’, ‘3:01’, ‘3.02’, ‘3:02’, ‘3.02’, ‘3:22’, ‘2.49’, ‘2:38’]

#clean的写法,三行变一行
with open('work/train_data_cor.txt') as f:
    for line in f:
        data = line.strip().split(',')
        print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))

–>
姓名:james生日:2004-5-21时间:[‘2.34’, ‘3:21’, ‘2.34’, ‘2.45’, ‘3.01’, ‘2:01’, ‘2:01’, ‘3:10’, ‘2-22’]
姓名:julie生日:2006-5-9时间:[‘2.59’, ‘2.11’, ‘2:11’, ‘2:23’, ‘3-10’, ‘2-23’, ‘3:10’, ‘3.21’, ‘3-21’]
姓名:sarah生日:2004-3-8时间:[‘2:58’, ‘2.58’, ‘2:39’, ‘2-25’, ‘2-55’, ‘2:54’, ‘2.18’, ‘2:55’, ‘2:55’]
姓名:mikey生日:2003-9-10时间:[‘2:22’, ‘3.01’, ‘3:01’, ‘3.02’, ‘3:02’, ‘3.02’, ‘3:22’, ‘2.49’, ‘2:38’]

写入文件内容

f = open('work/data.txt','w')
f.write('this is file content')
f.close()

–>
Help on built-in function close:

close() method of _io.TextIOWrapper instance
Flush and close the IO object.

This method has no effect if the file is already closed.

对象转JSON

import json
class Athlete(json.JSONEncoder):
    def __init__(self,a_name,a_dob=None,a_times=[]):
        self.name = a_name
        self.dob = a_dob
        self.times = a_times
    def top3(self):
        return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
    def sanitize(self,time_string):
        if '-' in time_string:
            splitter = '-'
        elif ':' in time_string:
            splitter = ':'
        else:
            return (time_string)
        (mins,secs) = time_string.split(splitter)
        return (mins+'.'+secs)


with open('work/train_data_cor.txt') as f:
    data = f.readline().strip().split(',')
    ath = Athlete(data.pop(0),data.pop(0),data)
    print(ath)

ath_json = json.dumps(ath.__dict__)

–><main.Athlete object at 0x7fdd6cc6a450>

读取json文件内容
with open('work/json.txt') as f:

    ath = json.load(f)
    print(ath)

{“name”: “james”, “dob”: “2004-5-21”, “times”: [“2.34”, “3:21”, “2.34”, “2.45”, “3.01”, “2:01”, “2:01”, “3:10”, “2-22”]}

目录访问

import os
#返回当前工作目录
current_path = os.getcwd()
print('当前路径:'+current_path)
#改变当前工作目录
os.chdir('/home/aistudio/work')
#运行mkdir命令
os.system('mkdir today')
from pathlib import Path
#返回当前绝对路径
abs_path = os.path.abspath('')
print('abs_path:'+abs_path)
#路径是否存在
Path(abs_path).exists()

–>
abs_path:/home/aistudio/work
True

print('当前路径:'+os.getcwd())
listdir = os.listdir()
#返回当前路径下文件和文件夹名
print(listdir)

–>
当前路径:/home/aistudio/work
[‘data.txt’, ‘athlete.py’, ‘train_data_wrg.txt’, ‘train_data_cor.txt’, ‘today’, ‘json.txt’]

#是否为文件夹
os.path.isdir('/home/aistudio/work/today')

–>
True

显示work路径下的所有类型为txt的文件

逐步运行程序,观察程序中变量的值

import pdb
import os



def recur(path):

    listdir = os.listdir(path)

    for name in listdir:
        if name[0] is '.' or name[0] is '_':
            continue
        next_path = path+'/'+name
        if os.path.isfile(next_path) :
            # print(next_path + '=====isfile')
            temp = name.split('.')
            (filename,filetype) = (temp.pop(0),temp.pop(0))
            if filetype == 'txt':
                target.append(name)
        else:
            recur(next_path)

    return os.path.dirname(next_path)

if __name__ == '__main__':
    pdb.set_trace()
    path = '/home/aistudio/work'
    target = []
    recur(path)
    print(target)


–>

(27)()
-> path = ‘/home/aistudio/work’
(Pdb)

#制造数据
with open('work/loren.txt','w+') as f:
    for i in range(5000000):
        f.write('loren,2011-11-3,270,3.59,4.11,3:11,3:23,4-10,3-23,4:10,4.21,4-21')
        f.write('\n')
#只使用进程的方式


print('压缩作业开始了,请您耐心等待...')

infile = 'work/loren.txt'
outfile = 'work/myarchive.zip'
f = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(infile)
f.close()


print('压缩作业结束了,请问还需要帮您做什么呢?')
import threading, zipfile

class AsyncZip(threading.Thread):
    def __init__(self, infile, outfile):
        threading.Thread.__init__(self)
        self.infile = infile
        self.outfile = outfile

    def run(self):
        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
        f.write(self.infile)
        f.close()
        print('压缩完成,您要的文件在:', self.outfile)

background = AsyncZip('work/loren.txt', 'work/myarchive.zip')

print('压缩作业开始了,请您耐心等待...')
background.start()
print('我正在为您压缩,请问还需要帮您做什么呢?')
background.join()


在这里插入图片描述

致谢

百度AI

侵删

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HelloWorld__来都来了

来都来了 福寿双全

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值