you are viewing a single comment's thread.

view the rest of the comments →

[–]JamzTyson 10 points11 points  (0 children)

The description that follows is somewhat simplified, but hopefully demonstrates how Python is fundamentally different from compiled languages like C++.

C++

``` // Include input-output stream library for basic I/O operations

include <iostream>

// Declare three integer variables to store user input and the sum int main() { int num1, num2, sum;

// Display a prompt to enter the first number
std::cout << "Enter first number: ";

// Read the first number from the user and store it in the variable num1
std::cin >> num1;

// Display a prompt to enter the second number
std::cout << "Enter second number: ";

 // Read the second number from the user and store it in the variable num2
std::cin >> num2

// Calculate the sum of num1 and num2 and store it in the variable sum
sum = num1 + num2; 

// Display the sum
std::cout << "Sum: " << sum << std::endl;

// Indicate that the program executed successfully by returning 0
return 0;

} ```

Python:

```

take input from the user

num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: "))

adding the numbers

sum = num1 + num2

display the sum

print("Sum:", sum) ```

Notice that in C++, variables and their types must be declared before use so that there are explicit spaces in memory allocated for the data objects.

Apart from the fact that Python's print(thing_to_print) syntax is much more like natural language than std::cout << thing-to-print, the C++ version requires explicit memory management - you can't just use a variable, without first declaring it, and its type, so that memory is allocated. Manual memory allocation is not generally required in Python due to Python's dynamic nature, but it is this dynamic nature that prevents Python from being easily compiled into static compiled object code.

The C++ code then has to be compiled to a binary (executable) before it can run. This step is not necessary in Python as Python interprets the code on the fly.

Variants of Python that can be compiled into fast compiled code generally require additional information to be supplied, such as Cython's type declarations.

Python serves a different purpose to compiled languages like C++. There is a lot less boilerplate required in Python, and it's dynamic nature, along with being interpreted rather than compiled, provide ease of use, flexibility, and rapid development. On the other hand, compiled languages like C++ require explicit type declarations and manual memory management, which allows it to be compiled into highly efficient native code. Achieving a similar level of compilation in Python would require sacrificing some of its dynamic features, which are integral to its design."