Software - General
1819802 Members
3264 Online
109607 Solutions
New Discussion

Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

 
pearlwilson
HPE Pro

Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Have you ever waited a long time for your python code to finish executing, only for it to fail midway? The first thought all of us would have, after our patience is thoroughly tested is: “Why didn’t it just fail in the beginning and save us the time?” and the answer to this question is the fact that Python is an interpreted language, and it reads and executes your code line-by-line.

Meanwhile, an error in a Compiled language like C, C++, C#, COBOL would have been caught before execution and this is because your source code gets compiled into machine code/binary before execution and any error that occurs, gets caught during the compilation phase, before it reaches the execution phase.

Here is an example on how this works with respect to Python and C++

Python

1.png

 

In the above Python code, we have a Type Error (This error happens because of a data type mismatch, here 10 is an integer, whereas ’20’ is a string), but this error is only detected after reaching line 6.

C++

2.png

The above C++ code fails before execution because of a type mismatch and hence did not print the statement.

Examples of some common run-time errors and the try-except block

In Python, the syntax errors are caught during the initial parsing, before the program is run. Here are some run-time errors that can be handled using the “try-except” block. These are the errors that you would come across frequently.

1: Type Error: This error occurs when an operation is performed on an object of an inappropriate type. Like in the example above in which an integer was being added to a string.

Handling: You can handle this error by using either a print statement asking for the right object type to be entered or if it or if you are expecting a specific kind of error to happen like in this case a string being entered, you can write the code to handle a string by converting it into an integer.

3.png

2: Value Error: This error occurs when an operation is performed on an object of the right type, but inappropriate value. In the below example, the attempt to convert a non- numeric string (“Heyyy”) into an integer throws a Value Error.

Handling: Print statement

4.png

3: Index Error: This error occurs when your list index is out of range. In the below example, it looks for the value on index 5, but the list only has 3 values (0-2 index).

Handling: Print statement/ alternate custom error handling – In the below case I will look for (n-3) index if 5 is not found.

5.png

4: Key Error: This error occurs when a dictionary key is not found. If you are someone who uses dictionaries a lot, this error could cause major complications.

Handling: Print statements

6.png

There is a broad range of built-in exception-handling available in Python than can be leveraged to enhance code readability and maintainability.

Pros and Cons of an interpreted language

Python’s extensive standard libraries, third-party libraries like numpy, pandas, and requests, along with cross-platform support and scripting capabilities, make it the perfect language for automation. Python allows dynamic typing, meaning you don't need to define a datatype for a variable, unlike in compiled languages. This allows programmers to focus more on solving problems than managing data types explicitly. However, this feature can cause different runtime errors as discussed above.

While Python's error detection might take more time, leading to increased typing and interaction, it excels in debugging with print statements. It simplifies testing small snippets of code interactively. Although a detailed analysis of both types of languages requires considering factors like portability and performance, understanding why Python works the way it does and leveraging its dynamic interactive error reporting can enhance productivity and provide immediate feedback.

So, the next time you're frustrated, waiting for an execution result, remember that it could just be a blessing in disguise.

 

I'm an HPE employee.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
5 REPLIES 5
dhanalakshmib
Visitor

Re: Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Great Read!!! Valuable insights on Python versus compiled languages, thanks for sharing!

Arun_KT
Occasional Visitor

Re: Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Hello Pearl,

The article is well-written, informative, and provides a good balance of technical detail and readability.

Great job!

VaidehiK
Occasional Visitor

Re: Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Well written, easy to understand the fundamentals of language. Thanks for sharing!

Pramodmadhavan
HPE Pro

Re: Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Hi Pearl,

I wanted to take a moment to commend you on the fantastic article you wrote. Your insights and thorough really shone through, making it an engaging and informative read. It's clear that you put a lot of effort into it, and your dedication is truly appreciated.

Regards

Pramod

I'm an HPE employee.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
pearlwilson
HPE Pro

Re: Python's Late Bloomers vs. Compiled Languages' Early Birds: The Comedy of Errors

Thank you Pramod!

I'm an HPE employee.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo