# **BENEFITS AND SOME COMMON MISTAKES IN PYTHON THAT YOU SHOULD AVOID** > [name=Trijeta Ghosh] * > ### ***ABOUT PYTHON*** * Python is an interpreted, object-oriented, high-level language with dynamic semantics developed programming language. * python is the most popular and demanding, usable programming language in the world. * python is very easy and simplistic. Python is used for solving multi-purpose, industrial problem, and real-world problems. It is used universally in general. * Its code readability, minimalism, and efficiency. --- * > ### ***BENEFITS OF PYTHON*** 1. Python is used in a data structure, with [Dynamic Typing](https://www.educative.io/answers/what-is-dynamic-typing//) and [Dynamic Binding](https://edukers.in/python-variables-what-is-dynamic-variable-binding-in-python/), that is helping to make the code very attractive and efficient for rapid application development. 2. It has a lot of flexibility to operate to write code in python, also it's an interpreted language. 3. Python is the most popular language, this is one of the top three programming languages, and it has the benefit to explore the global marketing industry. So you have a great chance to get a good job in industrial, technical and marketing areas all over the world. 4. Also it's helpful in the medical field, especially for those who are interested in data science and machine learning, python is a must-known language. As the pharmaceutical industry and biometric departments move into data science and machine learning area, programmers with python programming experience and knowledge can grow with the trend. --- > ### ***SOME COMMON MISTAKES*** ### **Misusing Expression** In Python, you can specify that a function argument is optional by giving its default value. It can create some confusion. ~~~~bar is voluntary and defaults to [] if not defined~~~~ > `>>> def foo(bar=[]): #bar is voluntary and defaults to [] if not defined > ...bar.append("baz") #this line can be problematic >... return bar ~~~~ In the above code, for example, one might expect that calling `foo()` repeatedly, would always return `'baz'` since the assumption would be that each time `foo()` is called `bar is set to`[]`(i.e new empty list). ~~~~example >>> foo() ["baz"] >>> foo() ["baz", "baz"] >>> foo() ["baz", "baz", "baz"] ~~~~ the bar argument is initialized to its default (i.e., an empty list) only when `foo()` is first defined, but then calls to `foo()`, will continue to use the same list to which `bar` was originally initialized. ~~~~example >>> def foo(bar=None): ... if the bar is None: ... bar = [] ... bar.append("baz") ... return bar ... >>> foo() ["baz"] >>> foo() ["baz"] >>> foo() ["baz"] ~~~~ To avoid misusing expressions, supply a suitable value for the optional argument because the default value for a function argument is only looked at once- once the function is defined. ### **Misusing The init Method** The init is a reserved method in python classes used as constructors. In object-oriented terminology, it is called a constructor And it gets called when Python allocates memory. ~~~~example int(value, base) ~~~~ The int() in python raises an error whenever we pass a data type other than a string or an integer. For example, if we try to typecast a list using int in python. The following error will be printed: ~~~~example TypeError: int() argument must be a string, a bytes-like object, or a number ~~~~ example: ~~~~example floating_number = 5.5 integer_number = int(floating_number) print("Integer number is:",integer_number) ~~~~ The int method raises an error when a non-string or non-integer data type is given as a parameter. The error raised is termed as TypeError ### ****syntax error**** Syntax errors are common mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Syntax errors occur when you enter a character or string that is unrecognizable to a system’s interpreter. Instead of executing the program, the parser raises the error, and the interpreter reports it. ~~~~~~~~example print(hello world) > syntaxError - invalid syntax ~~~~~~~~ we have to write like- ~~~~example print("hello world") ~~~~ you cannot use a reserved keyword to define a function. A keyword also cannot be assigned to a variable. Sometimes, you might move a keyword outside of a loop during development. In this case, your syntax error message will be more specific. It will read: SyntaxError: 'continue' not properly in the loop another example: ~~~~example num = int(input("Enter a number: ")) if (num % 2) == 0: print(" Even") else print(" Odd") ~~~~ where showing syntax error because after using else here use `:` in python. ~~~~example num = int(input("Enter a number: ")) if (num % 2) == 0: print(" Even") else: print(" Odd") ~~~~ ### **Not closing a previously opened file** In python sometimes we forgot to close the previous file and open a new file which should occur an error. So when we use python every time we close the opened file and then all the particular operations have been performed properly. Open files use your system resources and may be locked, if you do not close the files after using them, they will keep using the system resources and might prevent the other programs from using those resources. so we have to `close()`fuction after using`open()`function. ~~~~example with open('filename.txt', 'XYZ') as file: file.write('new_data') file.close() ~~~~ ### **LEGB Rule** The LEGB rule is a kind of name-lookup procedure, which determines the order in which Python looks up names. For example, if we access a name, then Python will look that name up sequentially in the local, enclosing, global, and built-in scope. It is the code block or body of any Python function or lambda expression Python scope resolution is based on what is known as the LEGB rule, which is shorthand for Local, Enclosing, Global, and Built-in. Python uses a different approach for scoping variables than other programming languages. If a user makes an assignment to a variable in a scope, that variable is automatically considered by Python to be local to that scope and shadows any similarly named variable in an outer scope. This error is particularly common for developers when using lists. > --- > # ***conclusion*** > In this article, we have covered python, its benefits, and some common mistakes that python programmers make. Avoid these mistakes and make your perfect code in python. Thanks for reading!