Python 简明教程
Python vs C++
Python 是一种通用的高级编程语言。Python 用于网站开发、机器学习和其他前沿软件开发。Python 适用于初学者和经验丰富的 C++ 和 Java 程序员。 Guido Van Rossam 在 1989 年于荷兰国家研究中心创建了 Python。Python 在 1991 年发布。
Python is a general-purpose, high-level programming language. Python is used for web development, Machine Learning, and other cutting-edge software development. Python is suitable for both new and seasoned C++ and Java programmers. Guido Van Rossam has created Python in 1989 at Netherlands' National Research Institute. Python was released in 1991.
C 是一种中级、区分大小写、面向对象编程语言。Bjarne Stroustrup 在贝尔实验室创建了 C。C 是一种独立于平台的编程语言,可用于 Windows、Mac OS 和 Linux。C 接近硬件,允许低级编程。这为开发者提供了对内存的控制、提高的性能和可靠的软件。
C is a middle-level, case-sensitive, object-oriented programming language. Bjarne Stroustrup created C at Bell Labs. C is a platform-independent programming language that works on Windows, Mac OS, and Linux. C is near to hardware, allowing low-level programming. This provides a developer control over memory, improved performance, and dependable software.
阅读本文档以获取 C++ 和 Python 的概述,以及了解这两种编程语言之间的不同。
Read through this article to get an overview of C++ and Python and how these two programming languages are different from each other.
What is Python?
Python 目前是最广泛使用的编程语言之一。它是一种解释性的高级编程语言。与其他语言相比,Python 的学习曲线低得多,并且易于使用。
Python is currently one of the most widely used programming languages. It is an interpreted programming language that operates at a high level. When compared to other languages, the learning curve for Python is much lower, and it is also quite straightforward to use.
Python 是工作在 Artificial Intelligence 、 Machine Learning (ML) 、 Data Science 、 Internet of Things (IoT) 等领域专业人员的首选语言,因为它不仅非常适合脚本应用程序,而且也非常适合独立程序。
Python is the programming language of choice for professionals working in fields such as Artificial Intelligence, Machine Learning (ML), Data Science, the Internet of Things (IoT), etc., because it excels at both scripting applications and as standalone programmes.
此外,Python 是首选语言的原因还包括,它易于学习。得益于其出色的语法和可读性,维护成本降低了。程序的模块化和代码的可重复使用性,都有助于支持各种包和模块。
In addition to this, Python is the language of choice because it is easy to learn. Because of its excellent syntax and readability, the amount of money spent on maintenance is decreased. The modularity of the programme and the reusability of the code both contribute to its support for a variety of packages and modules.
使用 Python,我们可以执行 −
Using Python, we can perform −
-
Web development
-
Data analysis and machine learning
-
Automation and scripting
-
Software testing and many more
Features
以下列出 Python 的一些重要功能 –
Here is a list of some of the important features of Python −
-
Easy to learn − Python has a simple structure, few keywords, and a clear syntax. This makes it easy for the student to learn quickly. Code written in Python is easier to read and understand.
-
Easy to maintain − The source code for Python is pretty easy to keep up with.
-
A large standard library − Most of Python’s library is easy to move around and works on UNIX, Windows, Mac.
-
Portable − Python can run on a wide range of hardware platforms, and all of them have the same interface.
Python Example
看看以下简单的 Python 程序 −
Take a look at the following simple Python program −
a = int(input("Enter value for a"))
b = int(input("Enter value for b"))
print("The number you have entered for a is ", a)
print("The number you have entered for b is ", b)
在我们的示例中,我们采用了两个变量“a”和“b”,并为这些变量分配了一些值。请注意,在 Python 中,我们无需显式声明变量的数据类型,因为 PVM 将根据用户的输入分配数据类型。
In our example, we have taken two variables "a" and "b" and assigning some value to those variables. Note that in Python, we don’t need to declare datatype for variables explicitly, as the PVM will assign datatype as per the user’s input.
-
The input() function is used to take input from the user through keyboard.
-
In Python, the return type of input() is string only, so we have to convert it explicitly to the type of data which we require. In our example, we have converted to int type explicitly through int( ) function.
-
print() is used to display the output.
执行此 Python 代码后,将产生以下输出 −
On execution, this Python code will produce the following output −
Enter value for a 10
Enter value for b 20
The number you have entered for a is 10
The number you have entered for b is 20
What is C++?
C 是一种静态类型、编译、多范例、通用编程语言,其学习曲线陡峭。它被广泛用于视频游戏、桌面应用程序和嵌入式系统。C 与 C 非常兼容,因此几乎可以构建所有 C 源代码,而无需进行任何更改。面向对象编程使得 C++ 成为比 C 更结构化、更安全的语言。
C is a statically typed, compiled, multi-paradigm, general-purpose programming language with a steep learning curve. Video games, desktop apps, and embedded systems use it extensively. C is so compatible with C that it can build practically all C source code without any changes. Object-oriented programming makes C++ a better-structured and safer language than C.
Features
让我们看看 C++ 的一些特性及其受欢迎的原因。
Let’s see some features of C++ and the reason of its popularity.
-
Middle-level language − It’s a middle-level language since it can be used for both systems development and large-scale consumer applications like Media Players, Photoshop, Game Engines, etc.
-
Execution Speed − C++ code runs quickly. Because it’s compiled and uses procedures extensively. Garbage collection, dynamic typing, and other modern features impede program execution.
-
Object-oriented language − Object-oriented programming is flexible and manageable. Large apps are possible. Growing code makes procedural code harder to handle. C++'s key advantage over C.
-
Extensive Library Support − C++ has a vast library. Third-party libraries are supported for fast development.
C++ Example
让我们通过下面编写的示例了解 C++ 的语法。
Let’s understand the syntax of C++ through an example written below.
#include
using namespace std;
int main() {
int a, b;
cout << "Enter The value for variable a \n";
cin >> a;
cout << "Enter The value for variable b";
cin >> b;
cout << "The value of a is "<< a << "and" << b;
return 0;
}
在我们的示例中,我们通过键盘从用户那里获取两个变量“a”和“b”的输入,并在控制台上显示数据。
In our example, we are taking input for two variables "a" and "b" from the user through the keyboard and displaying the data on the console.
执行后,它将生成以下 output −
On execution, it will produce the following output −
Enter The value for variable a
10
Enter The value for variable b
20
The value of a is 10 and 20
Comparison Between Python and C++ across Various Aspects
Python 和 C++ 都是最流行的编程语言。它们都各自具有优缺点。在本教程中,我们将仔细研究其区别彼此的特征。
Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this tutorial, we shall take a closure look at their characteristic features which differentiate one from another.
Compiled vs Interpreted
与 C 一样,C++ 也是基于编译器的语言。编译器将整个代码转换成特定于正在使用的操作系统和处理器架构的机器语言代码。
Like C, C++ is also a compiler-based language. A compiler translates the entire code in a machine language code specific to the operating system in use and processor architecture.
Python 是基于解释器的语言。解释器逐行执行源代码。
Python is interpreter-based language. The interpreter executes the source code line by line.
Cross platform
当在 Linux 上编译 C++ 源代码(例如 hello.cpp)时,它只能在任何其他带有 Linux 操作系统的计算机上运行。如果需要在其他操作系统上运行,则需要进行编译。
When a C++ source code such as hello.cpp is compiled on Linux, it can be only run on any other computer with Linux operating system. If required to run on other OS, it needs to be compiled.
Python 解释器不生成编译后的代码。源代码在每次运行在任何操作系统时都会被转换为字节码,而不会有任何更改或额外的步骤。
Python interpreter doesn’t produce compiled code. Source code is converted to byte code every time it is run on any operating system without any changes or additional steps.
Portability
Python 代码可以轻松地从一个操作系统移植到另一个操作系统。C++ 代码不可移植,因为它在操作系统更改时必须重新编译。
Python code is easily portable from one OS to other. C++ code is not portable as it must be recompiled if the OS changes.
Speed of Development
C++ 程序被编译为机器代码。因此,其执行速度比基于解释器的语言更快。
C++ program is compiled to the machine code. Hence, its execution is faster than interpreter based language.
Python 解释器不会生成机器代码。每次执行程序时都会将中间字节码转换为机器语言。
Python interpreter doesn’t generate the machine code. Conversion of intermediate byte code to machine language is done on each execution of program.
如果要频繁使用程序,则 C++ 比 Python 更有效率。
If a program is to be used frequently, C++ is more efficient than Python.
Easy to Learn
与 C 相比,Python 具有更简单的语法。其代码更具可读性。编写 C 代码一开始似乎令人望而生畏,因为语法规则复杂,例如使用花括号和分号来结束句子。
Compared to C, Python has a simpler syntax. Its code is more readable. Writing C code seems daunting in the beginning because of complicated syntax rule such as use of curly braces and semicolon for sentence termination.
Python 不使用花括号来标记一个语句块。相反,它使用缩进。相同缩进级别的语句标记一个块。这使得 Python 程序更具可读性。
Python doesn’t use curly brackets for marking a block of statements. Instead, it uses indents. Statements of similar indent level mark a block. This makes a Python program more readable.
Static vs Dynamic Typing
C++ 是一种静态类型语言。存储数据的变量的类型需要一开始就声明。未声明的变量无法使用。一旦变量被声明为特定类型,则只能存储该类型的变量。
C++ is a statically typed language. The type of variables for storing data need to be declared in the beginning. Undeclared variables can’t be used. Once a variable is declared to be of a certain type, value of only that type can be stored in it.
Python 是一种动态类型语言。它不要求在给变量赋值之前对其进行声明。由于变量可以存储任何类型的数据,因此称为动态类型。
Python is a dynamically typed language. It doesn’t require a variable to be declared before assigning it a value. Since, a variable may store any type of data, it is called dynamically typed.
OOP Concepts
C 和 Python 都实现了面向对象编程的概念。C 比 Python 更接近 OOP 理论。C++ 支持数据封装的概念,因为变量的可见性可以定义为 public、private 和 protected。
Both C and Python implement object oriented programming concepts. C is closer to the theory of OOP than Python. C++ supports the concept of data encapsulation as the visibility of the variables can be defined as public, private and protected.
Python 没有定义可见性的规定。与 C++ 不同,Python 不支持方法重载。因为它是一种动态类型,所以所有的方法本质上都是多态的。
Python doesn’t have the provision of defining the visibility. Unlike C++, Python doesn’t support method overloading. Because it is dynamically typed, all the methods are polymorphic in nature by default.
C 实际上是 C 的一个扩展。可以说在 C 中添加了额外的关键字,以便它支持 OOP。因此,我们可以在 C 中编写一个 C 类型面向过程的程序。
C is in fact an extension of C. One can say that additional keywords are added in C so that it supports OOP. Hence, we can write a C type procedure oriented program in C.
Python 是完全的面向对象语言。Python 的数据模型是这样的,即使你可以采用面向过程的方法,但 Python 的内部使用的是面向对象的方法。
Python is completely object oriented language. Python’s data model is such that, even if you can adapt a procedure oriented approach, Python internally uses object-oriented methodology.
Garbage Collection
C 使用指针的概念。C 程序中未使用的内存不会自动清除。在 C 中,垃圾回收过程是手动进行的。因此,C 程序可能面临与内存相关的异常行为。
C uses the concept of pointers. Unused memory in a C program is not cleared automatically. In C, the process of garbage collection is manual. Hence, a C program is likely to face memory related exceptional behavior.
Python 具有自动垃圾回收机制。因此,Python 程序更健壮,不太容易出现与内存相关的问题。
Python has a mechanism of automatic garbage collection. Hence, Python program is more robust and less prone to memory related issues.
Application Areas
因为 C++ 程序直接编译为机器代码,所以它更适合于系统编程、编写设备驱动程序、嵌入式系统和操作系统实用程序。
Because C++ program compiles directly to machine code, it is more suitable for systems programming, writing device drivers, embedded systems and operating system utilities.
Python 程序适合于应用程序编程。它今天的主要应用领域是数据科学、机器学习、API 开发等。
Python program is suitable for application programming. Its main area of application today is data science, machine learning, API development etc.
Difference Between Python and C++
下表总结了 Python 和 C++ 之间的区别:
The following table summarizes the differences between Python and C++ −
Criteria |
Python |
C++ |
Execution |
Python is an interpreted-based programming language. Python programs are interpreted by an interpreter. |
C is a compiler-based programming language. C programs are compiled by a compiler. |
Typing |
Python is a dynamic-typed language. |
C++ is a static-typed language. |
Portability |
Python is a highly portable language, code written and executed on a system can be easily run on another system. |
C++ is not a portable language, code written and executed on a system cannot be run on another system without making changes. |
Garbage collection |
Python provides a garbage collection feature. You do not need to worry about the memory management. It is automatic in Python. |
C does not provide garbage collection. You have to take care of freeing memories. It is manual in C. |
Syntax |
Python’s syntaxes are very easy to read, write, and understand. |
C++'s syntaxes are tedious. |
Performance |
Python’s execution performance is slower than C++'s. |
C++ codes are faster than Python codes. |
Application areas |
Python’s application areas are machine learning, web applications, and more. |
C++'s application areas are embedded systems, device drivers, and more. |