Python知识点复习

文章目录


Input & Output

在这里插入图片描述

Variables & Data types

str: a string represents a sequence of characters.
int: an integer, a whole number
float: a decimal number
bool: a boolean value is either True or False.
Date data type: including year, month, day, (not the time)
Date-time data type: including year, month, day, hour, minute, second, …

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Python字符串重复(字符串乘法)

在这里插入图片描述

字符串和数字连接在一起print时,要强制类型转换int为str

在这里插入图片描述

在这里插入图片描述

用input()得到的用户输入,是str类型,如果要以int形式计算的话,需要强制类型转换为int

在这里插入图片描述

我们可以只使用一个变量user_input来节省内存

在这里插入图片描述

convert string type to date type

strptime
在这里插入图片描述

convert date to string

strftime
在这里插入图片描述

Multi-line code statement 换行符

在这里插入图片描述

在括号内,行的延续的自动的

Line continuation is automatic when the split comes while a
statement is inside parenthesis ( , brackets [ or braces {
在这里插入图片描述

Escape sequence 转义字符

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

String format

在这里插入图片描述
在这里插入图片描述

格式 意义
<15 left alignment, using 15 spaces
^25 center alignment, using 25 spaces
>15 right alignment, using 15 spaces

string format 中限制输入占位大小的同时小数点后位数

在这里插入图片描述
还可以通过这种方式实现四舍五入取整
.0f
在这里插入图片描述

Arithmetic operators

Floor division = 地板除 = 向下取整除
在这里插入图片描述
floor division地板除是什么意思
向下取整除,就是地板除 floor division
向上取整除,就是天花板除,ceil division

来自 https://zhuanlan.zhihu.com/p/221901326
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Fundamentals of the Analysis of Algorithm Efficiency

Algorithm analysis framework 算法分析框架

Analysis of algorithms means to investigate an algorithm’s efficiency with respect to resources: running time and memory space
算法分析是指研究一个算法在资源方面的效率:运行时间和内存空间。
在这里插入图片描述

1. Measuring Input Sizes

Efficiency is defined as a function of input size.
F(n)

2. Units for Measuring Running Time

Count the number of times an algorithm’s basic operation is executed
计算一个算法的基本操作被执行的次数
Basic operation: the operation that contributes the most to the total
running time.
例如,基本操作通常是算法最内部循环中最耗时的操作。

3. Order of growth

在这里插入图片描述
在这里插入图片描述

4. Worst-Case, Best-Case, and Average-Case Efficiency

在这里插入图片描述
==Efficiency (# of times the basic operation will be executed) ==
在这里插入图片描述

Average case:
Efficiency (#of times the basic operation will be executed) for a typical/random
input of size n. NOT the average of worst and best case. How to find the
average case efficiency?
平均情况。对于大小为n的典型/随机输入的效率(基本操作将被执行的次数),而不是最坏和最好情况的平均值。如何找到平均案例的效率?

Summary

在这里插入图片描述
算法的运行时间(空间)随着其输入大小的增加而增长的阶数为无穷大。
对于相同大小的输入,一些算法的效率可能有很大的不同

渐进式符号

在这里插入图片描述

no faster

在这里插入图片描述
在这里插入图片描述

at least as fast as

在这里插入图片描述
在这里插入图片描述

at same rate

在这里插入图片描述
在这里插入图片描述

Summary

在这里插入图片描述

Some Properties

在这里插入图片描述
在这里插入图片描述
意义:算法的整体效率将由增长顺序较大的部分决定。

Using Limits for Comparing Orders of Growth

在这里插入图片描述
在这里插入图片描述

所有的对数函数loga n都属于同一个类别
所有相同度数k的多项式都属于同一类别
指数函数对于不同的a有不同的增长顺序
在这里插入图片描述
在这里插入图片描述

Analysis of non-recursive algorithms 非递归算法的分析

在这里插入图片描述

Analysis of recursive algorithms 递归算法的分析

在这里插入图片描述

  • 计算递归调用的次数
  • 解决递归问题,或通过后向替代或其他方法估计解决方案的数量级

Examples

求n!

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

重要的递归类型

在这里插入图片描述
在这里插入图片描述

For-loop

在这里插入图片描述
range(0,10) 范围是左闭右开

字符串操作

字符串内容大写/小写

在这里插入图片描述
.upper()
.lower()

找字串位置,找不到返回-1

在这里插入图片描述

字符串长度

在这里插入图片描述

根据下标返回字符串中对应字符

在这里插入图片描述

切割字符串

在这里插入图片描述
[i:j] 范围左开右闭,从下标为i的字符到下标为j-1的字符,获得的子串长度为j-i

Data Structure

data, relationship , operation
在这里插入图片描述

Abstract data type (ADT)

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Function 函数

在这里插入图片描述
在这里插入图片描述

四舍五入保留小数点后多少位函数

在这里插入图片描述

min,max函数是python内置的

在这里插入图片描述

random函数

在这里插入图片描述
左闭右闭

Class and object

对象是类的实例。
术语“对象”和“实例”可以互换使用。每个对象实例都有自己的数据值。
在这里插入图片描述

Instance attribute vs Class attribute

Some information belongs to individual object instance.
Some other information is common to all objects.

  • Instance attribute: data belongs to individual object instance.
  • Class attribute: data that is common to all objects. (Some classes do not have any class attributes.)

有些信息属于单个对象实例。
其他一些信息对于所有对象都是通用的。

  • 实例属性:数据属于单个对象实例。
  • 类属性:所有对象共有的数据。 (有些类没有任何类属性。)

在这里插入图片描述

Instance method vs Class/Static method

Instance method:

  • Deal with a particular individual object instance
  • The first argument (self) is always referred to the object instance
  • instance method can be invoked from an object

Static / Class method:

  • Do NOT deal with individual object instance
  • Common to all object instances
  • static/class method can be invoked from class name

实例方法:

  • 处理特定的单个对象实例
  • 第一个参数(self)始终引用对象实例
  • 可以从对象调用实例方法

静态/类方法:

  • 不处理单个对象实例
  • 所有对象实例共有
  • 静态/类方法可以从类名调用

Instance method

在这里插入图片描述
在这里插入图片描述

Special instance method

在这里插入图片描述
在 Python 中,特殊的实例方法(也称为魔术方法或魔法方法)在类中具有特殊的意义和用途。以下是你提到的几个魔术方法的解释和示例:

__init__ 方法

__init__ 方法是类的构造函数,用于初始化新创建的对象的属性。它在对象创建时自动调用。你可以使用它来设置对象的初始状态。

class TV_Program:
    def __init__(self, channel, title, start_time, duration):
        self.channel = channel
        self.title = title
        self.start_time = start_time
        self.duration = duration

# 创建一个 TV_Program 对象
program = TV_Program('HBO', 'Game of Thrones', '21:00', 60)
print(program.channel)  # 输出: HBO
__str__ 方法

__str__ 方法用于定义对象的“可读”字符串表示。当你使用 print() 函数或 str() 函数时,Python 会调用这个方法。这个方法应该返回一个易于阅读和理解的字符串。

class TV_Program:
    def __init__(self, channel, title, start_time, duration):
        self.channel = channel
        self.title = title
        self.start_time = start_time
        self.duration = duration

    def __str__(self):
        return f"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值