From e9a3fe67dd765a1d52a69fadbcdb738cbb9121dd Mon Sep 17 00:00:00 2001 From: "Matt.Mabx" Date: Mon, 21 Oct 2024 14:29:44 +0800 Subject: [PATCH] Update --- .gitignore | 2 ++ Note.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 0 3 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100644 Note.md create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..def72fe --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.md \ No newline at end of file diff --git a/Note.md b/Note.md new file mode 100644 index 0000000..83ed223 --- /dev/null +++ b/Note.md @@ -0,0 +1,67 @@ +# Python VS (Java or C++) + +## 语法简单 + +- Python: + ```python + print('Hello world') + ``` + +- C++: + ```c++ + #include + int main(){ + cout << "Hello world!" << endl; + return 0; + } + ``` + +- Java: + ```java + public class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } + } + ``` + +## 解释型语言 无需编译 + +**解释型语言**(Python)不需要在运行前编译成机器码,代码可以直接由解释器**逐行翻译并执行**。 + +- 简单说就是写好的代码可以直接运行。比如一个代码文件test.py,直接通过`python test.py`就能运行这段代码 + +**编译型语言**(C++、Java)在运行前需要将源代码编译成机器码或字节码,**生成可执行文件用来执行**。 + +- 写好的代码不能直接运行,要先变成可执行文件才能运行。比如一个文件test.java或test.cpp: + - Java:通过`javac test.java`生成一个test.class的字节码,之后使用`java test.class`来执行这个字节码,才能运行代码 + - C++:通过`g++ test.cpp`生成一个可执行文件test.exe (Windows系统)或test (Linux系统),之后执行这个文件来运行代码 + +**实际使用中的区别**: + +- Python相比Java和C++,通常**运行速度比较慢**,但是**好处是修改代码后可以立即看到变化**,不需要改一次编译一次。个别大型项目,编译一次需要几个小时。 + +- Python不需要显式的指定变量的类型: + + - Python: + + ```python + a = '1' # a为str类型 + b = '2' # b为str类型 + b = 3 # b可以被直接修改为int类型 + + print(a+b) # 报错:不同数据类型之间不能运算,说明python是强类型的 + ``` + + - C++: + ```c++ + char a = '1'; // a为str类型 + char b = '2'; // b为str类型 + b = 3; // 报错:b已经被声明为char,就不能用int对他赋值 + + int b = 3; // 这样是可以的,因为重新声明了变量类型 + print(a+b) // 报错:不同数据类型之间不能运算,说明C++也是强类型的 + ``` + + 出现这个现象的原因是Python**逐行翻译并执行**,每一行的内容会依次生效,变量的内存空间是**实时分配的**。而C++等语言需要提前编译,编译的过程中为每一个变量**预先分配了内存**,而不同类型的变量在内存中占用的大小不一致,因此不能通过赋值修改变量类型。 + diff --git a/main.py b/main.py new file mode 100644 index 0000000..e69de29