音樂盒的網(wǎng)站怎么做代寫稿子的平臺(tái)
文章目錄
- Input & Output
- Variables & Data types
- Python字符串重復(fù)(字符串乘法)
- 字符串和數(shù)字連接在一起print時(shí),要強(qiáng)制類型轉(zhuǎn)換int為str
- 用input()得到的用戶輸入,是str類型,如果要以int形式計(jì)算的話,需要強(qiáng)制類型轉(zhuǎn)換為int
- 我們可以只使用一個(gè)變量user_input來節(jié)省內(nèi)存
- convert string type to date type
- convert date to string
- Multi-line code statement 換行符
- 在括號(hào)內(nèi),行的延續(xù)的自動(dòng)的
- Escape sequence 轉(zhuǎn)義字符
- String format
- string format 中限制輸入占位大小的同時(shí)小數(shù)點(diǎn)后位數(shù)
- Arithmetic operators
- Fundamentals of the Analysis of Algorithm Efficiency
- Algorithm analysis framework 算法分析框架
- 1. Measuring Input Sizes
- 2. Units for Measuring Running Time
- 3. Order of growth
- 4. Worst-Case, Best-Case, and Average-Case Efficiency
- Summary
- 漸進(jìn)式符號(hào)
- no faster
- at least as fast as
- at same rate
- Summary
- Some Properties
- Using Limits for Comparing Orders of Growth
- Analysis of non-recursive algorithms 非遞歸算法的分析
- Analysis of recursive algorithms 遞歸算法的分析
- Examples
- 求n!
- 重要的遞歸類型
- For-loop
- 字符串操作
- 字符串內(nèi)容大寫/小寫
- 找字串位置,找不到返回-1
- 字符串長(zhǎng)度
- 根據(jù)下標(biāo)返回字符串中對(duì)應(yīng)字符
- 切割字符串
- Data Structure
- Abstract data type (ADT)
- Function 函數(shù)
- 四舍五入保留小數(shù)點(diǎn)后多少位函數(shù)
- min,max函數(shù)是python內(nèi)置的
- random函數(shù)
- Class and object
- Instance attribute vs Class attribute
- Instance method vs Class/Static method
- Instance method
- Special instance method
- `__init__` 方法
- `__str__` 方法
- `__repr__` 方法
- 完整示例
- Static / Class method
- Class method vs static method: 類方法和靜態(tài)方法的區(qū)別
- 類方法(Class Method)
- 靜態(tài)方法(Static Method)
- 示例代碼:
- Defining class and creating object 定義類和創(chuàng)建對(duì)象
- Accessing object instance attributes 訪問對(duì)象的實(shí)例屬性
- Modify object instance attributes 更改對(duì)象的實(shí)例屬性
- Defining an object instance method 定義對(duì)象的示例屬性
- 類注釋和函數(shù)注釋
- 案例學(xué)習(xí):定義一個(gè)學(xué)生類
- Class inheritance 類繼承
- Array and Linked List
- Numpy Array
- Python鏈表實(shí)現(xiàn)
- Debuging
- Exception&traceback
- Assertion
- Logging
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字符串重復(fù)(字符串乘法)
字符串和數(shù)字連接在一起print時(shí),要強(qiáng)制類型轉(zhuǎn)換int為str
用input()得到的用戶輸入,是str類型,如果要以int形式計(jì)算的話,需要強(qiáng)制類型轉(zhuǎn)換為int
我們可以只使用一個(gè)變量user_input來節(jié)省內(nèi)存
convert string type to date type
strptime
convert date to string
strftime
Multi-line code statement 換行符
在括號(hào)內(nèi),行的延續(xù)的自動(dòng)的
Line continuation is automatic when the split comes while a
statement is inside parenthesis ( , brackets [ or braces {
Escape sequence 轉(zhuǎn)義字符
String format
格式 | 意義 |
---|---|
<15 | left alignment, using 15 spaces |
^25 | center alignment, using 25 spaces |
>15 | right alignment, using 15 spaces |
string format 中限制輸入占位大小的同時(shí)小數(shù)點(diǎn)后位數(shù)
還可以通過這種方式實(shí)現(xiàn)四舍五入取整
.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
算法分析是指研究一個(gè)算法在資源方面的效率:運(yùn)行時(shí)間和內(nèi)存空間。
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
計(jì)算一個(gè)算法的基本操作被執(zhí)行的次數(shù)
Basic operation: the operation that contributes the most to the total
running time.
例如,基本操作通常是算法最內(nèi)部循環(huán)中最耗時(shí)的操作。
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?
平均情況。對(duì)于大小為n的典型/隨機(jī)輸入的效率(基本操作將被執(zhí)行的次數(shù)),而不是最壞和最好情況的平均值。如何找到平均案例的效率?
Summary
算法的運(yùn)行時(shí)間(空間)隨著其輸入大小的增加而增長(zhǎng)的階數(shù)為無窮大。
對(duì)于相同大小的輸入,一些算法的效率可能有很大的不同
漸進(jìn)式符號(hào)
no faster
at least as fast as
at same rate
Summary
Some Properties
意義:算法的整體效率將由增長(zhǎng)順序較大的部分決定。
Using Limits for Comparing Orders of Growth
所有的對(duì)數(shù)函數(shù)loga n都屬于同一個(gè)類別
所有相同度數(shù)k的多項(xiàng)式都屬于同一類別
指數(shù)函數(shù)對(duì)于不同的a有不同的增長(zhǎng)順序
Analysis of non-recursive algorithms 非遞歸算法的分析
Analysis of recursive algorithms 遞歸算法的分析
- 計(jì)算遞歸調(diào)用的次數(shù)
- 解決遞歸問題,或通過后向替代或其他方法估計(jì)解決方案的數(shù)量級(jí)
Examples
求n!
重要的遞歸類型
For-loop
range(0,10) 范圍是左閉右開
字符串操作
字符串內(nèi)容大寫/小寫
.upper()
.lower()
找字串位置,找不到返回-1
字符串長(zhǎng)度
根據(jù)下標(biāo)返回字符串中對(duì)應(yīng)字符
切割字符串
[i:j] 范圍左開右閉,從下標(biāo)為i的字符到下標(biāo)為j-1的字符,獲得的子串長(zhǎng)度為j-i
Data Structure
data, relationship , operation
Abstract data type (ADT)
Function 函數(shù)
四舍五入保留小數(shù)點(diǎn)后多少位函數(shù)
min,max函數(shù)是python內(nèi)置的
random函數(shù)
左閉右閉
Class and object
對(duì)象是類的實(shí)例。
術(shù)語“對(duì)象”和“實(shí)例”可以互換使用。每個(gè)對(duì)象實(shí)例都有自己的數(shù)據(jù)值。
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.)
有些信息屬于單個(gè)對(duì)象實(shí)例。
其他一些信息對(duì)于所有對(duì)象都是通用的。
- 實(shí)例屬性:數(shù)據(jù)屬于單個(gè)對(duì)象實(shí)例。
- 類屬性:所有對(duì)象共有的數(shù)據(jù)。 (有些類沒有任何類屬性。)
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
實(shí)例方法:
- 處理特定的單個(gè)對(duì)象實(shí)例
- 第一個(gè)參數(shù)(self)始終引用對(duì)象實(shí)例
- 可以從對(duì)象調(diào)用實(shí)例方法
靜態(tài)/類方法:
- 不處理單個(gè)對(duì)象實(shí)例
- 所有對(duì)象實(shí)例共有
- 靜態(tài)/類方法可以從類名調(diào)用
Instance method
Special instance method
在 Python 中,特殊的實(shí)例方法(也稱為魔術(shù)方法或魔法方法)在類中具有特殊的意義和用途。以下是你提到的幾個(gè)魔術(shù)方法的解釋和示例:
__init__
方法
__init__
方法是類的構(gòu)造函數(shù),用于初始化新創(chuàng)建的對(duì)象的屬性。它在對(duì)象創(chuàng)建時(shí)自動(dòng)調(diào)用。你可以使用它來設(shè)置對(duì)象的初始狀態(tài)。
class TV_Program:def __init__(self, channel, title, start_time, duration):self.channel = channelself.title = titleself.start_time = start_timeself.duration = duration# 創(chuàng)建一個(gè) TV_Program 對(duì)象
program = TV_Program('HBO', 'Game of Thrones', '21:00', 60)
print(program.channel) # 輸出: HBO
__str__
方法
__str__
方法用于定義對(duì)象的“可讀”字符串表示。當(dāng)你使用 print()
函數(shù)或 str()
函數(shù)時(shí),Python 會(huì)調(diào)用這個(gè)方法。這個(gè)方法應(yīng)該返回一個(gè)易于閱讀和理解的字符串。
class TV_Program:def __init__(self, channel, title, start_time, duration):self.channel = channelself.title = titleself.start_time = start_timeself.duration = durationdef __str__(self):return f"{self.title} on {self.channel} at {self.start_time} for {self.duration} minutes"# 創(chuàng)建一個(gè) TV_Program 對(duì)象
program = TV_Program('HBO', 'Game of Thrones', '21:00', 60)
print(program) # 輸出: Game of Thrones on HBO at 21:00 for 60 minutes
__repr__
方法
__repr__
方法用于定義對(duì)象的“正式”字符串表示。這通常是一個(gè)詳細(xì)且準(zhǔn)確的字符串,應(yīng)該盡可能包含信息,以便開發(fā)者可以重現(xiàn)該對(duì)象。__repr__
方法通常在交互式解釋器中調(diào)用,或者當(dāng)使用 repr()
函數(shù)時(shí)調(diào)用。
class TV_Program:def __init__(self, channel, title, start_time, duration):self.channel = channelself.title = titleself.start_time = start_timeself.duration = durationdef __repr__(self):return f"TV_Program(channel='{self.channel}', title='{self.title}', start_time='{self.start_time}', duration={self.duration})"# 創(chuàng)建一個(gè) TV_Program 對(duì)象
program = TV_Program('HBO', 'Game of Thrones', '21:00', 60)
print(repr(program)) # 輸出: TV_Program(channel='HBO', title='Game of Thrones', start_time='21:00', duration=60)
完整示例
class TV_Program:def __init__(self, channel, title, start_time, duration):self.channel = channelself.title = titleself.start_time = start_timeself.duration = durationdef __str__(self):return f"{self.title} on {self.channel} at {self.start_time} for {self.duration} minutes"def __repr__(self):return f"TV_Program(channel='{self.channel}', title='{self.title}', start_time='{self.start_time}', duration={self.duration})"# 創(chuàng)建一個(gè) TV_Program 對(duì)象
program = TV_Program('HBO', 'Game of Thrones', '21:00', 60)
print(program) # 調(diào)用 __str__ 方法
print(repr(program)) # 調(diào)用 __repr__ 方法
在這個(gè)示例中,__init__
方法用于初始化 TV_Program
對(duì)象的屬性,__str__
方法用于提供對(duì)象的可讀字符串表示,而 __repr__
方法用于提供對(duì)象的正式字符串表示。
Static / Class method
Class method vs static method: 類方法和靜態(tài)方法的區(qū)別
- The first argument (cls) of a class method is always referred to the class
在 Python 中,類方法和靜態(tài)方法都是屬于類的特殊方法,但它們有不同的用途和行為。以下是它們的區(qū)別和詳細(xì)解釋:
類方法(Class Method)
類方法使用 @classmethod
裝飾器定義,第一個(gè)參數(shù)總是指向類本身,通常命名為 cls
。類方法可以訪問類的屬性和方法,但不能直接訪問實(shí)例屬性。
特點(diǎn):
- 第一個(gè)參數(shù)
cls
代表類,而不是實(shí)例。 - 可以通過類名或者實(shí)例調(diào)用。
- 可以訪問類屬性和類方法,不能訪問實(shí)例屬性。
示例:
class Student:email_domain = "solla.sollew.edu" # 類屬性@classmethoddef admin_email(cls):return "admin@" + cls.email_domain # 訪問類屬性# 調(diào)用類方法
print(Student.admin_email()) # 輸出: admin@solla.sollew.edu# 創(chuàng)建實(shí)例并調(diào)用類方法
student = Student()
print(student.admin_email()) # 輸出: admin@solla.sollew.edu
靜態(tài)方法(Static Method)
靜態(tài)方法使用 @staticmethod
裝飾器定義。靜態(tài)方法沒有默認(rèn)的參數(shù),它們與類和實(shí)例都無關(guān),不能訪問類屬性或?qū)嵗龑傩?。靜態(tài)方法通常用于一些邏輯上與類相關(guān),但不需要訪問類或?qū)嵗娜魏螖?shù)據(jù)的方法。
特點(diǎn):
- 沒有默認(rèn)的參數(shù)(沒有
self
或cls
)。 - 可以通過類名或者實(shí)例調(diào)用。
- 無法訪問類屬性和實(shí)例屬性。
示例:
class Student:email_domain = "solla.sollew.edu" # 類屬性@staticmethoddef uni_website():return "http://www.solla.sollew.edu" # 邏輯上與類相關(guān),但不訪問類或?qū)嵗臄?shù)據(jù)# 調(diào)用靜態(tài)方法
print(Student.uni_website()) # 輸出: http://www.solla.sollew.edu# 創(chuàng)建實(shí)例并調(diào)用靜態(tài)方法
student = Student()
print(student.uni_website()) # 輸出: http://www.solla.sollew.edu
示例代碼:
總結(jié):
- 類方法 使用
@classmethod
裝飾,第一個(gè)參數(shù)cls
代表類本身,可以通過類名或?qū)嵗{(diào)用,能訪問類屬性和類方法。 - 靜態(tài)方法 使用
@staticmethod
裝飾,沒有默認(rèn)參數(shù),不能訪問類屬性或?qū)嵗龑傩?#xff0c;只是邏輯上與類相關(guān)的方法。
class Student:email_domain = "solla.sollew.edu" # 類屬性student_dir = "/user/student" # 類屬性@classmethoddef admin_email(cls):return "admin@" + cls.email_domain # 類方法,訪問類屬性@staticmethoddef uni_website():return "http://www.solla.sollew.edu" # 靜態(tài)方法,不訪問類或?qū)嵗龜?shù)據(jù)# 調(diào)用類方法和靜態(tài)方法
print(Student.admin_email()) # 輸出: admin@solla.sollew.edu
print(Student.uni_website()) # 輸出: http://www.solla.sollew.edu# 創(chuàng)建實(shí)例并調(diào)用類方法和靜態(tài)方法
student = Student()
print(student.admin_email()) # 輸出: admin@solla.sollew.edu
print(student.uni_website()) # 輸出: http://www.solla.sollew.edu
Defining class and creating object 定義類和創(chuàng)建對(duì)象
Accessing object instance attributes 訪問對(duì)象的實(shí)例屬性
Modify object instance attributes 更改對(duì)象的實(shí)例屬性
Defining an object instance method 定義對(duì)象的示例屬性
類注釋和函數(shù)注釋
案例學(xué)習(xí):定義一個(gè)學(xué)生類
Class inheritance 類繼承
Array and Linked List
Numpy Array
Python鏈表實(shí)現(xiàn)
Debuging
Exception&traceback
traceback
庫(kù)中的traceback.format_exc()
獲取異常追蹤的詳細(xì)信息
Assertion
? 它通常用于檢查不可能發(fā)生的情況
? 斷言語句導(dǎo)致終止
立即停止 → 快速失敗
? 對(duì)于來自程序員而不是用戶的錯(cuò)誤
Logging
- 使用 print() 向用戶顯示某些變量的值顯示消息
- Logging日志模塊在屏幕上顯示日志信息/向程序員顯示信息文件