Python for M
Python for M前言我当时在做一个和 ML、DL、Cybersecurity 有关的项目,但自己对 Python 工程经验、ML 和 DL 都不够熟悉,直接看课程和书会比较吃力。因此这篇文章整理的是一份快速补齐 Python 基础的路线,目标不是把 Python 学到很深,而是尽快能读懂 ML/DL/RL 相关代码,并能开始做实验。
如果后续要系统做 ML 或 DL,Python 基础越扎实越好;但入门阶段不必追求一次性学完所有语法。先掌握最常用的语言结构、数据处理、文件读写、包管理和调试方法,就足够支撑后续继续学习。
1. 基础语法需要先熟悉变量、数据类型、条件分支、循环、函数和类。写代码时尽量保持命名清晰、结构简单,后续读 ML 代码会轻松很多。
123456789101112131415161718192021222324252627282930313233343536# Variables and basic data typesname = "John"age = 25is_student = Trueheight = 1.75# Control ...
Vector
Vector
Hey folks! Today we are going to dive into the world of C++ vectors. In this blog, we will cover the basics of using vectors and explore the fundamental syntax of vectors. We will also touch upon some related techniques that you wonld not want to miss.
I would like to give a special thanks to @The Cherno for teaching me C++ through his C++ series on YouTube. Please subscribe to his channel if possible!
Brief intro
Vector is a powerful datatype in the C++ standard library that functions ...
Smart pointer
Smart pointer
Hey guys! It has been several days since I last wrote a blog like this. I have been busy with mid-term exams for days. But now I’m back!
This is the first time I have written a blog entirely in English. Some statements or terminology may sound strange or be incorrect, but I am excited to start and improve my skills in English blogging. There are several reasons for this, but the main ones are as follows:
Practicing English.
Becoming familiar with terminology in the computer scienc ...
Python-字典
字典字典是Python中唯一实现映射关系的内置类型
1. 解决单表代换密码问题1.1 模拟字典方法
以下不是真正的字典,但可以实现类似的功能
通过两个列表对照实现123456c_table = ["cipher1", "cipher2"...] # 密文表d_table = ["plain1", "plain2"...] # 明文表cipher = input("input your cipher") # 输入密文split_cipher = cipher.split(" ") # 密文拆分result = [d_table[c_table.index(each)] for each in split_cipher] # 通过查找密文下标找到对应下标明文来解密print(result) # 打印结果
通过一个列表实现12345table = ["cipher1", "plain1", "cipher2", ...
Python-分支和循环
参考资料:【Python教程】《零基础入门学习Python》最新版(2022年12月26日更新)
分支结构1. if结构123if condition: statement_1(s1)statement_2(s2)
2. if-else结构12345if condition: statement_1(s1)else: statement_2(s2)statement_3(s3)
3. if-elif-elif…(-else)结构1234567if condition_1: statement_1(s1)elif condition_2: statement_2(s2)...# else:# statement_n(sn)
4. oneline结构1true_statement if condition else false_statement
循环结构1. while循环1.1 基本结构123while condition: statement_1(s1)statement_2(s2)
1.2 while-break结构123456whi ...
Python-序列
参考资料:【Python教程】《零基础入门学习Python》最新版(2022年12月26日更新)
序列Python中,列表、元组和字符串都属于序列
1. 序列的基本运算
加法 +乘法 *
123456a = [1, 2, 3]b = [4, 5, 6]c = a + bd = a * 2print(c)print(d)
12[1, 2, 3, 4, 5, 6][1, 2, 3, 1, 2, 3]
2. 序列的判定函数2.1 关键词’is’ & ‘is not’123456a = [1, 2, 3]b = [1, 2, 3]print("a == b?")print("True") if a == b else print("False")print("a is b?")print("True") if a is b else print("False")
1234a == b?Truea is b?False
第一次判定的是’a’与’b’的元素值是否 ...
Python-字符串
参考资料:【Python教程】《零基础入门学习Python》最新版(2022年12月26日更新)
字符串关于字符串,主要分为两大块知识:一块是各种字符串相关函数,另一块是format字符串和f-string
1. 字母大小写转换
函数
功能
str.capitalize()
首字母大写,其余小写
str.casefold()
字母全变为小写,可以支持多种语言
str.title()
每个单词首字母都大写,其余小写
str.swapcase()
字母大小写全部和原来相反
str.upper()
字母全变为大写,英语之外可能不支持
str.lower()
字母全变为小写
注意:这些函数都没有直接改变str指向的字符串,而是按规则生成了一个新的字符串,即str还是与原本的字符串挂钩因此要改变str时:str = str.function()
2. 对齐函数
函数
功能
str.center(width, fillchar=’ ‘)
width设置总字符数,fillchar设置填充字符,使str居中
str.ljust(wid ...
Python-列表与元组
参考资料:【Python教程】《零基础入门学习Python》最新版(2022年12月26日更新)
列表1. 创建列表12mlist = [1, 2, 3, 4, 5, 6]_ = []
2. 访问列表123print(mlist[0], end=" ")print(mlist[-1])# 1 6
123for each in mlist print(each, end=" ")# 1 2 3 4 5
3. 修改列表3.1 通过下标索引修改12mlist = [1, 1, 3, 4, 5]mlist[1] = 2结果:mlist = [1, 2, 3, 4, 5]
3.2 通过切片修改1mlist[3:] = [2, 1]结果:mlist = [1, 2, 3, 2, 1]
3.3 通过运算符修改1234_1 = [1, 2]_2 = [3, 4]_ = _1 + _2# _ = [1, 2, 3, 4]
4. 切片4.1 切片访问列表12345678910111213mlist = [1, 2, 3, 4, 5, 6]mlist[2:4]# ...




