📜  Python与 C 和 C++ 的比较

📅  最后修改于: 2021-10-19 05:01:59             🧑  作者: Mango

在下面的文章中,我们将从初学者的角度比较 3 种最常用的编码语言。它将帮助您一起学习所有 3 种语言的基础知识,同时节省您的时间,还将帮助您从一种您知道的语言转换到另一种您不知道的语言。让我们讨论所有 3 种语言的简要历史,然后我们将继续进行实际学习。

C VS C++ VS Python

C C++ Python
C was developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Labs. C++ was developed by Bjarne Stroustrup in 1979. Python was created by Guido van Rossum, and released in 1991.
More difficult to write code in contrast to both Python and C++ due to complex syntax. C++ code is less complex than C but more complex in contrast to python. Easier to write code.
Longer lines of code as compared to python. Longer lines of code as compared to python. 3-5 times shorter than equivalent C/C++ programs.
Variables are declared in C. Variables are declared in C++ Python has no declaration.
C is a compiled language. C++ is a compiled language. Python is an interpreted language.
C contains 32 keywords. C++ contains 52 keywords. Python contains 33 keywords.
For the development of code, C supports procedural programming. C++ is known as hybrid language because C++ supports both procedural and object oriented programming paradigms. Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
C does not support inheritance. C++ support both single and multiple inheritance Python supports all 5 types of inheritance i.e. single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance
C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation. C++ provides new operator for memory allocation and delete operator for memory de-allocation. Python’s memory allocation and deallocation method is automatic.
Direct support for exception handling is not supported by C. Exception handling is supported by C++. Exception handling is supported by Python.

库和头文件包含

头文件:告诉编译器如何调用某些功能(不知道该功能实际如何工作)的文件称为头文件。它们包含函数原型。它们还包含与库一起使用的数据类型和常量。我们使用#include在程序中使用这些头文件。这些文件以 .h 扩展名结尾。
库:库是实现实际功能的地方,即它们包含函数体。
模块:模块是包含Python定义和语句的文件。模块可以定义函数、类和变量。模块还可以包含可运行的代码。将相关代码分组到一个模块中,使代码更易于理解和使用。 Python的导入类似于C/C++中的#include header_file。 Python模块可以通过使用import 导入文件/函数来访问来自另一个模块的代码。

C
// C program to demonstrate
// adding header file
 
#include 
#include 


C++
// C++ program to demonstrate
// adding header file
 
#include 
using namespace std;
#include 


Python
# Python program to demonstrate
# including modules
 
import tensorflow
 
 
# Including a class
# from existing module
from tensorflow import keras


C
# C program to demonstrate
# declaring main
 
#include 
int main()
{
 
// Your code here
 
return 0;
}


C++
# C++ program to demonstrate
# declaring main
 
#include 
int main()
{
 
// Your code here
 
return 0
}


Python3
# Python program to demonstrate
# declaring main
 
def main():
    # write your code here
 
if __name__=="__main__":
    main()


C
// C program to demonstrate
// declaring variable
 
#include 
 
int main()
{
    // Declaring one variable at a time
    int a;
 
    // Declaring more than one variable
    char a, b, c, d;
 
    // Initializing variables
    float a = 0, b, c;
    b = 1;
 
    return 0;
}


C++
// C++ program to demonstrate
// declaring variables
 
#include 
 
int main()
{
    // Declaring one variable at a time
    int a;
 
    // Declaring more than one variable
    char a, b, c, d;
 
    // Initializing variables
    float a = 0, b, c;
    b = 1;
  
    return 0;
}


Python
# Python program to demonstrate
# creating variables
  
# An integer assignment
age = 45                     
   
# A floating point
salary = 1456.8            
   
# A string  
name = "John"


C
// C program to showing
// how to print data
// on screen
 
#include 
 
int main()
{
   printf("Hello World");
   return 0;
}


C++
// C++ program to showing
// how to print data
// on screen
 
#include 
using namespace std;
 
int main()
{
    cout << "Hello World";
    return 0;
}


Python
# Python program to showing
# how to print data
# on screen
 
print("Hello World")


C
// C program showing
// how to take input
// from user
 
#include 
 
int main()
{
    int a, b, c;
    printf("first number: ");
    scanf("%d", &a);
 
    printf("second number: ");
    scanf("%d", &b);
 
    c = a + b;
 
    printf("Hello World\n%d + %d = %d", a, b, c);
 
    return 0;
}


C++
// C++ program showing
// how to take input
// from user
 
#include 
using namespace std;
 
int main()
{
    int a, b, c;
    cout << "first number: ";
    cin >> a;
 
    cout << endl;
    cout << "second number: ";
    cin >> b;
 
    cout << endl;
    c = a + b;
 
    cout << "Hello World" << endl;
    cout << a << "+" << b << "=" << c;
 
    return 0;
}


Python
# Python program showing
# how to take input
# from user
 
a = input("first number: ")
b = input("second number: ")
 
c = a + b
 
print("Hello World")
print(a, "+", b, "=", c)


主方法声明

Main 方法声明是向计算机声明应该从这里完成我的代码的实现。在 C 和 C++ 中声明 main 的过程是相同的。当我们声明 int main 其中 int 代表返回类型时,我们应该必须在代码末尾返回一些整数,以便编译时不会出错。我们可以在花括号之间编写代码。

C

# C program to demonstrate
# declaring main
 
#include 
int main()
{
 
// Your code here
 
return 0;
}

C++

# C++ program to demonstrate
# declaring main
 
#include 
int main()
{
 
// Your code here
 
return 0
}

除非并且直到您在代码中声明另一个函数,否则没有必要在Python代码中声明 main。所以我们可以在Python声明main如下。

蟒蛇3

# Python program to demonstrate
# declaring main
 
def main():
    # write your code here
 
if __name__=="__main__":
    main()

声明变量

在 C 和 C++ 中,我们首先声明变量的数据类型,然后声明变量的名称。数据类型的一些示例是 int、char、float、double 等。
Python不是“静态类型的”。我们不需要在使用它们之前声明变量或声明它们的类型。变量在我们第一次为其赋值的那一刻就被创建了。

C

// C program to demonstrate
// declaring variable
 
#include 
 
int main()
{
    // Declaring one variable at a time
    int a;
 
    // Declaring more than one variable
    char a, b, c, d;
 
    // Initializing variables
    float a = 0, b, c;
    b = 1;
 
    return 0;
}

C++

// C++ program to demonstrate
// declaring variables
 
#include 
 
int main()
{
    // Declaring one variable at a time
    int a;
 
    // Declaring more than one variable
    char a, b, c, d;
 
    // Initializing variables
    float a = 0, b, c;
    b = 1;
  
    return 0;
}

Python

# Python program to demonstrate
# creating variables
  
# An integer assignment
age = 45                     
   
# A floating point
salary = 1456.8            
   
# A string  
name = "John"             

打印到控制台

对于所有 3 种语言,将某些内容打印为输出的语法都不同。

C

// C program to showing
// how to print data
// on screen
 
#include 
 
int main()
{
   printf("Hello World");
   return 0;
}

C++

// C++ program to showing
// how to print data
// on screen
 
#include 
using namespace std;
 
int main()
{
    cout << "Hello World";
    return 0;
}

Python

# Python program to showing
# how to print data
# on screen
 
print("Hello World")

接受输入

所有三种语言的用户输入的语法都不同,所以让我们看看语法并用所有 3 种语言编写您的第一个基本代码。

C

// C program showing
// how to take input
// from user
 
#include 
 
int main()
{
    int a, b, c;
    printf("first number: ");
    scanf("%d", &a);
 
    printf("second number: ");
    scanf("%d", &b);
 
    c = a + b;
 
    printf("Hello World\n%d + %d = %d", a, b, c);
 
    return 0;
}

C++

// C++ program showing
// how to take input
// from user
 
#include 
using namespace std;
 
int main()
{
    int a, b, c;
    cout << "first number: ";
    cin >> a;
 
    cout << endl;
    cout << "second number: ";
    cin >> b;
 
    cout << endl;
    c = a + b;
 
    cout << "Hello World" << endl;
    cout << a << "+" << b << "=" << c;
 
    return 0;
}

Python

# Python program showing
# how to take input
# from user
 
a = input("first number: ")
b = input("second number: ")
 
c = a + b
 
print("Hello World")
print(a, "+", b, "=", c)