
python编程基础
主要介绍Python初级编程知识,将自己之前学过的知识逐步进行汇总
zhenyu wu
这个作者很懒,什么都没留下…
展开
-
Python编程基础:第六十节 多进程Multiprocessing
第六十节 多进程Multiprocessing前言实践前言多进程能够在不同的CPU核心上并行运行任务,可以绕过用于线程的GIL。多进程:更适合密集计算相关任务(cpu使用率高)。多线程:更好地处理与IO相关的任务。实践我们来实现一个数据求和的功能,例如从0加到100000000。首先我们使用单个进程进行计算:from multiprocessing import Process, cpu_countimport timeresult = {}def counter(num_1, num_原创 2021-11-17 15:16:27 · 892 阅读 · 0 评论 -
Python编程基础:第五十九节 守护线程Daemon Threading
第五十九节 守护线程Daemon Threading前言实践前言守护线程是在后台运行的线程,对程序的运行并不重要,你的程序在退出前不会等待守护线程的完成,此类线程的特点是,当程序中主线程及所有非守护线程执行结束时,未执行完毕的守护线程也会随之消亡(进行死亡状态),程序将结束运行。常见的守护线程常用于以下场景:后台任务,垃圾回收,等待输入,长期运行的代码段等等。实践我们这里写一个请求用户输入的代码,同时在后台运行一个计数器,每隔一秒加一,当用户输入字符串时,结束计数并退出整个程序import thr原创 2021-11-17 14:12:37 · 1227 阅读 · 0 评论 -
Python编程基础:第五十八节 线程Threading
第五十八节 线程Threading前言实践前言线程就是一个独立的代码执行流程,在一个线程内部Python会按照先后顺序执行指定的代码流。这里我们思考一下,如果我们创建多个线程,并为每个线程指定不同的任务不就可以实现并发操作了吗?这种设计方式就像工厂上班一样,我可以雇佣很多人然后把他们分为多个小组例如A、B、C,其中A组去制作产品的控制板、B组去制作产品的壳子、C组负责装配。我们同时调度三组员工并行执行任务一定比调度一组员工先做控制板,再做壳子,最后装配来得快。与实际情况不同的是,Python的GIL(g原创 2021-11-17 08:16:42 · 851 阅读 · 0 评论 -
Python编程基础:第五十七节 reduce函数Reduce
reduce() = apply a function to an iterable and reduce it to a single cumulative value. performs function on first two elements and repeats process until 1 value remains原创 2021-07-27 08:39:04 · 583 阅读 · 0 评论 -
Python编程基础:第五十六节 filter函数Filter
filter() = creates a collection of elements from an iterable for which a function returns true原创 2021-07-27 08:07:22 · 344 阅读 · 0 评论 -
Python编程基础:第五十五节 map函数Map
map() = applies a function to each item in an iterable (list, tuple, etc.)原创 2021-07-27 07:49:35 · 293 阅读 · 0 评论 -
Python编程基础:第五十四节 排序Sort
sort() method = used with lists, sort() function = used with iterables原创 2021-07-26 16:52:41 · 221 阅读 · 0 评论 -
Python编程基础:第五十三节 匿名函数Lambda Function
lambda function = function written in 1 line using lambda keyword原创 2021-07-26 16:20:47 · 486 阅读 · 0 评论 -
Python编程基础:第五十二节 高阶函数High Order Functions
Higher Order Function = a function that either: 1. accepts a function as an argument or 2. returns a function (In python, functions are also treated as objects)原创 2021-07-26 15:12:50 · 347 阅读 · 0 评论 -
Python编程基础:第五十一节 将函数赋值给变量Assign Functions to Variables
将函数赋值给变量就是为函数重命名原创 2021-07-26 10:16:44 · 1818 阅读 · 0 评论 -
Python编程基础:第五十节 海象运算符Walrus Operator
walrus operator :=. new to Python 3.8. assignment expression aka walrus operator. assigns values to variables as part of a larger expression原创 2021-07-26 09:52:53 · 1190 阅读 · 1 评论 -
Python编程基础:第四十九节 鸭子类型Duck Typing
duck typing = concept where the class of an object is less important than the methods/attributes. class type is not checked if minimum methods/attributes are present. "If it walks like a duck, and it quacks like a duck, then原创 2021-07-26 09:07:36 · 466 阅读 · 0 评论 -
Python编程基础:第四十八节 对象作为参数Objects as Argument
第四十八节 对象作为参数Objects as Arguments前言实践前言函数的参数不一定都是变量,有时候也可以指定一个对象作为参数传给函数。实践我们先定义一个Car类:class Car: color = None可见该类有一个color属性。接下来我们便定义一个函数来改变车辆的颜色:def Change_Color(car, color): car.color = color可见Change_Color函数需要传入两个参数,一个是car对象,一个是color字符串。原创 2021-07-21 14:18:59 · 3294 阅读 · 0 评论 -
Python编程基础:第四十七节 抽象类Abstract Classes
Prevents a user from creating an object of that class+ comple a user to override abstract methods in a child classabstract class = a class which contains one or more abstract methodsabstract method = a method that has a declaration but dose not原创 2021-07-20 19:31:36 · 1148 阅读 · 0 评论 -
Python编程基础:第四十六节 super函数Super Function
super() = Function used to give access to the methods of a parent class. Returns a temporary object of a parent class when used原创 2021-07-20 14:42:45 · 417 阅读 · 0 评论 -
Python编程基础:第四十五节 方法链Method Chaining
method chaining = calling multiple methods sequentially. each call performs an action on the same object and return self原创 2021-07-20 07:56:31 · 515 阅读 · 0 评论 -
Python编程基础:第四十四节 方法重写Method Overriding
第四十四节 方法重写Method Overriding前言实践前言我们前面说了,子类继承于父类,可以调用父类的所有属性和方法。那么如果我们想在继承的过程中重新书写父类的某些方法,此时就用到了方法重写。我们通常采用如下方式进行方法重写:class 父类: def 父类方法1(self): 方法1的具体执行方式class 子类(父类): def 父类方法1(self): 重写父类方法1的执行方式实践我们这里首先创建一个名为Animal的父类,并定义其方法eat():class An原创 2021-07-20 07:38:40 · 445 阅读 · 0 评论 -
Python编程基础:第四十三节 多继承Multiple Inheritance
multiple inheritance = when a child class is derived from more than one parent class原创 2021-07-19 20:00:46 · 399 阅读 · 0 评论 -
Python编程基础:第四十二节 多重继承Multi Level Inheritance
multi-level inheritance = when a derived (child) class inherits another derived (child) class原创 2021-07-19 19:42:26 · 253 阅读 · 0 评论 -
Python编程基础:第四十一节 继承Inheritance
第四十一节 继承Inheritance前言实践前言我们这一节一起来学习类的继承,这里需要首先学习两个概念:父类和子类。我们可以这么想,当定义好一个类以后,我们又有一个新的类,这个新的类需要用到之前定义好类中的属性以及方法,并在其基础上添加新的属性和方法,此时我们便可使用继承的方法,将之前的类当作父类,将新定义的类当作子类。简而言之,父类中定义公共的属性和方法,子类中针对每一个对象再定义属于其的一些属性和方法。通常而言,其代码结构为:# 父类class 父类名称: 公共属性 def 父类方法1(s原创 2021-07-19 14:29:21 · 275 阅读 · 1 评论 -
Python编程基础:第四十节 类变量Class Variables
第四十节 类变量Class Variables前言实践前言通过上一节学习我们知道Python中的类由两部分构成,属性(变量)和方法(函数)。这一节我们进一步学习属性。属性分为两部分,一部分定义在__init__函数之外,这里指定所有这个类的对象共有的属性,例如汽车都有4个轮子等等,而__init__内部定义的属性用于区别不同的对象,例如汽车的颜色,汽车的价格等等。class 对象(): 公共属性定义 def __init__(self, 属性1, 属性2, ..., 属性n): self.属性原创 2021-07-19 13:56:30 · 967 阅读 · 1 评论 -
Python编程基础:第三十九节 面向对象编程Object Oriented Programming
我们身边的所有东西都能被称为对象,例如手机,电脑,鼠标等等。每一个对象均包含其属性,也包含其方法。我们以鼠标为例,其属性包含颜色,大小,售价等等,而其方法包含左击,右击,双击,移动等等。通常,我们用变量表示属性,用函数表示方法,用类表示对象。原创 2021-07-19 09:05:27 · 219 阅读 · 1 评论 -
Python编程基础:第三十八节 问答游戏Quiz Game
我们这一节还是对之前学习内容的一个综合运用,主要涉及到函数编程、字典以及列表的使用、条件语句、循环结构等等。原创 2021-07-19 08:22:10 · 1903 阅读 · 2 评论 -
Python编程基础:第三十七节 石头剪刀布游戏Rock, Paper, Scissors Game
第三十七节 石头剪刀布游戏Rock, Paper, Scissors Game前言实践前言我们这一节的内容主要是对前边学习内容的一个综合应用,以石头,剪刀,布游戏为例讲解列表、随机数、用户输入、字符串操作、循环结构、选择分支、判断表达式等相关知识。如果你能独立完成本节编程内容,说明对前边的学习有了一个很好的掌握。实践我们的项目需求为:电脑随机选择剪刀石头布中的一个选项,用户自己输入一个作为自己的选择,然后比较电脑的选择结果与用户的选择结果判断输赢。如果用户输入错误,那就让用户一直输入,直到输入正确的原创 2021-07-16 15:14:32 · 1509 阅读 · 0 评论 -
Python编程基础:第三十六节 模块Modules
module = a file containing python code. May contain functions, classes, etc. used with modular programming, which is to separate a program into parts原创 2021-07-16 14:28:16 · 157 阅读 · 0 评论 -
Python编程基础:第三十五节 文件删除Delete a File
os.remove(path)用于删除指定路径下的文件原创 2021-07-16 09:31:40 · 557 阅读 · 4 评论 -
Python编程基础:第三十四节 文件移动Move a File
shutil.move()函数用于移动文件及文件夹原创 2021-07-16 09:00:54 · 2527 阅读 · 0 评论 -
Python编程基础:第三十三节 文件复制Copy a File
copyfile() = copies contents of a file原创 2021-07-16 08:28:53 · 1088 阅读 · 0 评论 -
Python编程基础:第三十二节 文件写入Write a File
文件写入需要用到的函数是write()原创 2021-07-16 08:07:57 · 938 阅读 · 0 评论 -
Python编程基础:第三十一节 文件读取Read a File
读取文件所用到的函数是read()原创 2021-07-15 22:00:45 · 274 阅读 · 0 评论 -
Python编程基础:第三十节 文件检测File Detection
检测指定路径下是否存在该文件原创 2021-07-15 20:55:12 · 969 阅读 · 0 评论 -
Python编程基础:第二十九节 异常Exception
exception = events detected during execution that interrupt the flow of a program原创 2021-07-15 20:24:03 · 235 阅读 · 1 评论 -
Python编程基础:第二十八节 随机数Random Numbers
Python为我们提供了random包帮助我们生成随机数。原创 2021-07-15 17:26:04 · 701 阅读 · 0 评论 -
Python编程基础:第二十七节 format输出Format
str.format() = optional method that gives users. more control when displaying output原创 2021-07-15 14:41:55 · 1121 阅读 · 0 评论 -
Python编程基础:第二十六节 kwargs参数**kwargs
**kwargs = parameter that will pack all arguments into a dictionary. useful so that a function can accept a varying amount of keyword argument原创 2021-07-15 10:19:09 · 1387 阅读 · 0 评论 -
Python编程基础:第二十五节 args参数*args
*args = parameter that will pack all arguments into a tuple. useful so that a function can accept a varying amount of arguments原创 2021-07-15 09:56:30 · 7344 阅读 · 0 评论 -
Python编程基础:第二十四节 作用域Scope
scope = The region that a variable is recognized. A variable is only avaliable from inside the region in is created. A global and locally scoped versions of a variable can be created原创 2021-07-15 09:32:59 · 255 阅读 · 0 评论 -
Python编程基础:第二十三节 嵌套函数调用Nested Functions Calls
nested functions calls = function calls inside other function calls. innermost function calls are resolved first. returned value is used as argument for the next outer function原创 2021-07-15 08:52:27 · 441 阅读 · 0 评论 -
Python编程基础:第二十二节 关键字参数Keyword Argument
keyword arguments = arguments preceded by an identifier when we pass them to a function. The order of the arguments doesn't matter, unlike positional arguments. Python knows the names of the arguments that our fu原创 2021-07-15 08:05:39 · 2183 阅读 · 0 评论 -
Python编程基础:第二十一节 函数返回Return
return statement = Functions send Python values/objects back to the caller. These values/objects are known as the function's return value原创 2021-07-14 17:37:40 · 393 阅读 · 0 评论