Header Ads Widget

Some common errors that a programmer faced in python

Some common Errors that a programmer faced in python  

Here are the some common errors that a coder faced during coding  in python . 

  1. Syntax errors
  2. Name errors
  3. Indentation errors
  4. Type errors
  5. Index errors
  6. Module errors

1. Syntax errors: These errors happen when the code violates the rules of the Python language, such as using an incorrect keyword, misspelling a variable or function name, or omitting parentheses or quotation marks. The interpreter will provide a message indicating where the error occurred and what type of error it is.

Example:-

# incorrect use of print function
pint("Hello, world!")
# Output: SyntaxError: invalid syntax


2. Name errors: These errors occur when the interpreter cannot locate a variable or function that is referenced in the code. This can occur if the variable or function is misspelled, has not yet been defined, or is out of scope. The interpreter will provide a message indicating that the name is not defined.
Example:-
# misspelled variable name
my_variable = 10
print(my_variabel)
# Output: NameError: name 'my_variabel' is not defined


3. Indentation errors: Since Python relies on indentation to define blocks of code, an indentation error will occur if the indentation is incorrect or inconsistent. This error happens when the interpreter encounters an unexpected indentation level, such as a mix of tabs and spaces or inconsistent use of spaces.
Example:-
# inconsistent use of spaces and tabs
def my_function():
    print("Hello, world!")
       print("This is a syntax error")
# Output: IndentationError: unexpected indent


4. Type errors: These errors occur when the code attempts to perform an operation on a variable or data type that is not compatible. For example, attempting to add an integer and a string or calling a method on an object that does not support that method. The interpreter will provide a message indicating that the types are not compatible.
Example:-
# trying to concatenate a string and integer
my_string = "Hello, "
my_number = 42
print(my_string + my_number)
# Output: TypeError: can only concatenate str (not "int") to str


5. Index errors: These errors occur when the code attempts to access an element in a sequence (such as a list or string) using an index that is out of range. The interpreter will provide a message indicating that the index is out of range.
Example:-
# accessing an index that is out of range
my_list = [1, 2, 3]
print(my_list[3])
# Output: IndexError: list index out of range


6. Value errors: These errors occur when the code attempts to perform an operation on a variable with an incorrect or unexpected value. For example, attempting to convert a string that cannot be parsed as an integer to an integer. The interpreter will provide a message indicating that the value is incorrect or unexpected.
Example:-
# trying to convert a string that cannot be parsed as an integer
my_string = "Hello, world!"
my_number = int(my_string)
# Output: ValueError: invalid literal for int() with base 10: 'Hello, world!'


7. Module errors: These errors occur when the code attempts to import a module that does not exist or cannot be found. The interpreter will provide a message indicating that the module cannot be found or imported. This can occur if the module is not installed or if the path to the module is not correctly specified.
Example:-
# trying to import a module that does not exist
import my_module
# Output: ModuleNotFoundError: No module named 'my_module'



Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-






Post a Comment

0 Comments